Merge pull request #124 from nbtca/claude/add-notification-preferences-endpoint-011UuYt1BnaE9yk8Eft7xNQB

Add get notification preferences endpoint
This commit is contained in:
clas 2025-11-23 16:37:38 +08:00 committed by GitHub
commit 435bc86a35
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 493 additions and 182 deletions

View file

@ -0,0 +1,188 @@
import { useEffect, useState } from "react"
import {
Modal,
ModalContent,
ModalHeader,
ModalBody,
ModalFooter,
Switch,
Spinner,
Button,
} from "@heroui/react"
import { saturdayClient } from "../../utils/client"
import type { components } from "../../types/saturday"
type NotificationPreferenceItem = components["schemas"]["NotificationPreferenceItem"]
type Item = components["schemas"]["Item"]
interface NotificationPreferencesProps {
token: string
}
export default function NotificationPreferences({ token }: NotificationPreferencesProps) {
const [isOpen, setIsOpen] = useState(false)
const [preferences, setPreferences] = useState<NotificationPreferenceItem[]>([])
const [isLoading, setIsLoading] = useState(false)
const [isSaving, setIsSaving] = useState(false)
const [errorMessage, setErrorMessage] = useState<string>("")
const [successMessage, setSuccessMessage] = useState<string>("")
const openModal = () => {
setIsOpen(true)
loadPreferences()
}
const closeModal = () => {
setIsOpen(false)
setErrorMessage("")
setSuccessMessage("")
}
const loadPreferences = async () => {
if (!token) return
setIsLoading(true)
setErrorMessage("")
try {
const { data, error } = await saturdayClient.GET("/member/notification-preferences", {
params: {
header: {
Authorization: `Bearer ${token}`,
},
},
})
if (error || !data) {
setErrorMessage("加载通知偏好设置失败")
return
}
setPreferences(data)
} catch (err) {
setErrorMessage("加载通知偏好设置时出错")
} finally {
setIsLoading(false)
}
}
const handleToggle = async (notificationType: string, newValue: boolean) => {
setIsSaving(true)
setErrorMessage("")
setSuccessMessage("")
// Optimistically update UI
const previousPreferences = [...preferences]
setPreferences(preferences.map(pref =>
pref.notificationType === notificationType
? { ...pref, enabled: newValue }
: pref
))
try {
// Create the request body with all preferences as an array of Item objects
const preferencesArray: Item[] = preferences.map(pref => ({
notificationType: pref.notificationType,
enabled: pref.notificationType === notificationType ? newValue : pref.enabled,
}))
const { error } = await saturdayClient.PUT("/member/notification-preferences", {
params: {
header: {
Authorization: `Bearer ${token}`,
},
},
body: {
preferences: preferencesArray,
},
})
if (error) {
// Revert on error
setPreferences(previousPreferences)
setErrorMessage("更新通知偏好设置失败")
return
}
setSuccessMessage("通知偏好设置已更新")
// Clear success message after 3 seconds
setTimeout(() => setSuccessMessage(""), 3000)
} catch (err) {
// Revert on error
setPreferences(previousPreferences)
setErrorMessage("更新通知偏好设置时出错")
} finally {
setIsSaving(false)
}
}
return (
<>
<Button onPress={openModal} color="primary" variant="flat">
</Button>
<Modal isOpen={isOpen} onClose={closeModal} size="2xl">
<ModalContent>
<ModalHeader className="flex flex-col gap-1">
<h3 className="text-xl font-bold"></h3>
<p className="text-sm text-gray-500 font-normal"></p>
</ModalHeader>
<ModalBody>
{isLoading ? (
<div className="flex justify-center items-center py-8">
<Spinner label="加载中..." />
</div>
) : (
<>
<div className="flex flex-col gap-4">
{preferences.map((pref) => (
<div
key={pref.notificationType}
className="flex flex-col sm:flex-row sm:items-center justify-between gap-2 sm:gap-4 py-3 border-b border-gray-100 last:border-0"
>
<div className="flex-1">
<p className="text-sm sm:text-base font-medium">{pref.description}</p>
<p className="text-xs text-gray-400 mt-1">{pref.notificationType}</p>
</div>
<Switch
isSelected={pref.enabled}
onValueChange={(value) => handleToggle(pref.notificationType, value)}
isDisabled={isSaving}
size="sm"
/>
</div>
))}
{preferences.length === 0 && !errorMessage && (
<div className="text-center py-4 text-gray-500">
<p className="text-sm"></p>
</div>
)}
</div>
{/* Messages */}
{errorMessage && (
<div className="bg-red-50 border border-red-200 rounded-lg p-3 mt-2">
<p className="text-sm text-red-800">{errorMessage}</p>
</div>
)}
{successMessage && (
<div className="bg-green-50 border border-green-200 rounded-lg p-3 mt-2">
<p className="text-sm text-green-800">{successMessage}</p>
</div>
)}
</>
)}
</ModalBody>
<ModalFooter>
<Button variant="flat" onPress={closeModal}>
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
)
}

