2023-03-30 10:30:48 +00:00
|
|
|
import { useEffect, useState } 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";
|
2023-03-30 10:30:48 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
2023-02-02 12:34:13 +00:00
|
|
|
// components
|
|
|
|
import { ModuleLeadSelect, ModuleMembersSelect, ModuleStatusSelect } from "components/modules";
|
|
|
|
// ui
|
2023-03-17 05:10:38 +00:00
|
|
|
import { CustomDatePicker, Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui";
|
2023-03-30 10:30:48 +00:00
|
|
|
// helper
|
|
|
|
import { isDateRangeValid } from "helpers/date-time.helper";
|
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: "",
|
|
|
|
status: null,
|
|
|
|
lead: null,
|
|
|
|
members_list: [],
|
|
|
|
};
|
|
|
|
|
2023-02-20 05:53:04 +00:00
|
|
|
export const ModuleForm: React.FC<Props> = ({ handleFormSubmit, handleClose, status, data }) => {
|
2023-03-30 10:30:48 +00:00
|
|
|
const [isDateValid, setIsDateValid] = useState(true);
|
|
|
|
const { setToastAlert } = useToast();
|
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-02-02 12:34:13 +00:00
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit(handleCreateUpdateModule)}>
|
|
|
|
<div className="space-y-5">
|
|
|
|
<h3 className="text-lg font-medium leading-6 text-gray-900">
|
|
|
|
{status ? "Update" : "Create"} Module
|
|
|
|
</h3>
|
|
|
|
<div className="space-y-3">
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
label="Name"
|
|
|
|
name="name"
|
|
|
|
type="name"
|
|
|
|
placeholder="Enter name"
|
|
|
|
autoComplete="off"
|
|
|
|
error={errors.name}
|
|
|
|
register={register}
|
|
|
|
validations={{
|
|
|
|
required: "Name is required",
|
|
|
|
maxLength: {
|
|
|
|
value: 255,
|
|
|
|
message: "Name should be less than 255 characters",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<TextArea
|
|
|
|
id="description"
|
|
|
|
name="description"
|
|
|
|
label="Description"
|
|
|
|
placeholder="Enter description"
|
|
|
|
error={errors.description}
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="flex gap-x-2">
|
|
|
|
<div className="w-full">
|
|
|
|
<h6 className="text-gray-500">Start Date</h6>
|
|
|
|
<div className="w-full">
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="start_date"
|
|
|
|
render={({ field: { value, onChange } }) => (
|
2023-03-30 10:30:48 +00:00
|
|
|
<CustomDatePicker
|
|
|
|
renderAs="input"
|
|
|
|
value={value}
|
|
|
|
onChange={(val) => {
|
|
|
|
onChange(val);
|
|
|
|
if (val && watch("target_date")) {
|
|
|
|
if (isDateRangeValid(val, `${watch("target_date")}`)) {
|
|
|
|
setIsDateValid(true);
|
|
|
|
} else {
|
|
|
|
setIsDateValid(false);
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "You have enter invalid date.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
2023-02-02 12:34:13 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="w-full">
|
|
|
|
<h6 className="text-gray-500">Target Date</h6>
|
|
|
|
<div className="w-full">
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="target_date"
|
|
|
|
render={({ field: { value, onChange } }) => (
|
2023-03-30 10:30:48 +00:00
|
|
|
<CustomDatePicker
|
|
|
|
renderAs="input"
|
|
|
|
value={value}
|
|
|
|
onChange={(val) => {
|
|
|
|
onChange(val);
|
|
|
|
if (watch("start_date") && val) {
|
|
|
|
if (isDateRangeValid(`${watch("start_date")}`, val)) {
|
|
|
|
setIsDateValid(true);
|
|
|
|
} else {
|
|
|
|
setIsDateValid(false);
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "You have enter invalid date.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
2023-02-02 12:34:13 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
|
|
<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>
|
|
|
|
<div className="mt-5 flex justify-end gap-2">
|
2023-03-17 05:10:38 +00:00
|
|
|
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
|
2023-03-30 10:30:48 +00:00
|
|
|
<PrimaryButton type="submit" loading={isSubmitting || isDateValid ? false : true}>
|
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>
|
|
|
|
);
|
|
|
|
};
|