Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface DataTableDeleteButtonProps<TData extends { id: number }, TPromise> {
| 'group'
| 'course'
| 'user'
| 'notice'
deleteTarget: (id: number, extra?: number) => Promise<TPromise>
extraArg?: number // 현재는 groupId만 사용되고 있음
getCanDelete?: (selectedRows: TData[]) => Promise<boolean>
Expand Down
33 changes: 33 additions & 0 deletions apps/frontend/app/admin/notice/_components/DeleteNoticeButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { DELETE_NOTICE } from '@/graphql/notice/mutation'
import { useMutation } from '@apollo/client'
import { DataTableDeleteButton } from '../../_components/table/DataTableDeleteButton'
import { useDataTable } from '../../_components/table/context'

interface DeleteNoticeButtonProps {
onSuccess?: () => void
}

export function DeleteNoticeButton({ onSuccess }: DeleteNoticeButtonProps) {
const { table } = useDataTable()
const [deleteNotice] = useMutation(DELETE_NOTICE)

const deleteTarget = (id: number) => {
return deleteNotice({
variables: {
noticeId: id
}
})
}

return (
<DataTableDeleteButton
target="notice"
deleteTarget={deleteTarget}
onSuccess={onSuccess}
className="ml-auto"
deleteItems={table
.getSelectedRowModel()
.rows.map((row) => row.getValue('title'))}
/>
)
}
117 changes: 117 additions & 0 deletions apps/frontend/app/admin/notice/_components/NoticeColumns.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { DataTableColumnHeader } from '@/app/admin/_components/table/DataTableColumnHeader'
import { Checkbox } from '@/components/shadcn/checkbox'
import type { ColumnDef } from '@tanstack/react-table'
import { FaEye, FaEyeSlash } from 'react-icons/fa'
import { PiPushPinFill, PiPushPinSlashBold } from 'react-icons/pi'

export interface Notice {
id: number
title: string
isVisible: boolean
isFixed: boolean
createdBy?: string
createTime?: Date
updateTime: Date
}

export const columns: ColumnDef<Notice>[] = [
{
id: 'select',
header: ({ table }) => (
<Checkbox
onClick={(e) => e.stopPropagation()}
checked={table.getIsAllPageRowsSelected()}
onCheckedChange={(value) =>
table.toggleAllPageRowsSelected(Boolean(value))
}
aria-label="Select all"
className="translate-y-[2px]"
/>
),
cell: ({ row }) => (
<Checkbox
onClick={(e) => e.stopPropagation()}
checked={row.getIsSelected()}
onCheckedChange={(value) => row.toggleSelected(Boolean(value))}
aria-label="Select row"
className="translate-y-[2px] bg-transparent"
/>
),
enableSorting: false,
enableHiding: false
},
{
accessorKey: 'isVisible',
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Visible" />
),
cell: ({ row }) =>
row.getValue('isVisible') ? (
<FaEye className="text-primary" size={18} />
) : (
<FaEyeSlash className="text-gray-400" size={18} />
),
enableSorting: false
},
{
accessorKey: 'isFixed',
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Fixed" />
),
cell: ({ row }) =>
row.getValue('isFixed') ? (
<PiPushPinFill className="text-primary" size={18} />
) : (
<PiPushPinSlashBold className="text-gray-400" size={18} />
),
enableSorting: false
},
{
accessorKey: 'title',
header: ({ column }) => (
<DataTableColumnHeader
column={column}
title="Title"
className="w-[300px]"
/>
),
cell: ({ row }) => {
return (
<div className="flex w-full justify-center">
{row.getValue('title')}
</div>
)
},
enableSorting: false
},
{
accessorKey: 'createdBy',
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Writer" />
),
cell: ({ row }) => row.getValue('createdBy') ?? '-',
enableSorting: false
},
{
accessorKey: 'createTime',
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Create" />
),
cell: ({ getValue }) => {
const createTime = getValue<string>()

return new Date(createTime).toLocaleDateString('ko-KR')
}
},
{
accessorKey: 'updateTime',
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Update" />
),
cell: ({ getValue }) => {
const updateTime = getValue<string>()

return new Date(updateTime).toLocaleString('ko-KR')
}
}
]
69 changes: 69 additions & 0 deletions apps/frontend/app/admin/notice/_components/NoticeDataTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use client'

