2023-03-23 05:31:06 +00:00
|
|
|
import { useEffect } from "react";
|
2023-03-27 17:49:05 +00:00
|
|
|
|
|
|
|
import dynamic from "next/dynamic";
|
|
|
|
|
|
|
|
// react-hook-form
|
|
|
|
import { Controller, 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-27 17:49:05 +00:00
|
|
|
// rich-text-editor
|
|
|
|
const RemirrorRichTextEditor = dynamic(() => import("components/rich-text-editor"), {
|
|
|
|
ssr: false,
|
|
|
|
loading: () => (
|
|
|
|
<Loader>
|
|
|
|
<Loader.Item height="12rem" width="100%" />
|
|
|
|
</Loader>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
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-27 17:49:05 +00:00
|
|
|
control,
|
|
|
|
setValue,
|
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-04-20 08:11:24 +00:00
|
|
|
<h3 className="text-lg font-medium leading-6 text-brand-base">
|
2023-03-23 05:31:06 +00:00
|
|
|
{status ? "Update" : "Create"} Page
|
|
|
|
</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>
|
2023-03-27 17:49:05 +00:00
|
|
|
{/* <div>
|
|
|
|
<Controller
|
2023-03-23 05:31:06 +00:00
|
|
|
name="description"
|
2023-03-27 17:49:05 +00:00
|
|
|
control={control}
|
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<RemirrorRichTextEditor
|
|
|
|
value={value}
|
|
|
|
onJSONChange={(jsonValue) => setValue("description", jsonValue)}
|
|
|
|
onHTMLChange={(htmlValue) => setValue("description_html", htmlValue)}
|
|
|
|
placeholder="Description"
|
|
|
|
/>
|
|
|
|
)}
|
2023-03-23 05:31:06 +00:00
|
|
|
/>
|
2023-03-27 17:49:05 +00:00
|
|
|
</div> */}
|
2023-03-23 05:31:06 +00:00
|
|
|
</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>
|
|
|
|
);
|
|
|
|
};
|