96
96
</div >
97
97
98
98
<Divider />
99
+ <div
100
+ v-if =" pushEnabled"
101
+ class =" mt-4 w-full text-center"
102
+ >
103
+ <p
104
+ v-if =" loading || isSubscribed === null"
105
+ class =" text-gray-500 text-sm"
106
+ >
107
+ <i class =" mdi mdi-loading mdi-spin mr-2" ></i >
108
+ {{ t("Checking push subscription...") }}
109
+ </p >
110
+
111
+ <div v-else >
112
+ <template v-if =" isSubscribed " >
113
+ <div class =" flex flex-col items-center text-green-700" >
114
+ <i class =" mdi mdi-bell-ring-outline text-4xl mb-2" ></i >
115
+ <p class =" text-sm font-semibold" >
116
+ {{ t("You're subscribed to push notifications in this browser.") }}
117
+ </p >
118
+ <BaseButton
119
+ :label =" t('Unsubscribe')"
120
+ class =" mt-2"
121
+ icon =" bell-off"
122
+ type =" danger"
123
+ size =" small"
124
+ @click =" handleUnsubscribe"
125
+ :loading =" loading"
126
+ />
127
+ <div
128
+ v-if =" showDetails"
129
+ class =" mt-2 bg-gray-100 rounded p-2 text-gray-800 text-xs break-all max-w-full"
130
+ >
131
+ <strong >{{ t("Endpoint") }}:</strong >
132
+ <br />
133
+ {{ subscriptionInfo?.endpoint }}
134
+ </div >
135
+ </div >
136
+ </template >
137
+
138
+ <template v-else >
139
+ <div class =" flex flex-col items-center text-red-700" >
140
+ <i class =" mdi mdi-bell-off-outline text-4xl mb-2" ></i >
141
+ <p class =" text-sm" >
142
+ {{ t("Push notifications are not enabled in this browser.") }}
143
+ </p >
144
+ <BaseButton
145
+ :label =" t('Enable notifications')"
146
+ class =" mt-2"
147
+ icon =" bell"
148
+ type =" primary"
149
+ size =" small"
150
+ @click =" handleSubscribe"
151
+ :loading =" loading"
152
+ />
153
+ </div >
154
+ </template >
155
+ </div >
156
+ </div >
157
+
99
158
<BaseButton
100
159
v-if =" isCurrentUser || securityStore.isAdmin"
101
160
:label =" t('Edit profile')"
117
176
</template >
118
177
119
178
<script setup>
120
- import { computed , inject , ref , watchEffect } from " vue"
179
+ import { computed , inject , onMounted , ref , watchEffect } from " vue"
121
180
import BaseCard from " ../basecomponents/BaseCard.vue"
122
181
import BaseButton from " ../basecomponents/BaseButton.vue"
123
182
import { useI18n } from " vue-i18n"
124
183
import Divider from " primevue/divider"
125
184
import axios from " axios"
126
185
import { useSecurityStore } from " ../../store/securityStore"
127
186
import BaseUserAvatar from " ../basecomponents/BaseUserAvatar.vue"
187
+ import { usePushSubscription } from " ../../composables/usePushSubscription"
128
188
129
189
const { t } = useI18n ()
130
190
const securityStore = useSecurityStore ()
@@ -138,17 +198,31 @@ const showFullProfile = computed(() => isCurrentUser.value || securityStore.isAd
138
198
const languageInfo = ref (null )
139
199
const vCardUserLink = ref (" " )
140
200
const visibility = ref ({})
141
- watchEffect (() => {
142
- if (user .value && user .value .id ) {
143
- fetchUserProfile (user .value .id )
144
- }
145
- })
146
201
147
- const editProfile = () => {
202
+ const {
203
+ isSubscribed ,
204
+ subscriptionInfo ,
205
+ subscribe ,
206
+ unsubscribe ,
207
+ loading ,
208
+ checkSubscription ,
209
+ loadVapidKey ,
210
+ vapidPublicKey ,
211
+ pushEnabled ,
212
+ registerServiceWorker ,
213
+ } = usePushSubscription ()
214
+
215
+ const showDetails = ref (false )
216
+
217
+ function toggleDetails () {
218
+ showDetails .value = ! showDetails .value
219
+ }
220
+
221
+ function editProfile () {
148
222
window .location = " /account/edit"
149
223
}
150
224
151
- const changePassword = () => {
225
+ function changePassword () {
152
226
window .location = " /account/change-password"
153
227
}
154
228
@@ -170,8 +244,33 @@ async function fetchUserProfile(userId) {
170
244
171
245
function flagIconExists (code ) {
172
246
const mdiFlagIcons = [" us" , " fr" , " de" , " es" , " it" , " pl" ]
173
- return mdiFlagIcons .includes (code .toLowerCase ())
247
+ return mdiFlagIcons .includes (code? .toLowerCase ())
174
248
}
175
249
176
250
function chatWith (userId , completeName , isOnline , avatarSmall ) {}
251
+
252
+ watchEffect (async () => {
253
+ if (user .value && user .value .id ) {
254
+ fetchUserProfile (user .value .id )
255
+ loadVapidKey ()
256
+ await registerServiceWorker ()
257
+ await checkSubscription (user .value .id )
258
+ }
259
+ })
260
+
261
+ async function handleSubscribe () {
262
+ if (user .value ? .id ) {
263
+ await subscribe (user .value .id )
264
+ } else {
265
+ console .error (" [Push] No user id for subscription." )
266
+ }
267
+ }
268
+
269
+ async function handleUnsubscribe () {
270
+ if (user .value ? .id ) {
271
+ await unsubscribe (user .value .id )
272
+ } else {
273
+ console .error (" [Push] No user id for unsubscription." )
274
+ }
275
+ }
177
276
< / script>
0 commit comments