feat(notifications): add notification system and wire order/dispute events
This commit is contained in:
@@ -6,6 +6,8 @@ import type { Actor } from "@/lib/policy/actor"
|
||||
import type { PolicyDecision } from "@/lib/policy/decision"
|
||||
import { mockDisputes } from "@/lib/mock"
|
||||
import type { Dispute } from "@/lib/types"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useNotificationStore } from "@/store/notifications"
|
||||
import { useOrderStore } from "@/store/orders"
|
||||
|
||||
type DisputeTimelineType = "created" | "response" | "reviewing" | "resolved" | "appealed"
|
||||
@@ -120,6 +122,19 @@ function asRecord(dispute: Dispute): DisputeRecord {
|
||||
}
|
||||
}
|
||||
|
||||
function notifyDispute(orderId: string, title: string, content: string) {
|
||||
if (!useAuthStore.getState().notificationPrefs.order) {
|
||||
return
|
||||
}
|
||||
|
||||
useNotificationStore.getState().addNotification({
|
||||
type: "order",
|
||||
title,
|
||||
content,
|
||||
link: `/dispute/${orderId}`,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDisputeStore = create<DisputeState>((set, get) => {
|
||||
const scheduleProgress = (disputeId: string) => {
|
||||
clearProgressTimers(disputeId)
|
||||
@@ -148,11 +163,14 @@ export const useDisputeStore = create<DisputeState>((set, get) => {
|
||||
}, DISPUTE_TO_REVIEWING_MS)
|
||||
|
||||
const toResolved = setTimeout(() => {
|
||||
let resolvedOrderId: string | null = null
|
||||
set((state) => ({
|
||||
disputes: state.disputes.map((dispute) => {
|
||||
if (dispute.id !== disputeId) return dispute
|
||||
if (dispute.status !== "reviewing" && dispute.status !== "appealed") return dispute
|
||||
|
||||
resolvedOrderId = dispute.orderId
|
||||
|
||||
return {
|
||||
...dispute,
|
||||
status: "resolved",
|
||||
@@ -169,6 +187,11 @@ export const useDisputeStore = create<DisputeState>((set, get) => {
|
||||
}
|
||||
}),
|
||||
}))
|
||||
|
||||
if (resolvedOrderId) {
|
||||
notifyDispute(resolvedOrderId, "争议已处理", "平台已给出争议处理结果")
|
||||
}
|
||||
|
||||
clearProgressTimers(disputeId)
|
||||
}, DISPUTE_TO_RESOLVED_MS)
|
||||
|
||||
@@ -275,6 +298,8 @@ export const useDisputeStore = create<DisputeState>((set, get) => {
|
||||
}),
|
||||
}))
|
||||
|
||||
notifyDispute(dispute.orderId, "争议收到回应", "对方已提交争议回应材料")
|
||||
|
||||
return allow()
|
||||
},
|
||||
submitAppeal: (disputeId, actorId, reason) => {
|
||||
@@ -322,6 +347,8 @@ export const useDisputeStore = create<DisputeState>((set, get) => {
|
||||
}),
|
||||
}))
|
||||
|
||||
notifyDispute(dispute.orderId, "争议已申诉", "申诉已提交,平台将继续复核")
|
||||
|
||||
scheduleProgress(disputeId)
|
||||
return allow()
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user