2023-03-23 05:31:06 +00:00
|
|
|
import { useEffect } from "react";
|
2023-03-27 17:49:05 +00:00
|
|
|
|
|
|
|
// react-hook-form
|
2023-04-24 05:49:53 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
2023-03-23 05:31:06 +00:00
|
|
|
// ui
|
2023-03-27 17:49:05 +00:00
|
|
|
import { Input, Loader, PrimaryButton, SecondaryButton } from "components/ui";
|
2023-03-23 05:31:06 +00:00
|
|
|
// types
|
2023-03-25 18:09:46 +00:00
|
|
|
import { IPage } from "types";
|
2023-03-23 05:31:06 +00:00
|
|
|
|
|
|
|
type Props = {
|
2023-03-25 18:09:46 +00:00
|
|
|
handleFormSubmit: (values: IPage) => Promise<void>;
|
2023-03-23 05:31:06 +00:00
|
|
|
handleClose: () => void;
|
|
|
|
status: boolean;
|
2023-03-25 18:09:46 +00:00
|
|
|
data?: IPage | null;
|
2023-03-23 05:31:06 +00:00
|
|
|
};
|
|
|
|
|
2023-03-25 18:09:46 +00:00
|
|
|
const defaultValues = {
|
2023-03-23 05:31:06 +00:00
|
|
|
name: "",
|
|
|
|
description: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
export const PageForm: React.FC<Props> = ({ handleFormSubmit, handleClose, status, data }) => {
|
|
|
|
const {
|
|
|
|
register,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
handleSubmit,
|
|
|
|
reset,
|
2023-03-25 18:09:46 +00:00
|
|
|
} = useForm<IPage>({
|
2023-03-23 05:31:06 +00:00
|
|
|
defaultValues,
|
|
|
|
});
|
|
|
|
|
2023-03-25 18:09:46 +00:00
|
|
|
const handleCreateUpdatePage = async (formData: IPage) => {
|
2023-03-23 05:31:06 +00:00
|
|
|
await handleFormSubmit(formData);
|
|
|
|
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
...data,
|
|
|
|
});
|
|
|
|
}, [data, reset]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit(handleCreateUpdatePage)}>
|
|
|
|
<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-03-23 05:31:06 +00:00
|
|
|
{status ? "Update" : "Create"} Page
|
|
|
|
</h3>
|
|
|
|
<div className="space-y-3">
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
type="name"
|
2023-04-24 05:49:53 +00:00
|
|
|
placeholder="Title"
|
|
|
|
className="resize-none text-xl"
|
2023-03-23 05:31:06 +00:00
|
|
|
autoComplete="off"
|
|
|
|
error={errors.name}
|
|
|
|
register={register}
|
|
|
|
validations={{
|
2023-04-24 05:49:53 +00:00
|
|
|
required: "Title is required",
|
2023-03-23 05:31:06 +00:00
|
|
|
maxLength: {
|
|
|
|
value: 255,
|
2023-04-24 05:49:53 +00:00
|
|
|
message: "Title should be less than 255 characters",
|
2023-03-23 05:31:06 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="mt-5 flex justify-end gap-2">
|
|
|
|
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
|
|
|
|
<PrimaryButton type="submit" loading={isSubmitting}>
|
|
|
|
{status
|
|
|
|
? isSubmitting
|
|
|
|
? "Updating Page..."
|
|
|
|
: "Update Page"
|
|
|
|
: isSubmitting
|
|
|
|
? "Creating Page..."
|
|
|
|
: "Create Page"}
|
|
|
|
</PrimaryButton>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|