1
1
<template >
2
2
<h1 >Impostazioni</h1 >
3
3
<div class =" impostazioni tf-box" >
4
- <UserCard v-bind =" props. userInfo" />
4
+ <UserCard v-bind =" userInfo" />
5
5
<TextEntry description =" Nickname" v-model:text =" form.nickname" />
6
6
<TextEntry
7
7
description =" Biografia"
68
68
</style >
69
69
70
70
<script setup>
71
- import { reactive , watch , computed } from " vue" ;
71
+ import { reactive , computed , inject } from " vue" ;
72
72
import UserCard from " @/components/UserCard.vue" ;
73
73
import TextEntry from " @/components/TextEntry.vue" ;
74
74
import ToggleEntry from " @/components/ToggleEntry.vue" ;
75
75
76
76
document .title = " TeacherFinder – Impostazioni" ;
77
+ const userInfo = reactive (inject (" userInfo" ));
78
+ console .log (userInfo);
77
79
78
80
const notificationsString = (formObject ) => {
79
81
let str = " " ;
@@ -97,60 +99,24 @@ const notificationsStringToFormObject = (str) => {
97
99
};
98
100
99
101
const hasChanged = (fieldName ) => {
100
- return form[fieldName] != props . userInfo [fieldName];
102
+ return form[fieldName] != userInfo[fieldName];
101
103
};
102
104
103
- const props = defineProps ({
104
- ads: Array ,
105
- userInfo: {
106
- firstName: String ,
107
- lastName: String ,
108
- nickname: String ,
109
- biography: String ,
110
- notifications: String ,
111
- },
112
- });
113
-
114
105
const form = reactive ({
115
- nickname: props . userInfo .nickname ,
116
- biography: props . userInfo .biography ,
106
+ nickname: userInfo .nickname ,
107
+ biography: userInfo .biography ,
117
108
notifications: {
118
- ... notificationsStringToFormObject (props . userInfo .notifications ),
109
+ ... notificationsStringToFormObject (userInfo .notifications ),
119
110
},
120
111
});
121
112
122
- watch (
123
- () => props .userInfo .nickname ,
124
- () => {
125
- form .nickname = props .userInfo .nickname ;
126
- }
127
- );
128
-
129
- watch (
130
- () => props .userInfo .biography ,
131
- () => {
132
- form .biography = props .userInfo .biography ;
133
- }
134
- );
135
-
136
- watch (
137
- () => props .userInfo .notifications ,
138
- () => {
139
- Object .assign (
140
- form .notifications ,
141
- notificationsStringToFormObject (props .userInfo .notifications )
142
- );
143
- }
144
- );
145
-
146
113
function loadSettings () {
147
- if (props .userInfo .nickname !== " " ) form .nickname = props .userInfo .nickname ;
148
- if (props .userInfo .biography !== " " )
149
- form .biography = props .userInfo .biography ;
150
- if (typeof props .userInfo .notifications ? .rifiutato !== " undefined" )
114
+ if (userInfo .nickname !== " " ) form .nickname = userInfo .nickname ;
115
+ if (userInfo .biography !== " " ) form .biography = userInfo .biography ;
116
+ if (typeof userInfo .notifications ? .rifiutato !== " undefined" )
151
117
Object .assign (
152
118
form .notifications ,
153
- notificationsStringToFormObject (props . userInfo .notifications )
119
+ notificationsStringToFormObject (userInfo .notifications )
154
120
);
155
121
}
156
122
@@ -164,19 +130,17 @@ const logout = async () => {
164
130
};
165
131
166
132
const cancelEdits = () => {
167
- form .nickname = props . userInfo .nickname ;
168
- form .biography = props . userInfo .biography ;
133
+ form .nickname = userInfo .nickname ;
134
+ form .biography = userInfo .biography ;
169
135
form .notifications = {
170
- ... notificationsStringToFormObject (props . userInfo .notifications ),
136
+ ... notificationsStringToFormObject (userInfo .notifications ),
171
137
};
172
138
};
173
139
174
- loadSettings ();
175
-
176
140
const saveEdits = async () => {
177
141
// REST api salvataggio modifiche
178
142
let query = {
179
- sessionToken: props . userInfo .sessionToken ,
143
+ sessionToken: userInfo .sessionToken ,
180
144
updates: {},
181
145
};
182
146
if (hasChanged (" nickname" )) query .updates .nickname = form .nickname ;
@@ -198,26 +162,28 @@ const saveEdits = async () => {
198
162
console .log (res);
199
163
200
164
if (res .acknowledged && res .modifiedCount >= 1 ) {
201
- window .location .reload ();
202
- // Non posso aggiornare le prop, quindi aggiorno per ricaricare tutto
165
+ if (query .updates .nickname ) userInfo .nickname = query .updates .nickname ;
166
+ if (query .updates .biography ) userInfo .biography = query .updates .biography ;
167
+ if (query .updates .notifications )
168
+ userInfo .notifications = query .updates .notifications ;
203
169
}
204
170
}
205
171
};
206
172
207
173
const haveNotificationsChanged = () => {
208
- return (
209
- notificationsString (form .notifications ) != props .userInfo .notifications
210
- );
174
+ return notificationsString (form .notifications ) != userInfo .notifications ;
211
175
};
212
176
213
177
const settingsModified = computed (() => {
214
- console .log (props . userInfo .nickname , form .nickname );
215
- console .log (props . userInfo .biography , form .biography );
216
- console .log (props . userInfo .notifications , form .notifications );
178
+ console .log (userInfo .nickname , form .nickname );
179
+ console .log (userInfo .biography , form .biography );
180
+ console .log (userInfo .notifications , form .notifications );
217
181
return ! (
218
182
hasChanged (" nickname" ) ||
219
183
hasChanged (" biography" ) ||
220
184
haveNotificationsChanged ()
221
185
);
222
186
});
187
+
188
+ loadSettings ();
223
189
< / script>
0 commit comments