1+ <template >
2+ <ConfirmLeavePageDialog @cancel =" () => { showConfirmLeaveDialog = false ; leaveTo = null } "
3+ @ok =" () => { showConfirmLeaveDialog = false ; skipConfirmLeavePageDialog = true } "
4+ v-model =" showConfirmLeaveDialog " :leaveTo =" leaveTo " />
5+ <SettingsForm v-bind =" { initialSettings , loadingSaveSettings , hasChanges } " @changeSettings =" settings = $event " @save =" saveSettings " />
6+ </template >
7+
8+ <script setup>
9+ import { ref , computed } from ' vue'
10+ import ConfirmLeavePageDialog from ' ./ConfirmLeavePageDialog.vue'
11+ import SettingsForm from ' ./SettingsForm.vue'
12+ import { onBeforeRouteLeave } from ' vue-router'
13+ import axios from ' @/services/axios'
14+ import { useSnacksStore } from ' @/stores/snacks'
15+
16+ const snacksStore = useSnacksStore ()
17+ const initialSettings = ref ({});
18+ await axios .get (' /users/settings' ).then (({ data }) => {
19+ initialSettings .value = data;
20+ })
21+
22+ const settings = ref ({ ... initialSettings .value });
23+ const showConfirmLeaveDialog = ref (false );
24+ const leaveTo = ref (null );
25+ const skipConfirmLeavePageDialog = ref (false );
26+ const loadingSaveSettings = ref (false );
27+
28+ const hasChanges = computed (() => {
29+ return JSON .stringify (initialSettings .value ) !== JSON .stringify (settings .value );
30+ })
31+
32+ onBeforeRouteLeave (to => {
33+ if (! hasChanges .value || skipConfirmLeavePageDialog .value ) {
34+ return true
35+ }
36+ showConfirmLeaveDialog .value = true
37+ leaveTo .value = to
38+ return false
39+ });
40+
41+ async function saveSettings () {
42+ loadingSaveSettings .value = true
43+ try {
44+ await axios .put (' /users/settings' , { ... settings .value });
45+ initialSettings .value = { ... settings .value }
46+ snacksStore .addSnack ({
47+ text: ' ⚙️ ההעדפות שלך נשמרו בהצלחה' ,
48+ color: ' success'
49+ })
50+ } catch (err) {
51+ snacksStore .addSnack ({
52+ text: err .message ,
53+ color: ' error'
54+ })
55+ } finally {
56+ loadingSaveSettings .value = false
57+ }
58+ }
59+ </script >
0 commit comments