-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNotificationPopup.vue
199 lines (187 loc) · 5.66 KB
/
NotificationPopup.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<script lang="ts" setup>
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'
import ScrollArea from '../ui/scroll-area/ScrollArea.vue'
import {
getListNotificationApi,
markSpecificNotificationApi,
markAllNotificationApi,
} from '@/services/notification'
import { useNotificationSocketStore } from '@/stores/socket/notification'
import type { INotification } from '@/types/notification'
import type { IPaging } from '@/types'
import { showToast } from '@/utils/toast'
import { apiError } from '@/utils/exceptionHandler'
import { formatCommentDateTime } from '@/utils/time'
import router from '@/routers/router'
const notificationStore = useNotificationSocketStore()
const emits = defineEmits<{
(e: 'readAllNotification', value: boolean): void
}>()
const getMessage = computed(() => {
return notificationStore.getMessage
})
const listNotification = ref<INotification[]>([])
const isLoading = ref(false)
const metaPage = ref<IPaging>()
watch(getMessage, (val: any) => {
if (val.event === 'notification') {
listNotification.value.unshift(val.data)
}
})
const getListNotification = async (page = 1) => {
try {
isLoading.value = true
const data = await getListNotificationApi(page)
listNotification.value.push(...data.data)
metaPage.value = data.meta
} catch (error) {
showToast({
description: apiError(error).message,
variant: 'destructive',
})
}
isLoading.value = false
}
const handleMarkSpecific = async (id: string) => {
try {
markSpecificNotificationApi(id)
} catch (error) {
showToast({
description: apiError(error).message,
variant: 'destructive',
})
}
}
const handleDetailNoti = (item: INotification) => {
if (item.notification_type === 'POST') {
router.push(`/groups/${item.target_id}/posts/${item.object.id}`)
}
if (item.notification_type === 'COMMENT') {
router.push(`/groups/${item.target_id}/posts/${item.object.post_id}`)
}
handleMarkSpecific(item.id)
}
const handleLoadMore = () => {
if (metaPage.value?.has_next_page) {
getListNotification(metaPage.value.current_page + 1)
}
}
const handleMarkAllNotification = async () => {
try {
await markAllNotificationApi()
emits('readAllNotification', true)
listNotification.value.forEach((item) => {
if (!item.is_read) {
item.is_read = true
}
})
} catch (error) {
showToast({
description: apiError(error).message,
variant: 'destructive',
})
}
}
onMounted(() => {
getListNotification()
})
</script>
<template>
<div class="flex flex-col bg-white w-[400px] shadow-lg rounded-lg">
<div class="flex p-4 items-center justify-between rounded-t-lg">
<h3 class="text-base font-medium">Notification</h3>
<p
class="cursor-pointer text-sm text-primary"
@click.stop.prevent="handleMarkAllNotification()"
>
Read all
</p>
</div>
<div class="h-0.5 w-full bg-slate-100"></div>
<ScrollArea>
<div class="flex flex-col cursor-pointer h-[400px] pr-2">
<div
v-for="(item, index) in listNotification"
:key="index"
class=""
>
<div
class="flex gap-3 p-4 hover:bg-slate-50"
@click="handleDetailNoti(item)"
>
<Avatar class="border-2">
<AvatarImage :src="item.agent.avatar" />
<AvatarFallback>{{ item.agent.name.charAt(0).toUpperCase() }}</AvatarFallback>
</Avatar>
<div class="flex flex-col gap-2 w-full relative">
<div class="flex gap-0 flex-col">
<div class="flex items-end gap-1">
<h3 class="text-base font-semibold">{{ item.agent.name }}</h3>
<p
v-if="item.notification_type == 'COMMENT'"
class="text-sm font-normal text-slate-700"
>
{{ item.content }}
</p>
</div>
<p
v-if="item.notification_type == 'POST'"
class="text-sm font-normal text-slate-700"
>
{{ item.content }}
</p>
<p
v-else
class="text-sm font-normal text-slate-700 description"
>
{{ item.description }}
</p>
</div>
<div class="w-full flex">
<p class="w-full text-right text-xs font-normal text-slate-500">
{{ formatCommentDateTime(item.created_at) }}
</p>
</div>
<div
v-if="!item.is_read"
class="absolute -top-1 -right-1 w-3 h-3 bg-primary rounded-full"
></div>
</div>
</div>
<div class="h-px w-full bg-slate-100"></div>
</div>
<div
v-if="isLoading"
class="flex justify-center items-center my-3"
>
<span class="i-svg-spinners-180-ring-with-bg text-primary"></span>
</div>
</div>
</ScrollArea>
<div class="h-0.5 w-full bg-slate-100"></div>
<div
v-if="metaPage?.has_next_page"
class="flex items-center justify-center my-3 gap-2"
>
<p
class="text-sm text-primary cursor-pointer"
@click.stop.prevent="handleLoadMore()"
>
See more
</p>
<span
v-if="isLoading"
class="i-svg-spinners-180-ring-with-bg text-primary"
></span>
</div>
</div>
</template>
<style lang="scss" scoped>
.description {
display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
}
</style>