Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: #301 프로젝트 생성 & 할 일 생성 날짜 종료일 수정 #307

Merged
merged 5 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 30 additions & 7 deletions src/components/common/PeriodDateInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type PeriodDateInputProps = {
endDateLabel: string;
startDateFieldName: string;
endDateFieldName: string;
enableEndDateSync: boolean;
limitStartDate?: string | Date | null;
limitEndDate?: string | Date | null;
};
Expand All @@ -27,6 +28,7 @@ export default function PeriodDateInput({
endDateFieldName,
limitStartDate = null,
limitEndDate = null,
enableEndDateSync = true,
}: PeriodDateInputProps) {
const [hasDeadline, setHasDeadline] = useState(false);
const { toastWarn } = useToast();
Expand All @@ -42,14 +44,29 @@ export default function PeriodDateInput({
const endDateStr = watch(endDateFieldName);

useEffect(() => {
const startDate = startDateStr ? DateTime.fromISO(startDateStr).startOf('day') : null;
const endDate = endDateStr ? DateTime.fromISO(endDateStr).startOf('day') : null;
if (startDate && endDate) setHasDeadline(startDate < endDate);
}, [startDateStr, endDateStr]);
if (enableEndDateSync && !hasDeadline) {
setValue(endDateFieldName, null);
} else {
const startDate = startDateStr ? DateTime.fromISO(startDateStr).startOf('day') : null;
const endDate = endDateStr ? DateTime.fromISO(endDateStr).startOf('day') : null;
if (startDate && endDate) {
setHasDeadline(startDate < endDate);
}
}
Seok93 marked this conversation as resolved.
Show resolved Hide resolved
}, [enableEndDateSync, hasDeadline, startDateStr, endDateStr]);

const handleDeadlineToggle = () => {
setValue(endDateFieldName, getValues(startDateFieldName));
clearErrors(endDateFieldName);
if (hasDeadline === false) {
if (enableEndDateSync) {
setValue(endDateFieldName, null);
} else {
setValue(endDateFieldName, getValues(startDateFieldName));
}
clearErrors(endDateFieldName);
} else {
setValue(endDateFieldName, getValues(startDateFieldName));
clearErrors(endDateFieldName);
}
Seok93 marked this conversation as resolved.
Show resolved Hide resolved
setHasDeadline((prev) => !prev);
};

Expand Down Expand Up @@ -82,7 +99,13 @@ export default function PeriodDateInput({
className={`${hasDeadline ? '' : '!bg-disable outline-none'}`}
readOnly={!hasDeadline}
{...register(endDateFieldName, {
...PERIOD_VALIDATION_RULES.END_DATE(hasDeadline, limitStartDate, limitEndDate, watch(startDateFieldName)),
...PERIOD_VALIDATION_RULES.END_DATE(
hasDeadline,
limitStartDate,
limitEndDate,
watch(startDateFieldName),
enableEndDateSync,
),
onChange: (e) => {
const startDate = DateTime.fromISO(startDateStr).startOf('day');
const endDate = DateTime.fromISO(e.target.value).startOf('day');
Expand Down
1 change: 1 addition & 0 deletions src/components/modal/project/ModalProjectForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export default function ModalProjectForm({ formId, onSubmit }: ModalProjectFormP
endDateId="endDate"
startDateFieldName="startDate"
endDateFieldName="endDate"
enableEndDateSync
/>

<SearchUserInput
Expand Down
1 change: 1 addition & 0 deletions src/components/modal/project/UpdateModalProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export default function UpdateModalProject({ projectId, onClose: handleClose }:
endDateId="endDate"
startDateFieldName="startDate"
endDateFieldName="endDate"
enableEndDateSync
/>
</form>
</FormProvider>
Expand Down
1 change: 1 addition & 0 deletions src/components/modal/task/ModalTaskForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export default function ModalTaskForm({ formId, project, onSubmit }: ModalTaskFo
endDateFieldName="endDate"
limitStartDate={projectStartDate}
limitEndDate={projectEndDate}
enableEndDateSync
/>

<div className="mb-20">
Expand Down
1 change: 1 addition & 0 deletions src/components/modal/task/UpdateModalTask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export default function UpdateModalTask({
endDateFieldName="endDate"
limitStartDate={projectStartDate}
limitEndDate={projectEndDate}
enableEndDateSync={false}
/>

<MarkdownEditor id="content" label="내용" contentFieldName="content" />
Expand Down
7 changes: 4 additions & 3 deletions src/constants/formValidationRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type ValidateOption = { [key: string]: (value: string) => string | boolean };
function getDateValidation(
periodStartDate: Date | string | null,
periodEndDate: Date | string | null,
referenceDate?: Date | string,
referenceDate?: Date | string | null,
) {
const validation: ValidateOption = {};
// 기준일이 설정되어 있다면, 기준일 이후로 설정되는가 검증
Expand Down Expand Up @@ -199,9 +199,10 @@ export const PERIOD_VALIDATION_RULES = deepFreeze({
hasDeadline: boolean,
periodStartDate: Date | string | null,
periodEndDate: Date | string | null,
referenceDate: Date | string,
referenceDate: Date | string | null,
enableEndDateSync: boolean,
) => ({
required: hasDeadline && '종료일을 선택해주세요.',
validate: getDateValidation(periodStartDate, periodEndDate, referenceDate),
validate: getDateValidation(periodStartDate, periodEndDate, enableEndDateSync ? null : referenceDate),
Seok93 marked this conversation as resolved.
Show resolved Hide resolved
}),
});
Loading