2023-03-15 05:30:05 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
2023-03-18 06:04:29 +00:00
|
|
|
|
2023-03-15 05:30:05 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
// ui
|
2023-03-30 11:29:07 +00:00
|
|
|
import { Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui";
|
|
|
|
// components
|
|
|
|
import { FilterList } from "components/core";
|
2023-03-15 05:30:05 +00:00
|
|
|
// types
|
|
|
|
import { IView } from "types";
|
2023-03-23 06:31:50 +00:00
|
|
|
// components
|
|
|
|
import { SelectFilters } from "components/views";
|
2023-03-15 05:30:05 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
handleFormSubmit: (values: IView) => Promise<void>;
|
|
|
|
handleClose: () => void;
|
|
|
|
status: boolean;
|
2023-04-12 10:03:21 +00:00
|
|
|
data?: IView | null;
|
2023-03-15 05:30:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: Partial<IView> = {
|
|
|
|
name: "",
|
|
|
|
description: "",
|
|
|
|
};
|
|
|
|
|
2023-04-12 10:03:21 +00:00
|
|
|
export const ViewForm: React.FC<Props> = ({ handleFormSubmit, handleClose, status, data }) => {
|
2023-03-15 05:30:05 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
handleSubmit,
|
|
|
|
reset,
|
2023-03-18 06:04:29 +00:00
|
|
|
watch,
|
|
|
|
setValue,
|
2023-03-15 05:30:05 +00:00
|
|
|
} = useForm<IView>({
|
|
|
|
defaultValues,
|
|
|
|
});
|
2023-04-12 10:03:21 +00:00
|
|
|
const filters = watch("query");
|
2023-03-15 05:30:05 +00:00
|
|
|
|
|
|
|
const handleCreateUpdateView = async (formData: IView) => {
|
|
|
|
await handleFormSubmit(formData);
|
|
|
|
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
...data,
|
|
|
|
});
|
|
|
|
}, [data, reset]);
|
|
|
|
|
2023-03-16 08:37:19 +00:00
|
|
|
useEffect(() => {
|
2023-04-12 10:03:21 +00:00
|
|
|
if (status && data) {
|
|
|
|
setValue("query", data.query_data);
|
|
|
|
}
|
|
|
|
}, [data, status, setValue]);
|
2023-03-18 06:04:29 +00:00
|
|
|
|
2023-03-15 05:30:05 +00:00
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit(handleCreateUpdateView)}>
|
|
|
|
<div className="space-y-5">
|
|
|
|
<h3 className="text-lg font-medium leading-6 text-gray-900">
|
|
|
|
{status ? "Update" : "Create"} View
|
|
|
|
</h3>
|
|
|
|
<div className="space-y-3">
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
type="name"
|
2023-04-12 10:03:21 +00:00
|
|
|
placeholder="Title"
|
2023-03-15 05:30:05 +00:00
|
|
|
autoComplete="off"
|
2023-04-12 10:03:21 +00:00
|
|
|
mode="transparent"
|
|
|
|
className="resize-none text-xl"
|
2023-03-15 05:30:05 +00:00
|
|
|
error={errors.name}
|
|
|
|
register={register}
|
|
|
|
validations={{
|
2023-04-12 10:03:21 +00:00
|
|
|
required: "Title is required",
|
2023-03-15 05:30:05 +00:00
|
|
|
maxLength: {
|
|
|
|
value: 255,
|
2023-04-12 10:03:21 +00:00
|
|
|
message: "Title should be less than 255 characters",
|
2023-03-15 05:30:05 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<TextArea
|
|
|
|
id="description"
|
|
|
|
name="description"
|
2023-04-12 10:03:21 +00:00
|
|
|
placeholder="Description"
|
|
|
|
className="h-32 resize-none text-sm"
|
|
|
|
mode="transparent"
|
2023-03-15 05:30:05 +00:00
|
|
|
error={errors.description}
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-03-18 06:04:29 +00:00
|
|
|
<div>
|
2023-03-23 06:31:50 +00:00
|
|
|
<SelectFilters
|
|
|
|
filters={filters}
|
2023-03-18 06:04:29 +00:00
|
|
|
onSelect={(option) => {
|
|
|
|
const key = option.key as keyof typeof filters;
|
|
|
|
|
|
|
|
if (!filters?.[key]?.includes(option.value))
|
|
|
|
setValue("query", {
|
|
|
|
...filters,
|
|
|
|
[key]: [...((filters?.[key] as any[]) ?? []), option.value],
|
|
|
|
});
|
|
|
|
else {
|
|
|
|
setValue("query", {
|
|
|
|
...filters,
|
|
|
|
[key]: (filters?.[key] as any[])?.filter((item) => item !== option.value),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
2023-03-30 11:29:07 +00:00
|
|
|
<FilterList
|
|
|
|
filters={filters}
|
|
|
|
setFilters={(query: any) => {
|
|
|
|
setValue("query", {
|
|
|
|
...filters,
|
|
|
|
...query,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
2023-03-18 06:04:29 +00:00
|
|
|
</div>
|
2023-03-15 05:30:05 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="mt-5 flex justify-end gap-2">
|
2023-03-17 05:10:38 +00:00
|
|
|
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
|
|
|
|
<PrimaryButton type="submit" loading={isSubmitting}>
|
2023-03-15 05:30:05 +00:00
|
|
|
{status
|
|
|
|
? isSubmitting
|
|
|
|
? "Updating View..."
|
|
|
|
: "Update View"
|
|
|
|
: isSubmitting
|
|
|
|
? "Creating View..."
|
|
|
|
: "Create View"}
|
2023-03-17 05:10:38 +00:00
|
|
|
</PrimaryButton>
|
2023-03-15 05:30:05 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|