fe480ff131
- Removed redundant state management and effects from DashboardQuickRepliesPage. - Integrated DashboardCrudPage for handling CRUD operations and filtering. - Created a new DashboardCrudPage component to encapsulate common CRUD logic. - Added utility functions for building and normalizing dashboard CRUD queries. - Implemented tests for the new utility functions to ensure correctness.
197 lines
6.3 KiB
TypeScript
197 lines
6.3 KiB
TypeScript
"use client"
|
|
|
|
import { FileTextIcon, RefreshCwIcon } from "lucide-react"
|
|
import { toast } from "sonner"
|
|
|
|
import { DashboardCrudPage } from "@/components/dashboard/crud"
|
|
import { Badge } from "@/components/ui/badge"
|
|
import { DropdownMenuItem } from "@/components/ui/dropdown-menu"
|
|
import {
|
|
createQuickReply,
|
|
deleteQuickReply,
|
|
fetchQuickReplies,
|
|
updateQuickReply,
|
|
type AdminQuickReply,
|
|
type CreateAdminQuickReplyPayload,
|
|
} from "@/lib/api/admin"
|
|
import { getEnumOptions } from "@/lib/enums"
|
|
import { Status, StatusLabels } from "@/lib/generated/enums"
|
|
import { useI18n } from "@/i18n/provider"
|
|
import { EditDialog } from "./_components/edit"
|
|
|
|
function getStatusLabel(status: Status, t: (key: string) => string) {
|
|
if (status === Status.Disabled) {
|
|
return t("status.disabled")
|
|
}
|
|
if (status === Status.Deleted) {
|
|
return t("status.deleted")
|
|
}
|
|
return t("status.ok")
|
|
}
|
|
|
|
export default function DashboardQuickRepliesPage() {
|
|
const t = useI18n()
|
|
const listStatusOptions = [
|
|
{ value: "all", label: t("status.all") },
|
|
...getEnumOptions(StatusLabels)
|
|
.filter((item) => Number(item.value) !== Status.Deleted)
|
|
.map((item) => ({
|
|
value: String(item.value),
|
|
label: getStatusLabel(item.value as Status, t),
|
|
})),
|
|
]
|
|
|
|
return (
|
|
<DashboardCrudPage<AdminQuickReply, CreateAdminQuickReplyPayload>
|
|
filters={[
|
|
{
|
|
name: "title",
|
|
label: t("quickReply.filterTitle"),
|
|
placeholder: t("quickReply.filterTitle"),
|
|
defaultValue: "",
|
|
trim: true,
|
|
className: "w-full sm:w-72",
|
|
},
|
|
{
|
|
name: "groupName",
|
|
label: t("quickReply.filterGroup"),
|
|
placeholder: t("quickReply.filterGroup"),
|
|
defaultValue: "",
|
|
trim: true,
|
|
className: "w-full sm:w-44",
|
|
},
|
|
{
|
|
name: "status",
|
|
label: t("status.all"),
|
|
type: "select",
|
|
defaultValue: "all",
|
|
allValue: "all",
|
|
options: listStatusOptions,
|
|
className: "w-full sm:w-36",
|
|
},
|
|
]}
|
|
columns={[
|
|
{
|
|
key: "quickReply",
|
|
label: t("quickReply.columnQuickReply"),
|
|
render: (item) => (
|
|
<div className="flex items-start gap-3">
|
|
<div className="mt-0.5 flex size-8 items-center justify-center rounded-md bg-muted text-muted-foreground">
|
|
<FileTextIcon className="size-4" />
|
|
</div>
|
|
<div className="min-w-0">
|
|
<div className="font-medium">{item.title}</div>
|
|
<div className="mt-1 line-clamp-2 text-sm text-muted-foreground">
|
|
{item.content}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
),
|
|
},
|
|
{
|
|
key: "groupName",
|
|
label: t("quickReply.columnGroup"),
|
|
render: (item) => <Badge variant="outline">{item.groupName}</Badge>,
|
|
},
|
|
{
|
|
key: "status",
|
|
label: t("quickReply.columnStatus"),
|
|
render: (item) => (
|
|
<Badge variant={item.status === Status.Ok ? "default" : "outline"}>
|
|
{getStatusLabel(item.status as Status, t)}
|
|
</Badge>
|
|
),
|
|
},
|
|
{
|
|
key: "sortNo",
|
|
label: t("quickReply.columnSort"),
|
|
render: (item) => item.sortNo,
|
|
},
|
|
{
|
|
key: "createdBy",
|
|
label: t("quickReply.columnCreator"),
|
|
render: (item) => item.createdBy || "-",
|
|
},
|
|
]}
|
|
fetchList={fetchQuickReplies}
|
|
getItemId={(item) => item.id}
|
|
createItem={createQuickReply}
|
|
updateItem={(item, payload) => updateQuickReply({ id: item.id, ...payload })}
|
|
deleteItem={(item) => deleteQuickReply(item.id)}
|
|
renderRowActions={({ item, actionLoading, reload, setActionLoadingId }) => (
|
|
<DropdownMenuItem
|
|
onClick={() => {
|
|
void (async () => {
|
|
setActionLoadingId(item.id)
|
|
try {
|
|
const nextStatus =
|
|
item.status === Status.Ok ? Status.Disabled : Status.Ok
|
|
await updateQuickReply({
|
|
id: item.id,
|
|
groupName: item.groupName,
|
|
title: item.title,
|
|
content: item.content,
|
|
sortNo: item.sortNo,
|
|
status: nextStatus,
|
|
})
|
|
toast.success(
|
|
t(
|
|
nextStatus === Status.Ok
|
|
? "quickReply.enabled"
|
|
: "quickReply.disabled",
|
|
{ title: item.title }
|
|
)
|
|
)
|
|
await reload()
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error
|
|
? error.message
|
|
: t("quickReply.statusUpdateFailed")
|
|
)
|
|
} finally {
|
|
setActionLoadingId(null)
|
|
}
|
|
})()
|
|
}}
|
|
>
|
|
<RefreshCwIcon />
|
|
{actionLoading
|
|
? t("quickReply.processing")
|
|
: item.status === Status.Ok
|
|
? t("quickReply.disable")
|
|
: t("quickReply.enable")}
|
|
</DropdownMenuItem>
|
|
)}
|
|
renderEditDialog={({ open, saving, itemId, onOpenChange, onSubmit }) => (
|
|
<EditDialog
|
|
open={open}
|
|
saving={saving}
|
|
itemId={itemId}
|
|
onOpenChange={onOpenChange}
|
|
onSubmit={onSubmit}
|
|
/>
|
|
)}
|
|
labels={{
|
|
refresh: t("quickReply.refresh"),
|
|
create: t("quickReply.new"),
|
|
query: t("quickReply.query"),
|
|
loading: t("quickReply.loading"),
|
|
empty: t("quickReply.empty"),
|
|
actions: t("quickReply.columnActions"),
|
|
edit: t("quickReply.edit"),
|
|
delete: t("quickReply.delete"),
|
|
processing: t("quickReply.processing"),
|
|
moreActions: (item) =>
|
|
t("quickReply.moreActions", { title: item.title }),
|
|
loadFailed: t("quickReply.loadFailed"),
|
|
saveFailed: t("quickReply.saveFailed"),
|
|
deleteFailed: t("quickReply.deleteFailed"),
|
|
created: (payload) => t("quickReply.created", { title: payload.title }),
|
|
updated: (item) => t("quickReply.updated", { title: item.title }),
|
|
deleted: (item) => t("quickReply.deleted", { title: item.title }),
|
|
}}
|
|
/>
|
|
)
|
|
}
|