Skip to content

Commit

Permalink
✨ Add notification types and fetchers
Browse files Browse the repository at this point in the history
  • Loading branch information
injoonH committed May 17, 2024
1 parent 84b0f44 commit b286d0a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/lib/api/notification.ts
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/`);
};
25 changes: 25 additions & 0 deletions src/lib/queries/notification.ts
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] });
},
});
};
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./board";
export * from "./notification";
export * from "./pagination";
40 changes: 40 additions & 0 deletions src/types/notification.ts
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>;

0 comments on commit b286d0a

Please sign in to comment.