143 lines
4.3 KiB
TypeScript
143 lines
4.3 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { getConfig } from "@common/config";
|
|
import { ENDPOINT } from "../../config";
|
|
import { cn, getCookie } from "../../lib";
|
|
import {
|
|
getNoticeUserList,
|
|
getUnreadNoticeCount,
|
|
readNotice,
|
|
} from "../../api/notice-user";
|
|
import { Label } from "../label";
|
|
import { Badge } from "../badge";
|
|
import { Button } from "../button";
|
|
import { Checkbox } from "../checkbox";
|
|
import { Popover, PopoverContent, PopoverTrigger } from "../popover";
|
|
|
|
import dayjs from "dayjs";
|
|
import relativeTime from "dayjs/plugin/relativeTime";
|
|
import "dayjs/locale/ko";
|
|
|
|
dayjs.extend(relativeTime);
|
|
dayjs.locale("ko");
|
|
|
|
const appConfig = getConfig();
|
|
const userId = getCookie(appConfig.AUTH_USER_ID ?? "");
|
|
|
|
export function NoticePopover() {
|
|
const queryClient = useQueryClient();
|
|
|
|
const [open, setOpen] = useState(false);
|
|
const [selectedNoticeIds, setSelectedNoticeIds] = useState<number[]>([]);
|
|
|
|
const { data: noticeUserList } = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.NOTICE_USER],
|
|
queryFn: () => getNoticeUserList(userId!),
|
|
enabled: !!userId,
|
|
select: (data) => data.content.filter((item) => item.notReadYn === "N"),
|
|
});
|
|
|
|
const { data: unreadNoticeCount } = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.NOTICE],
|
|
queryFn: () => getUnreadNoticeCount(userId!),
|
|
enabled: !!userId,
|
|
});
|
|
|
|
const handleSelectNotice = (notiId: number) => {
|
|
setSelectedNoticeIds((prevIds) => {
|
|
if (prevIds.includes(notiId)) {
|
|
return prevIds.filter((id) => id !== notiId);
|
|
} else {
|
|
return [...prevIds, notiId];
|
|
}
|
|
});
|
|
};
|
|
|
|
const handleSelectAll = () => {
|
|
if (noticeUserList) {
|
|
if (selectedNoticeIds.length === noticeUserList.length) {
|
|
setSelectedNoticeIds([]);
|
|
} else {
|
|
const allIds = noticeUserList.map((item) => item.notiId);
|
|
setSelectedNoticeIds(allIds);
|
|
}
|
|
}
|
|
};
|
|
|
|
const handleReadNotice = async () => {
|
|
if (selectedNoticeIds.length === 0) return;
|
|
try {
|
|
await Promise.all(selectedNoticeIds.map((notiId) => readNotice(notiId)));
|
|
await queryClient.invalidateQueries({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.NOTICE_USER],
|
|
});
|
|
await queryClient.invalidateQueries({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.NOTICE],
|
|
});
|
|
setSelectedNoticeIds([]);
|
|
} catch (error) {
|
|
console.error("알림 읽음 처리 중 오류 발생:", error);
|
|
}
|
|
};
|
|
|
|
const isAllSelected = noticeUserList
|
|
? selectedNoticeIds.length === noticeUserList.length &&
|
|
noticeUserList.length > 0
|
|
: false;
|
|
|
|
useEffect(() => {
|
|
if (open) setSelectedNoticeIds([]);
|
|
}, [open]);
|
|
|
|
return (
|
|
<Popover open={open} onOpenChange={setOpen}>
|
|
<PopoverTrigger asChild>
|
|
<div className="flex w-full items-center gap-2 text-left">
|
|
알림 <Badge>{unreadNoticeCount ?? 0}</Badge>
|
|
</div>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="w-[32rem] border-border px-0">
|
|
<div className="flex items-center gap-4 p-4 pt-0">
|
|
<Checkbox
|
|
id="all-select"
|
|
checked={isAllSelected}
|
|
onCheckedChange={handleSelectAll}
|
|
/>
|
|
<Label htmlFor="all-select" className="text-[1.4rem]">
|
|
전체 선택
|
|
</Label>
|
|
</div>
|
|
<ul>
|
|
{noticeUserList?.map((item) => (
|
|
<li
|
|
key={item.notiId}
|
|
className={cn(
|
|
"flex items-center gap-4 border-t border-border p-4",
|
|
item.notReadYn === "N" && "bg-primary-transparent",
|
|
)}
|
|
>
|
|
<Checkbox
|
|
checked={selectedNoticeIds.includes(item.notiId)}
|
|
onCheckedChange={() => handleSelectNotice(item.notiId)}
|
|
/>
|
|
<div>
|
|
<span className="text-[1.2rem]">{item.notiType}</span>
|
|
<p className="text-[1.4rem]">{item.notiTtl}</p>
|
|
<span className="text-[1.2rem] opacity-50">
|
|
{dayjs(item.regDt).fromNow()}
|
|
</span>
|
|
</div>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
<div
|
|
className="border-t border-border pt-4 text-center"
|
|
onClick={() => setOpen(false)}
|
|
>
|
|
<Button onClick={handleReadNotice}>확인</Button>
|
|
</div>
|
|
</PopoverContent>
|
|
</Popover>
|
|
);
|
|
}
|