mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-05-10 20:57:28 +08:00
Revert "feat: add paginated notifications and unread endpoint"
This reverts commit 73168c1859.
This commit is contained in:
@@ -53,13 +53,13 @@
|
||||
</div>
|
||||
|
||||
<BasePlaceholder
|
||||
v-else-if="notifications.length === 0"
|
||||
v-else-if="filteredNotifications.length === 0"
|
||||
text="暂时没有消息 :)"
|
||||
icon="fas fa-inbox"
|
||||
/>
|
||||
|
||||
<div class="timeline-container" v-if="notifications.length > 0">
|
||||
<BaseTimeline :items="notifications">
|
||||
<div class="timeline-container" v-if="filteredNotifications.length > 0">
|
||||
<BaseTimeline :items="filteredNotifications">
|
||||
<template #item="{ item }">
|
||||
<div class="notif-content" :class="{ read: item.read }">
|
||||
<span v-if="!item.read" class="unread-dot"></span>
|
||||
@@ -505,23 +505,16 @@
|
||||
</div>
|
||||
</template>
|
||||
</BaseTimeline>
|
||||
<InfiniteLoadMore
|
||||
:key="ioKey"
|
||||
:on-load="fetchNextPage"
|
||||
:pause="isLoadingMessage"
|
||||
root-margin="200px 0px"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed, onActivated, ref, watch } from 'vue'
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
import BasePlaceholder from '~/components/BasePlaceholder.vue'
|
||||
import BaseTimeline from '~/components/BaseTimeline.vue'
|
||||
import NotificationContainer from '~/components/NotificationContainer.vue'
|
||||
import InfiniteLoadMore from '~/components/InfiniteLoadMore.vue'
|
||||
import { toast } from '~/main'
|
||||
import { authState, getToken } from '~/utils/auth'
|
||||
import { stripMarkdownLength } from '~/utils/markdown'
|
||||
@@ -542,11 +535,9 @@ const selectedTab = ref(
|
||||
['all', 'unread', 'control'].includes(route.query.tab) ? route.query.tab : 'unread',
|
||||
)
|
||||
const notificationPrefs = ref([])
|
||||
const ioKey = computed(() => selectedTab.value)
|
||||
const loadFirstPage = async () => {
|
||||
await fetchNotifications({ unread: selectedTab.value === 'unread', reset: true })
|
||||
}
|
||||
const fetchNextPage = async () => fetchNotifications()
|
||||
const filteredNotifications = computed(() =>
|
||||
selectedTab.value === 'all' ? notifications.value : notifications.value.filter((n) => !n.read),
|
||||
)
|
||||
|
||||
const fetchPrefs = async () => {
|
||||
notificationPrefs.value = await fetchNotificationPreferences()
|
||||
@@ -556,7 +547,7 @@ const togglePref = async (pref) => {
|
||||
const ok = await updateNotificationPreference(pref.type, !pref.enabled)
|
||||
if (ok) {
|
||||
pref.enabled = !pref.enabled
|
||||
await fetchNotifications({ unread: selectedTab.value === 'unread', reset: true })
|
||||
await fetchNotifications()
|
||||
await fetchUnreadCount()
|
||||
} else {
|
||||
toast.error('操作失败')
|
||||
@@ -636,12 +627,8 @@ const formatType = (t) => {
|
||||
}
|
||||
}
|
||||
|
||||
watch(selectedTab, (val) => {
|
||||
if (val !== 'control') loadFirstPage()
|
||||
})
|
||||
|
||||
onActivated(() => {
|
||||
if (selectedTab.value !== 'control') loadFirstPage()
|
||||
fetchNotifications()
|
||||
fetchPrefs()
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -118,9 +118,7 @@ export async function updateNotificationPreference(type, enabled) {
|
||||
function createFetchNotifications() {
|
||||
const notifications = ref([])
|
||||
const isLoadingMessage = ref(false)
|
||||
const page = ref(0)
|
||||
const currentUnread = ref(false)
|
||||
const fetchNotifications = async ({ unread = false, reset = false } = {}) => {
|
||||
const fetchNotifications = async () => {
|
||||
const config = useRuntimeConfig()
|
||||
const API_BASE_URL = config.public.apiBaseUrl
|
||||
if (isLoadingMessage && notifications && markRead) {
|
||||
@@ -128,18 +126,11 @@ function createFetchNotifications() {
|
||||
const token = getToken()
|
||||
if (!token) {
|
||||
toast.error('请先登录')
|
||||
return false
|
||||
}
|
||||
if (reset) {
|
||||
notifications.value = []
|
||||
page.value = 0
|
||||
currentUnread.value = unread
|
||||
return
|
||||
}
|
||||
isLoadingMessage.value = true
|
||||
const endpoint = currentUnread.value
|
||||
? `/api/notifications/unread?page=${page.value}`
|
||||
: `/api/notifications?page=${page.value}`
|
||||
const res = await fetch(`${API_BASE_URL}${endpoint}`, {
|
||||
notifications.value = []
|
||||
const res = await fetch(`${API_BASE_URL}/api/notifications`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
@@ -147,7 +138,7 @@ function createFetchNotifications() {
|
||||
isLoadingMessage.value = false
|
||||
if (!res.ok) {
|
||||
toast.error('获取通知失败')
|
||||
return true
|
||||
return
|
||||
}
|
||||
const data = await res.json()
|
||||
|
||||
@@ -293,14 +284,10 @@ function createFetchNotifications() {
|
||||
})
|
||||
}
|
||||
}
|
||||
page.value++
|
||||
return data.length < 50
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
const markRead = async (id) => {
|
||||
@@ -348,6 +335,7 @@ function createFetchNotifications() {
|
||||
markRead,
|
||||
notifications,
|
||||
isLoadingMessage,
|
||||
markRead,
|
||||
markAllRead,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user