Skip to content

Commit d24633e

Browse files
committed
Cambio logica di globalizzazione delle informazioni di sessione
1 parent cc3e0c4 commit d24633e

File tree

4 files changed

+54
-78
lines changed

4 files changed

+54
-78
lines changed

.eslintrc.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ module.exports = {
1212
"no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off",
1313
},
1414
globals: {
15-
defineProps: 'readonly',
16-
defineEmits: 'readonly',
17-
defineExpose: 'readonly',
18-
withDefaults: 'readonly'
19-
}
15+
defineProps: "readonly",
16+
defineEmits: "readonly",
17+
defineExpose: "readonly",
18+
withDefaults: "readonly",
19+
},
2020
};

src/App.vue

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@
1414
<Searchbar @search-offer="searchOffer" />
1515
<Navbar v-if="sessionToken" />
1616
</header>
17-
<router-view :ads="ads" :userInfo="userInfo" />
17+
<router-view
18+
:ads="ads"
19+
v-if="
20+
(getCookie('sessionToken') !== '' && userInfo._id !== '') ||
21+
getCookie('sessionToken') == ''
22+
"
23+
/>
1824
</template>
1925

2026
<script>
2127
import Searchbar from "@/components/Searchbar.vue";
2228
import Navbar from "@/components/Navbar.vue";
2329
24-
import { defineComponent, ref, reactive, watch } from "vue";
30+
import { defineComponent, ref, reactive, watch, provide } from "vue";
2531
import { useRoute } from "vue-router";
2632
2733
export default defineComponent({
@@ -40,6 +46,7 @@ export default defineComponent({
4046
biography: "",
4147
sessionToken: "",
4248
});
49+
provide("userInfo", userInfo);
4350
4451
const searchOffer = async function (searchterms) {
4552
console.log("Searching for: " + searchterms);
@@ -124,7 +131,14 @@ export default defineComponent({
124131
},
125132
];
126133
127-
return { searchOffer, sessionToken, showLoginBtn, ads, userInfo };
134+
return {
135+
searchOffer,
136+
sessionToken,
137+
showLoginBtn,
138+
ads,
139+
userInfo,
140+
getCookie,
141+
};
128142
},
129143
});
130144
</script>

src/views/Annunci.vue

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,32 @@
4141

4242
<script setup>
4343
import SearchResult from "@/components/SearchResult.vue";
44-
import { watch, ref } from "vue";
44+
import { watch, ref, inject } from "vue";
4545
import { useRouter } from "vue-router";
4646
const router = useRouter();
4747
48-
const props = defineProps({
49-
userInfo: {
50-
_id: String,
51-
},
52-
});
48+
const userInfo = inject("userInfo");
5349
5450
let ads = ref([]);
5551
5652
watch(
57-
() => props.userInfo._id,
53+
() => userInfo._id,
5854
async () => {
59-
if (props.userInfo._id !== "") {
55+
if (userInfo._id !== "") {
6056
await loadAds();
6157
}
6258
}
6359
);
6460
6561
async function loadAds() {
66-
let adsFetch = await fetch(`/api/ads/list/${props.userInfo._id}`);
62+
let adsFetch = await fetch(`/api/ads/list/${userInfo._id}`);
6763
6864
if (adsFetch.ok) {
6965
ads.value = await adsFetch.json();
7066
}
7167
}
7268
73-
if (props.userInfo._id !== "") loadAds();
69+
if (userInfo._id !== "") loadAds();
7470
7571
document.title = "TeacherFinder – I miei annunci";
7672
</script>

src/views/Impostazioni.vue

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<h1>Impostazioni</h1>
33
<div class="impostazioni tf-box">
4-
<UserCard v-bind="props.userInfo" />
4+
<UserCard v-bind="userInfo" />
55
<TextEntry description="Nickname" v-model:text="form.nickname" />
66
<TextEntry
77
description="Biografia"
@@ -68,12 +68,14 @@
6868
</style>
6969

7070
<script setup>
71-
import { reactive, watch, computed } from "vue";
71+
import { reactive, computed, inject } from "vue";
7272
import UserCard from "@/components/UserCard.vue";
7373
import TextEntry from "@/components/TextEntry.vue";
7474
import ToggleEntry from "@/components/ToggleEntry.vue";
7575
7676
document.title = "TeacherFinder – Impostazioni";
77+
const userInfo = reactive(inject("userInfo"));
78+
console.log(userInfo);
7779
7880
const notificationsString = (formObject) => {
7981
let str = "";
@@ -97,60 +99,24 @@ const notificationsStringToFormObject = (str) => {
9799
};
98100
99101
const hasChanged = (fieldName) => {
100-
return form[fieldName] != props.userInfo[fieldName];
102+
return form[fieldName] != userInfo[fieldName];
101103
};
102104
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-
114105
const form = reactive({
115-
nickname: props.userInfo.nickname,
116-
biography: props.userInfo.biography,
106+
nickname: userInfo.nickname,
107+
biography: userInfo.biography,
117108
notifications: {
118-
...notificationsStringToFormObject(props.userInfo.notifications),
109+
...notificationsStringToFormObject(userInfo.notifications),
119110
},
120111
});
121112
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-
146113
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")
151117
Object.assign(
152118
form.notifications,
153-
notificationsStringToFormObject(props.userInfo.notifications)
119+
notificationsStringToFormObject(userInfo.notifications)
154120
);
155121
}
156122
@@ -164,19 +130,17 @@ const logout = async () => {
164130
};
165131
166132
const cancelEdits = () => {
167-
form.nickname = props.userInfo.nickname;
168-
form.biography = props.userInfo.biography;
133+
form.nickname = userInfo.nickname;
134+
form.biography = userInfo.biography;
169135
form.notifications = {
170-
...notificationsStringToFormObject(props.userInfo.notifications),
136+
...notificationsStringToFormObject(userInfo.notifications),
171137
};
172138
};
173139
174-
loadSettings();
175-
176140
const saveEdits = async () => {
177141
// REST api salvataggio modifiche
178142
let query = {
179-
sessionToken: props.userInfo.sessionToken,
143+
sessionToken: userInfo.sessionToken,
180144
updates: {},
181145
};
182146
if (hasChanged("nickname")) query.updates.nickname = form.nickname;
@@ -198,26 +162,28 @@ const saveEdits = async () => {
198162
console.log(res);
199163
200164
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;
203169
}
204170
}
205171
};
206172
207173
const haveNotificationsChanged = () => {
208-
return (
209-
notificationsString(form.notifications) != props.userInfo.notifications
210-
);
174+
return notificationsString(form.notifications) != userInfo.notifications;
211175
};
212176
213177
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);
217181
return !(
218182
hasChanged("nickname") ||
219183
hasChanged("biography") ||
220184
haveNotificationsChanged()
221185
);
222186
});
187+
188+
loadSettings();
223189
</script>

0 commit comments

Comments
 (0)