View file

@ -34,6 +34,7 @@ import type { PublicMember } from "../../store/member"
import type { UserInfoResponse } from "@logto/browser"
import { getAvailableEventActions, type EventAction, type IdentityContext } from "./EventAction"
import { ExportExcelModal } from "./ExportEventDialog"
import NotificationPreferences from "./NotificationPreferences"
type PublicEvent = components["schemas"]["PublicEvent"]
@ -525,11 +526,12 @@ export default function App() {
<section className="box-border max-w-full px-4 sm:px-6 lg:max-w-[1024px] lg:px-[22px] mx-auto mb-16 sm:mb-24">
<div className="mt-6 flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
<div className="text-xl sm:text-2xl font-bold"></div>
{
userInfo?.roles?.find(v => v.toLowerCase() == "repair admin")
? <div className="w-full sm:w-auto"><ExportExcelModal></ExportExcelModal></div>
: <></>
}
<div className="flex gap-2 w-full sm:w-auto">
{token && <NotificationPreferences token={token} />}
{userInfo?.roles?.find(v => v.toLowerCase() == "repair admin") && (
<ExportExcelModal />
)}
</div>
</div>
<div className="my-8 flex flex-col gap-4">
{/* Mobile Cards Layout */}

View file

@ -298,6 +298,24 @@ export interface paths {
patch: operations["alter-commit-event"]
trace?: never
}
"/member/notification-preferences": {
parameters: {
query?: never
header?: never
path?: never
cookie?: never
}
/** Get member notification preferences */
get: operations["get-notification-preferences"]
/** Update member notification preferences */
put: operations["update-notification-preferences"]
post?: never
delete?: never
options?: never
head?: never
patch?: never
trace?: never
}
"/member/token/logto": {
parameters: {
query?: never
@ -547,6 +565,7 @@ export interface components {
logtoId: string
memberId: string
name: string
notificationPreferences: components["schemas"]["NotificationPreferences"]
phone: string
profile: string
qq: string
@ -637,6 +656,10 @@ export interface components {
logId: number
memberId: string
}
"Item": {
enabled: boolean
notificationType: string
}
"Member": {
/**
* Format: uri
@ -653,12 +676,23 @@ export interface components {
logtoId: string
memberId: string
name: string
notificationPreferences: components["schemas"]["NotificationPreferences"]
phone: string
profile: string
qq: string
role: string
section: string
}
"NotificationPreferenceItem": {
description: string
enabled: boolean
notificationType: string
}
"NotificationPreferences": {
event_assigned_to_me: boolean
event_status_changed: boolean
new_event_created: boolean
}
"NullInt64": {
/** Format: int64 */
Int64: number
@ -769,6 +803,15 @@ export interface components {
profile: string
qq: string
}
"UpdateNotificationPreferencesInputBody": {
/**
* Format: uri
* @description A URL to the JSON Schema for this object.
* @example https://api.nbtca.space/schemas/UpdateNotificationPreferencesInputBody.json
*/
readonly $schema?: string
preferences: components["schemas"]["Item"][] | null
}
}
responses: never
parameters: never
@ -1714,6 +1757,84 @@ export interface operations {
}
}
}
"get-notification-preferences": {
parameters: {
query?: never
header?: {
/** @description Bearer token or JWT token */
Authorization?: string
}
path?: never
cookie?: never
}
requestBody?: never
responses: {
/** @description OK */
200: {
headers: {
"X-Limit"?: number | null
"X-Offset"?: number | null
"X-Page"?: number | null
"X-Total-Count"?: number | null
"X-Total-Pages"?: number | null
[name: string]: unknown
}
content: {
"application/json": components["schemas"]["NotificationPreferenceItem"][] | null
}
}
/** @description Error */
default: {
headers: {
[name: string]: unknown
}
content: {
"application/problem+json": components["schemas"]["ErrorModel"]
}
}
}
}
"update-notification-preferences": {
parameters: {
query?: never
header?: {
/** @description Bearer token or JWT token */
Authorization?: string
}
path?: never
cookie?: never
}
requestBody: {
content: {
"application/json": components["schemas"]["UpdateNotificationPreferencesInputBody"]
}
}
responses: {
/** @description OK */
200: {
headers: {
"X-Limit"?: number | null
"X-Offset"?: number | null
"X-Page"?: number | null
"X-Total-Count"?: number | null
"X-Total-Pages"?: number | null
[name: string]: unknown
}
content: {
"application/json": string
}
}
/** @description Error */
default: {
headers: {
[name: string]: unknown
}
content: {
"application/problem+json": components["schemas"]["ErrorModel"]
}
}
}
}
"create-token-via-logto-token": {
parameters: {
query?: never