|
| 1 | +<script setup lang="ts"> |
| 2 | +import { BellOutlined, CheckCircleOutlined, CloseCircleOutlined, DeleteOutlined, InfoCircleOutlined, WarningOutlined } from '@ant-design/icons-vue' |
| 3 | +import { useGettext } from 'vue3-gettext' |
| 4 | +import type { Ref } from 'vue' |
| 5 | +import { message } from 'ant-design-vue' |
| 6 | +import notification from '@/api/notification' |
| 7 | +import type { Notification } from '@/api/notification' |
| 8 | +import { NotificationTypeT } from '@/constants' |
| 9 | +import { useUserStore } from '@/pinia' |
| 10 | +
|
| 11 | +const { $gettext } = useGettext() |
| 12 | +const loading = ref(false) |
| 13 | +
|
| 14 | +const { unreadCount } = storeToRefs(useUserStore()) |
| 15 | +
|
| 16 | +const data = ref([]) as Ref<Notification[]> |
| 17 | +function init() { |
| 18 | + loading.value = true |
| 19 | + notification.get_list().then(r => { |
| 20 | + data.value = r.data |
| 21 | + unreadCount.value = r.pagination.total |
| 22 | + }).catch(e => { |
| 23 | + message.error($gettext(e?.message ?? 'Server error')) |
| 24 | + }).finally(() => { |
| 25 | + loading.value = false |
| 26 | + }) |
| 27 | +} |
| 28 | +
|
| 29 | +onMounted(() => { |
| 30 | + init() |
| 31 | +}) |
| 32 | +
|
| 33 | +const open = ref(false) |
| 34 | +
|
| 35 | +watch(open, v => { |
| 36 | + if (v) |
| 37 | + init() |
| 38 | +}) |
| 39 | +
|
| 40 | +function clear() { |
| 41 | + notification.clear().then(() => { |
| 42 | + message.success($gettext('Cleared successfully')) |
| 43 | + data.value = [] |
| 44 | + unreadCount.value = 0 |
| 45 | + }).catch(e => { |
| 46 | + message.error($gettext(e?.message ?? 'Server error')) |
| 47 | + }) |
| 48 | +} |
| 49 | +
|
| 50 | +function remove(id: number) { |
| 51 | + notification.destroy(id).then(() => { |
| 52 | + message.success($gettext('Removed successfully')) |
| 53 | + init() |
| 54 | + }).catch(e => { |
| 55 | + message.error($gettext(e?.message ?? 'Server error')) |
| 56 | + }) |
| 57 | +} |
| 58 | +
|
| 59 | +const router = useRouter() |
| 60 | +function viewAll() { |
| 61 | + router.push('/notifications') |
| 62 | + open.value = false |
| 63 | +} |
| 64 | +</script> |
| 65 | + |
| 66 | +<template> |
| 67 | + <span class="cursor-pointer"> |
| 68 | + <APopover |
| 69 | + v-model:open="open" |
| 70 | + placement="bottomRight" |
| 71 | + overlay-class-name="notification-popover" |
| 72 | + trigger="click" |
| 73 | + > |
| 74 | + <ABadge |
| 75 | + :count="unreadCount" |
| 76 | + dot |
| 77 | + > |
| 78 | + <BellOutlined /> |
| 79 | + </ABadge> |
| 80 | + <template #content> |
| 81 | + <div class="flex justify-between items-center p-2"> |
| 82 | + <h3 class="mb-0">{{ $gettext('Notifications') }}</h3> |
| 83 | + <APopconfirm |
| 84 | + :cancel-text="$gettext('No')" |
| 85 | + :ok-text="$gettext('OK')" |
| 86 | + :title="$gettext('Are you sure you want to clear all notifications?')" |
| 87 | + placement="bottomRight" |
| 88 | + @confirm="clear" |
| 89 | + > |
| 90 | + <a> |
| 91 | + {{ $gettext('Clear') }} |
| 92 | + </a> |
| 93 | + </APopconfirm> |
| 94 | + </div> |
| 95 | + |
| 96 | + <ADivider class="mt-2 mb-2" /> |
| 97 | + |
| 98 | + <AList |
| 99 | + :data-source="data" |
| 100 | + class="max-h-96 overflow-scroll" |
| 101 | + > |
| 102 | + <template #renderItem="{ item }"> |
| 103 | + <AListItem> |
| 104 | + <template #actions> |
| 105 | + <span |
| 106 | + key="list-loadmore-remove" |
| 107 | + class="cursor-pointer" |
| 108 | + @click="remove(item.id)" |
| 109 | + > |
| 110 | + <DeleteOutlined /> |
| 111 | + </span> |
| 112 | + </template> |
| 113 | + <AListItemMeta |
| 114 | + :title="item.title" |
| 115 | + :description="item.details" |
| 116 | + > |
| 117 | + <template #avatar> |
| 118 | + <div> |
| 119 | + <CloseCircleOutlined |
| 120 | + v-if="item.type === NotificationTypeT.Error" |
| 121 | + class="text-red-500" |
| 122 | + /> |
| 123 | + <WarningOutlined |
| 124 | + v-else-if="item.type === NotificationTypeT.Warning" |
| 125 | + class="text-orange-400" |
| 126 | + /> |
| 127 | + <InfoCircleOutlined |
| 128 | + v-else-if="item.type === NotificationTypeT.Info" |
| 129 | + class="text-blue-500" |
| 130 | + /> |
| 131 | + <CheckCircleOutlined |
| 132 | + v-else-if="item.type === NotificationTypeT.Success" |
| 133 | + class="text-green-500" |
| 134 | + /> |
| 135 | + </div> |
| 136 | + </template> |
| 137 | + </AListItemMeta> |
| 138 | + </AListItem> |
| 139 | + </template> |
| 140 | + </AList> |
| 141 | + <ADivider class="m-0 mb-2" /> |
| 142 | + <div class="flex justify-center p-2"> |
| 143 | + <a @click="viewAll">{{ $gettext('View all notifications') }}</a> |
| 144 | + </div> |
| 145 | + </template> |
| 146 | + </APopover> |
| 147 | + </span> |
| 148 | +</template> |
| 149 | + |
| 150 | +<style lang="less"> |
| 151 | +.notification-popover { |
| 152 | + width: 400px; |
| 153 | +} |
| 154 | +</style> |
| 155 | + |
| 156 | +<style scoped lang="less"> |
| 157 | +:deep(.ant-list-item-meta) { |
| 158 | + align-items: center !important; |
| 159 | +} |
| 160 | +
|
| 161 | +:deep(.ant-list-item-meta-avatar) { |
| 162 | + font-size: 24px; |
| 163 | +} |
| 164 | +</style> |
0 commit comments