136 lines
5.7 KiB
TypeScript
136 lines
5.7 KiB
TypeScript
"use client";
|
|
|
|
import { useMemo, useState } from "react";
|
|
import type { ColumnDef, PaginationState, SortingState } from "@tanstack/react-table";
|
|
import { AppPageTitle } from "@/components/app-shell/AppPageTitle";
|
|
import { ContentSearchBar } from "@/components/common/ContentSearchBar";
|
|
import { CenterTargetCell } from "@/components/common/CenterTargetCell";
|
|
import { DataTable } from "@/components/data-table";
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import {Card, CardContent, CardHeader} from "@/components/ui/card";
|
|
import { useAuth } from "@/hooks/useAuth";
|
|
import { useDialog } from "@/hooks/useDialog";
|
|
import { useNoticeSearchQuery } from "@/query/notice";
|
|
import { useCentersQuery } from "@/query/center";
|
|
import { isAdminRole } from "@/types/auth";
|
|
import type { Notice } from "@/types/notice";
|
|
import { NoticeFormDialog } from "./NoticeFormDialog";
|
|
import { NoticeViewDialog } from "./NoticeViewDialog";
|
|
|
|
export default function NoticesPage() {
|
|
const { auth } = useAuth();
|
|
const admin = isAdminRole(auth.user?.userRole);
|
|
const { showDialog } = useDialog();
|
|
const centersQuery = useCentersQuery();
|
|
|
|
const [pagination, setPagination] = useState<PaginationState>({ pageIndex: 0, pageSize: 10 });
|
|
const [sorting, setSorting] = useState<SortingState>([{ id: "regDt", desc: true }]);
|
|
const [keywordInput, setKeywordInput] = useState("");
|
|
const [searchKeyword, setSearchKeyword] = useState("");
|
|
const [targetCenterCds, setTargetCenterCds] = useState<string[]>([]);
|
|
const [selectedNoticeId, setSelectedNoticeId] = useState<number | null>(null);
|
|
const [editingNoticeId, setEditingNoticeId] = useState<number | null>(null);
|
|
|
|
const searchQuery = useNoticeSearchQuery({
|
|
pageIndex: pagination.pageIndex,
|
|
pageSize: pagination.pageSize,
|
|
sortBy: sorting[0]?.id ?? "regDt",
|
|
sortDir: sorting[0]?.desc === false ? "ASC" : "DESC",
|
|
searchKeyword: searchKeyword.trim() || undefined,
|
|
targetCenterCds: targetCenterCds.length ? targetCenterCds.map(Number) : undefined,
|
|
});
|
|
|
|
const totalElements = searchQuery.data?.totalElements ?? 0;
|
|
const centerOptions = useMemo(
|
|
() => (centersQuery.data ?? []).filter((center) => center.centerCd !== 0).map((center) => ({ label: center.centerName, value: String(center.centerCd) })),
|
|
[centersQuery.data],
|
|
);
|
|
|
|
const columns = useMemo<ColumnDef<Notice>[]>(() => {
|
|
const base: ColumnDef<Notice>[] = [
|
|
{
|
|
id: "noticeId",
|
|
header: "No.",
|
|
accessorFn: (row) => row.noticeId,
|
|
size: 70,
|
|
cell: ({ row }) => totalElements - pagination.pageIndex * pagination.pageSize - row.index,
|
|
},
|
|
{
|
|
accessorKey: "title",
|
|
header: "공지 제목",
|
|
cell: ({ row }) => (
|
|
<span className="flex items-center gap-2">
|
|
<span className="truncate">{row.original.title}</span>
|
|
{!admin && !row.original.viewed ? <Badge>N</Badge> : null}
|
|
</span>
|
|
),
|
|
},
|
|
];
|
|
|
|
if (admin) {
|
|
base.push({
|
|
id: "target",
|
|
header: "공지 대상",
|
|
size: 120,
|
|
cell: ({ row }) => <CenterTargetCell centerCds={row.original.centerCds} centers={centersQuery.data ?? []} />,
|
|
});
|
|
}
|
|
|
|
base.push(
|
|
{ accessorKey: "regUser", header: "게시자", size: 100, cell: ({ row }) => row.original.regUserName ?? row.original.regUser },
|
|
{ accessorKey: "regDt", header: "게시일", size: 110, cell: ({ row }) => row.original.regDt?.slice(0, 10) },
|
|
);
|
|
|
|
return base;
|
|
}, [admin, totalElements, pagination.pageIndex, pagination.pageSize, centersQuery.data]);
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<AppPageTitle title="공지사항" description="세종학당재단 공지사항입니다." />
|
|
<Card>
|
|
<CardHeader className="flex flex-col gap-3 sm:flex-row sm:items-end sm:justify-between">
|
|
<div className="min-w-0 flex-1">
|
|
<ContentSearchBar
|
|
keyword={keywordInput}
|
|
onKeywordChange={setKeywordInput}
|
|
onSubmit={() => { setSearchKeyword(keywordInput); setPagination((current) => ({ ...current, pageIndex: 0 })); }}
|
|
targetCenterCds={targetCenterCds}
|
|
onTargetCenterCdsChange={(values) => { setTargetCenterCds(values); setPagination((current) => ({ ...current, pageIndex: 0 })); }}
|
|
centerOptions={centerOptions}
|
|
showTargetFilter={admin}
|
|
placeholder="No., 제목, 게시자 검색"
|
|
/>
|
|
</div>
|
|
{admin ? (
|
|
<Button type="button" onClick={() => { setEditingNoticeId(null); showDialog("noticeForm"); }}>등록</Button>
|
|
) : null}
|
|
</CardHeader>
|
|
<CardContent className="px-4 bp-4">
|
|
<DataTable
|
|
columns={columns}
|
|
data={searchQuery.data?.content ?? []}
|
|
getRowId={(row) => String(row.noticeId)}
|
|
isClient={false}
|
|
pagination={pagination}
|
|
onPaginationChange={setPagination}
|
|
onSortingChange={setSorting}
|
|
pageCount={searchQuery.data?.totalPages ?? 0}
|
|
totalCount={totalElements}
|
|
selectMode="click-event"
|
|
onRowClick={(row) => { setSelectedNoticeId(row.noticeId); showDialog("noticeView"); }}
|
|
emptyText="등록된 공지사항이 없습니다."
|
|
/>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<NoticeViewDialog
|
|
noticeId={selectedNoticeId}
|
|
onEdit={(noticeId) => { setEditingNoticeId(noticeId); showDialog("noticeForm"); }}
|
|
onDeleted={() => searchQuery.refetch()}
|
|
/>
|
|
<NoticeFormDialog noticeId={editingNoticeId} onSaved={() => searchQuery.refetch()} />
|
|
</div>
|
|
);
|
|
}
|