Files
frontend/apps/cs/src/pages/schedule/ui/schedule-form.tsx
T

358 lines
13 KiB
TypeScript

import dayjs from "dayjs";
import { useState } from "react";
import dynamic from "next/dynamic";
import type { Path, PathValue, UseFormReturn } from "react-hook-form";
import {
Checkbox,
DatePicker,
Input,
Label,
RadioGroup,
RadioGroupItem,
Table,
TableBody,
TableCell,
TableHead,
TableRow,
} from "@common/components";
import { GeneralFileUploader } from "@/widgets/post";
import { FormWrapper } from "@/widgets/form-wrapper";
import { CodeSelect } from "@/entities/code";
import { FormItem, FormControl, FormField } from "@/shared/ui/form";
import {
type ScheduleFormData,
type ScheduleUpdateFormData,
} from "../model/schedule-interface";
import { DateTimePicker } from "./date-time-picker";
const Editor = dynamic(
() => import("@/entities/editor").then((module) => module.Editor),
{ ssr: false },
);
type ScheduleFormProps<T extends ScheduleFormData | ScheduleUpdateFormData> = {
form: UseFormReturn<T>;
scheduleId?: string;
handleSave: (data: T) => Promise<void>;
handleDelete?: () => Promise<void>;
};
function ScheduleForm<T extends ScheduleFormData | ScheduleUpdateFormData>({
form,
scheduleId,
...props
}: ScheduleFormProps<T>) {
const isModifyMode = !!scheduleId;
const [isAllDay, setIsAllDay] = useState(false);
const [deleteAttachmentIds, setDeleteAttachmentIds] = useState<string[]>([]);
const handleDateTimeChange = (name: Path<T>) => (date?: Date) => {
if (!date) return;
form.setValue(
name,
dayjs(date).format("YYYY-MM-DD HH:mm:ss") as PathValue<T, Path<T>>,
);
setIsAllDay(false);
};
return (
<FormWrapper form={form} isModifyMode={isModifyMode} {...props}>
<div className="card">
<Table variant="secondary">
<colgroup>
<col width="15%" />
<col width="85%" />
</colgroup>
<TableBody>
<TableRow>
<TableHead>제목</TableHead>
<TableCell className="border">
<FormField
control={form.control}
name={"schdlTtl" as Path<T>}
render={({ field }) => (
<FormItem>
<FormControl>
<Input
{...field}
value={(field.value as string) ?? ""}
className="!text-[1.3rem]"
placeholder="일정 제목을 입력하십시오."
/>
</FormControl>
</FormItem>
)}
/>
</TableCell>
</TableRow>
<TableRow>
<TableHead>일정 기간</TableHead>
<TableCell className="border">
<div className="flex items-center gap-4">
<div className="flex items-center gap-2">
<FormField
control={form.control}
name={"schdlBgngDt" as Path<T>}
render={({ field }) => (
<DateTimePicker
date={
field.value
? dayjs(field.value as string).toDate()
: undefined
}
classes={{ trigger: "!w-auto px-4 justify-start" }}
onDateChange={handleDateTimeChange(
"schdlBgngDt" as Path<T>,
)}
{...field}
/>
)}
/>
~
<FormField
control={form.control}
name={"schdlEndDt" as Path<T>}
render={({ field }) => (
<DateTimePicker
date={
field.value
? dayjs(field.value as string).toDate()
: undefined
}
classes={{ trigger: "!w-auto px-4 justify-start" }}
onDateChange={handleDateTimeChange(
"schdlEndDt" as Path<T>,
)}
{...field}
/>
)}
/>
</div>
<div className="flex items-center gap-2">
<Checkbox
id="allDay"
checked={isAllDay}
onCheckedChange={(checked) => {
setIsAllDay(checked === true);
if (checked === true) {
const schdlBgngDt = form.getValues(
"schdlBgngDt" as Path<T>,
);
const schdlEndDt = form.getValues(
"schdlEndDt" as Path<T>,
);
const allSchdlBgngDt = dayjs(
schdlBgngDt as string,
).format("YYYY-MM-DD 00:00:00");
const allSchdlEndDt = dayjs(
schdlEndDt as string,
).format("YYYY-MM-DD 23:59:59");
form.setValue(
"schdlBgngDt" as Path<T>,
allSchdlBgngDt as PathValue<T, Path<T>>,
);
form.setValue(
"schdlEndDt" as Path<T>,
allSchdlEndDt as PathValue<T, Path<T>>,
);
}
}}
/>
<Label htmlFor="allDay">종일</Label>
</div>
</div>
</TableCell>
</TableRow>
<TableRow>
<TableHead>일정 반복</TableHead>
<TableCell className="border">
<div className="flex items-center gap-4">
<div className="flex items-center gap-2">
<FormField
control={form.control}
name={"reptYn" as Path<T>}
render={({ field }) => (
<FormItem>
<FormControl>
<div className="mr-2 flex items-center gap-2">
<Checkbox
id="reptY"
checked={field.value === "Y"}
onCheckedChange={(checked) => {
field.onChange(checked ? "Y" : "N");
}}
/>
<Label htmlFor="reptY">반복</Label>
</div>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name={"reptDto.reptCycleCd" as Path<T>}
render={({ field }) => (
<FormItem>
<FormControl>
<CodeSelect
{...field}
upCd="SCHDLRPTCD"
value={(field.value as string) ?? ""}
onValueChange={field.onChange}
placeholder="반복 주기 선택"
disabled={form.watch("reptYn" as Path<T>) === "N"}
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name={"reptDto.reptEndDt" as Path<T>}
render={({ field }) => (
<FormItem>
<FormControl>
<DatePicker
mode="single"
selected={
field.value
? dayjs(field.value as string).toDate()
: undefined
}
onSelect={(value) => {
if (!value) return;
field.onChange(value);
form.setValue(
"reptDto.reptEndDt" as Path<T>,
dayjs(value).format(
"YYYY-MM-DD 23:59:59",
) as PathValue<T, Path<T>>,
);
}}
className="!w-auto px-4"
placeholder="반복 종료일 선택"
disabled={form.watch("reptYn" as Path<T>) === "N"}
/>
</FormControl>
</FormItem>
)}
/>
</div>
</div>
</TableCell>
</TableRow>
<TableRow>
<TableHead>일정 유형</TableHead>
<TableCell className="border">
<FormField
control={form.control}
name={"schdlSeCd" as Path<T>}
render={({ field }) => (
<FormItem>
<FormControl>
<CodeSelect
{...field}
upCd="SCHDLCLCD"
value={(field.value as string) ?? ""}
onValueChange={field.onChange}
/>
</FormControl>
</FormItem>
)}
/>
</TableCell>
</TableRow>
<TableRow>
<TableHead>일정 내용</TableHead>
<TableCell className="border">
<FormField
control={form.control}
name={"schdlCn" as Path<T>}
render={({ field }) => (
<FormItem>
<FormControl>
<Editor
value={(field.value as string) ?? ""}
onEditorChange={field.onChange}
/>
</FormControl>
</FormItem>
)}
/>
</TableCell>
</TableRow>
<TableRow>
<TableHead>대상 사이트</TableHead>
<TableCell className="border">
<FormField
control={form.control}
name={"commSchdlYn" as Path<T>}
render={({ field }) => (
<FormItem>
<FormControl>
<div className="flex items-center gap-2">
<Checkbox
id="commSchdlY"
checked={field.value === "Y"}
onCheckedChange={(checked) => {
field.onChange(checked ? "Y" : "N");
}}
/>
<Label htmlFor="commSchdlY">공용</Label>
</div>
</FormControl>
</FormItem>
)}
/>
</TableCell>
</TableRow>
<TableRow>
<TableHead>첨부 파일</TableHead>
<TableCell className="border">
<GeneralFileUploader
form={form}
deleteAttachmentIds={deleteAttachmentIds}
setDeleteAttachmentIds={setDeleteAttachmentIds}
/>
</TableCell>
</TableRow>
<TableRow>
<TableHead>사용 여부</TableHead>
<TableCell className="border">
<FormField
control={form.control}
name={"useYn" as Path<T>}
render={({ field }) => (
<FormItem>
<FormControl>
<RadioGroup
{...field}
value={(field.value as string) ?? ""}
onValueChange={field.onChange}
className="flex items-center gap-4"
>
<div className="flex items-center gap-2">
<RadioGroupItem value="Y" id="useY" />
<Label htmlFor="useY">사용</Label>
</div>
<div className="flex items-center gap-2">
<RadioGroupItem value="N" id="useN" />
<Label htmlFor="useN">미사용</Label>
</div>
</RadioGroup>
</FormControl>
</FormItem>
)}
/>
</TableCell>
</TableRow>
</TableBody>
</Table>
</div>
</FormWrapper>
);
}
export { ScheduleForm };