2023-08-21 06:16:02 +00:00
|
|
|
import { useEffect } from "react";
|
2023-02-20 05:53:04 +00:00
|
|
|
|
2023-02-02 12:34:13 +00:00
|
|
|
// react-hook-form
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
// components
|
|
|
|
import { ModuleLeadSelect, ModuleMembersSelect, ModuleStatusSelect } from "components/modules";
|
|
|
|
// ui
|
2023-04-24 05:49:53 +00:00
|
|
|
import { DateSelect, Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui";
|
2023-02-02 12:34:13 +00:00
|
|
|
// types
|
|
|
|
import { IModule } from "types";
|
|
|
|
|
|
|
|
type Props = {
|
2023-02-20 05:53:04 +00:00
|
|
|
handleFormSubmit: (values: Partial<IModule>) => Promise<void>;
|
2023-02-02 12:34:13 +00:00
|
|
|
handleClose: () => void;
|
|
|
|
status: boolean;
|
2023-02-20 05:53:04 +00:00
|
|
|
data?: IModule;
|
2023-02-02 12:34:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: Partial<IModule> = {
|
|
|
|
name: "",
|
|
|
|
description: "",
|
2023-05-29 16:46:32 +00:00
|
|
|
status: "backlog",
|
2023-02-02 12:34:13 +00:00
|
|
|
lead: null,
|
|
|
|
members_list: [],
|
|
|
|
};
|
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
export const ModuleForm: React.FC<Props> = ({ handleFormSubmit, handleClose, status, data }) => {
|
2023-02-02 12:34:13 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
handleSubmit,
|
2023-03-30 10:30:48 +00:00
|
|
|
watch,
|
2023-02-02 12:34:13 +00:00
|
|
|
control,
|
|
|
|
reset,
|
|
|
|
} = useForm<IModule>({
|
|
|
|
defaultValues,
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleCreateUpdateModule = async (formData: Partial<IModule>) => {
|
|
|
|
await handleFormSubmit(formData);
|
|
|
|
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
useEffect(() => {
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
...data,
|
|
|
|
});
|
|
|
|
}, [data, reset]);
|
|
|
|
|
2023-08-21 06:16:02 +00:00
|
|
|
const startDate = watch("start_date");
|
|
|
|
const targetDate = watch("target_date");
|
|
|
|
|
|
|
|
const minDate = startDate ? new Date(startDate) : null;
|
|
|
|
minDate?.setDate(minDate.getDate());
|
|
|
|
|
|
|
|
const maxDate = targetDate ? new Date(targetDate) : null;
|
|
|
|
maxDate?.setDate(maxDate.getDate());
|
|
|
|
|
2023-02-02 12:34:13 +00:00
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit(handleCreateUpdateModule)}>
|
|
|
|
<div className="space-y-5">
|
2023-07-10 07:17:00 +00:00
|
|
|
<h3 className="text-lg font-medium leading-6 text-custom-text-100">
|
2023-02-02 12:34:13 +00:00
|
|
|
{status ? "Update" : "Create"} Module
|
|
|
|
</h3>
|
|
|
|
<div className="space-y-3">
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
type="name"
|
2023-03-30 13:58:04 +00:00
|
|
|
placeholder="Title"
|
2023-02-02 12:34:13 +00:00
|
|
|
autoComplete="off"
|
2023-03-30 13:58:04 +00:00
|
|
|
className="resize-none text-xl"
|
2023-02-02 12:34:13 +00:00
|
|
|
error={errors.name}
|
|
|
|
register={register}
|
|
|
|
validations={{
|
2023-03-30 13:58:04 +00:00
|
|
|
required: "Title is required",
|
2023-02-02 12:34:13 +00:00
|
|
|
maxLength: {
|
|
|
|
value: 255,
|
2023-03-30 13:58:04 +00:00
|
|
|
message: "Title should be less than 255 characters",
|
2023-02-02 12:34:13 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<TextArea
|
|
|
|
id="description"
|
|
|
|
name="description"
|
2023-03-30 13:58:04 +00:00
|
|
|
placeholder="Description"
|
|
|
|
className="h-32 resize-none text-sm"
|
2023-02-02 12:34:13 +00:00
|
|
|
error={errors.description}
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-03-30 13:58:04 +00:00
|
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="start_date"
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<DateSelect
|
|
|
|
label="Start date"
|
|
|
|
value={value}
|
|
|
|
onChange={(val) => {
|
|
|
|
onChange(val);
|
|
|
|
}}
|
2023-08-21 06:16:02 +00:00
|
|
|
maxDate={maxDate ?? undefined}
|
2023-02-02 12:34:13 +00:00
|
|
|
/>
|
2023-03-30 13:58:04 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="target_date"
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<DateSelect
|
|
|
|
label="Target date"
|
|
|
|
value={value}
|
|
|
|
onChange={(val) => {
|
|
|
|
onChange(val);
|
|
|
|
}}
|
2023-08-21 06:16:02 +00:00
|
|
|
minDate={minDate ?? undefined}
|
2023-02-02 12:34:13 +00:00
|
|
|
/>
|
2023-03-30 13:58:04 +00:00
|
|
|
)}
|
|
|
|
/>
|
2023-02-02 12:34:13 +00:00
|
|
|
<ModuleStatusSelect control={control} error={errors.status} />
|
2023-03-07 17:26:22 +00:00
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="lead"
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<ModuleLeadSelect value={value} onChange={onChange} />
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="members"
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<ModuleMembersSelect value={value} onChange={onChange} />
|
|
|
|
)}
|
|
|
|
/>
|
2023-02-02 12:34:13 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-07-17 10:58:23 +00:00
|
|
|
<div className="-mx-5 mt-5 flex justify-end gap-2 border-t border-custom-border-200 px-5 pt-5">
|
2023-03-17 05:10:38 +00:00
|
|
|
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
|
2023-08-21 06:16:02 +00:00
|
|
|
<PrimaryButton type="submit" loading={isSubmitting}>
|
2023-02-02 12:34:13 +00:00
|
|
|
{status
|
|
|
|
? isSubmitting
|
|
|
|
? "Updating Module..."
|
|
|
|
: "Update Module"
|
|
|
|
: isSubmitting
|
|
|
|
? "Creating Module..."
|
|
|
|
: "Create Module"}
|
2023-03-17 05:10:38 +00:00
|
|
|
</PrimaryButton>
|
2023-02-02 12:34:13 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|