-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add notification types and fetchers
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { client } from "@/lib/axios"; | ||
import { notificationSchema, paginationSchema } from "@/types"; | ||
|
||
export type GetNotificationsOptions = { | ||
page?: number; | ||
unread?: boolean; | ||
}; | ||
|
||
export const getNotifications = async (options?: GetNotificationsOptions) => { | ||
const notifications = ( | ||
await client.get("notifications/", { | ||
params: { | ||
page: options?.page, | ||
is_read: options?.unread === undefined ? undefined : !options.unread, | ||
}, | ||
}) | ||
).data; | ||
return paginationSchema(notificationSchema).parse(notifications); | ||
}; | ||
|
||
export const readNotification = async (id: number) => { | ||
await client.post(`notifications/${id}/read/`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./board"; | ||
export * from "./notification"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; | ||
|
||
import { | ||
type GetNotificationsOptions, | ||
getNotifications, | ||
readNotification, | ||
} from "@/lib/api/notification"; | ||
|
||
const NOTIFICATION_QUERY_KEY = "notifications"; | ||
|
||
export const useNotifications = (options?: GetNotificationsOptions) => | ||
useQuery({ | ||
queryKey: [NOTIFICATION_QUERY_KEY, options], | ||
queryFn: () => getNotifications(options), | ||
}); | ||
|
||
export const useReadNotification = () => { | ||
const queryClient = useQueryClient(); | ||
return useMutation({ | ||
mutationFn: readNotification, | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: [NOTIFICATION_QUERY_KEY] }); | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from "./board"; | ||
export * from "./notification"; | ||
export * from "./pagination"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { z } from "zod"; | ||
|
||
import { NameType } from "@/constants/enum"; | ||
|
||
const baseSchema = z.object({ | ||
id: z.number(), | ||
createdAt: z.coerce.date(), | ||
title: z.string(), | ||
content: z.string(), | ||
isRead: z.boolean(), | ||
relatedArticle: z.object({ | ||
id: z.number(), | ||
createdAt: z.coerce.date(), | ||
createdBy: z.number(), | ||
title: z.string(), | ||
nameType: z.nativeEnum(NameType), | ||
parentBoard: z.number(), | ||
parentTopic: z.number().nullable(), | ||
}), | ||
}); | ||
const commentNotificationSchema = baseSchema | ||
.extend({ | ||
type: z.literal("article_commented"), | ||
relatedComment: z.null(), | ||
}) | ||
.transform(({ type, ...data }) => ({ type: "comment" as const, ...data })); | ||
const replyNotificationSchema = baseSchema | ||
.extend({ | ||
type: z.literal("comment_commented"), | ||
relatedComment: z.object({ | ||
id: z.number(), | ||
createdAt: z.coerce.date(), | ||
createdBy: z.number(), | ||
content: z.string(), | ||
nameType: z.nativeEnum(NameType), | ||
}), | ||
}) | ||
.transform(({ type, ...data }) => ({ type: "reply" as const, ...data })); | ||
export const notificationSchema = z.union([commentNotificationSchema, replyNotificationSchema]); | ||
export type Notification = z.infer<typeof notificationSchema>; |