import {
DataTable,
DataTableFallback,
DataTablePagination,
DataTableRoot,
DataTableSearchBar
} from '@/app/admin/_components/table'
import { GET_NOTICES } from '@/graphql/notice/queries'
import { useApolloClient, useSuspenseQuery } from '@apollo/client'
import { useMemo } from 'react'
import { DeleteNoticeButton } from './DeleteNoticeButton'
import { columns } from './NoticeColumns'

export function NoticeDataTable() {
const client = useApolloClient()

const { data } = useSuspenseQuery(GET_NOTICES, {
variables: { take: 40 }
})
const notices = useMemo(
() =>
data.getNotices.map((notice) => ({
id: Number(notice?.id),
title: notice?.title ?? '-',
isVisible: notice?.isVisible ?? true,
isFixed: notice?.isFixed ?? false,
createdBy: notice?.createdBy?.username,
creteTime: notice?.createTime,
updateTime: notice?.updateTime
})),
[data.getNotices]
)

const onSuccess = () => {
client.refetchQueries({
include: [GET_NOTICES]
})
}

const bodyStyle = {
title: 'justify-start'
}

return (
<div>
<DataTableRoot data={notices} columns={columns}>
<div className="flex justify-between">
<div className="flex gap-2">
<DataTableSearchBar columndId="title" className="rounded-full" />
</div>
<div className="flex gap-2">
<DeleteNoticeButton onSuccess={onSuccess} />
</div>
</div>
<DataTable
bodyStyle={bodyStyle}
getHref={(data) => `/admin/notice/${data.id}`}
/>
<DataTablePagination showSelection />
</DataTableRoot>
</div>
)
}

export function NoticeDataTableFallback() {
return <DataTableFallback withSearchBar={false} columns={columns} />
}
32 changes: 29 additions & 3 deletions apps/frontend/app/admin/notice/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
import { FetchErrorFallback } from '@/components/FetchErrorFallback'
import { Button } from '@/components/shadcn/button'
import { ErrorBoundary } from '@suspensive/react'
import Link from 'next/link'
import { Suspense } from 'react'
import { HiMiniPlusCircle } from 'react-icons/hi2'
import {
NoticeDataTable,
NoticeDataTableFallback
} from './_components/NoticeDataTable'

export const dynamic = 'force-dynamic'

export default function Page() {
return (
<main className="absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 transform">
<p className="font-medium text-slate-400">TODO: Show notice list</p>
</main>
<div className="container mx-auto space-y-5 py-10">
<div className="flex justify-between">
<h1 className="text-4xl font-bold">NOTICE LIST</h1>
<Link href="/admin/notice/create">
<Button size="middle">
<HiMiniPlusCircle className="size-5" />
<span>CREATE</span>
</Button>
</Link>
</div>
<ErrorBoundary fallback={FetchErrorFallback}>
<Suspense fallback={<NoticeDataTableFallback />}>
<NoticeDataTable />
</Suspense>
</ErrorBoundary>
</div>
)
}
10 changes: 9 additions & 1 deletion apps/frontend/graphql/notice/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ const CREATE_NOTICE = gql(`
}
`)

export { CREATE_NOTICE }
const DELETE_NOTICE = gql(`
mutation DeleteNotice($noticeId: Int!) {
deleteNotice(noticeId: $noticeId) {
id
}
}
`)

export { CREATE_NOTICE, DELETE_NOTICE }
18 changes: 17 additions & 1 deletion apps/frontend/graphql/notice/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,20 @@ const GET_NOTICE = gql(`
}
`)

export { GET_NOTICE }
const GET_NOTICES = gql(`
query GetNotices($cursor: Int, $take: Int!) {
getNotices(cursor: $cursor, take: $take) {
id
createdBy {
username
}
title
isVisible
isFixed
createTime
updateTime
}
}
`)

export { GET_NOTICE, GET_NOTICES }
Loading