135 lines
3.5 KiB
TypeScript
135 lines
3.5 KiB
TypeScript
"use client";
|
|
import {
|
|
useCompanyCategoriesQuery,
|
|
useCreateCompany,
|
|
useUpdateCompany,
|
|
} from "@/hooks/queries";
|
|
import { ICreateCompanyCredentials } from "@/lib/api";
|
|
import { useIsMutating } from "@tanstack/react-query";
|
|
import { useRouter } from "next/navigation";
|
|
import { useEffect } from "react";
|
|
import { SubmitHandler, useForm } from "react-hook-form";
|
|
|
|
interface IProps {
|
|
editMode?: boolean;
|
|
defaultValues?: ICreateCompanyCredentials;
|
|
id?: string;
|
|
}
|
|
|
|
const useCreateCompanyPresenter = ({ defaultValues, editMode, id }: IProps) => {
|
|
const isMutating = useIsMutating();
|
|
const {
|
|
register,
|
|
control,
|
|
formState: { errors },
|
|
handleSubmit,
|
|
watch,
|
|
setValue,
|
|
reset,
|
|
} = useForm<ICreateCompanyCredentials>({
|
|
mode: "onChange",
|
|
defaultValues: {
|
|
...defaultValues,
|
|
},
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (defaultValues) {
|
|
const categories = defaultValues?.tags?.categories?.map(
|
|
(item: any) => item.id,
|
|
);
|
|
setValue("tags.categories", categories);
|
|
}
|
|
}, [defaultValues, setValue]);
|
|
const router = useRouter();
|
|
const { data: companyCategories } = useCompanyCategoriesQuery({
|
|
published: true,
|
|
});
|
|
const { mutate: createCompany, isPending: isCreating } = useCreateCompany();
|
|
const { mutate: updateCompany, isPending: isUpdating } = useUpdateCompany(
|
|
id as string,
|
|
);
|
|
const { mutateAsync: updateCompanyDocuments } = useUpdateCompany(
|
|
id as string,
|
|
{
|
|
redirectOnSuccess: false,
|
|
showSuccessToast: false,
|
|
},
|
|
);
|
|
|
|
const optionalNumber = (value: unknown) => {
|
|
if (value === "" || value === null || value === undefined) return undefined;
|
|
|
|
return Number(value);
|
|
};
|
|
|
|
const formatCompanyData = (
|
|
data: ICreateCompanyCredentials,
|
|
documentFileIds = data.document_file_ids,
|
|
) => ({
|
|
...data,
|
|
founded_year: optionalNumber(data.founded_year),
|
|
employee_count: optionalNumber(data.employee_count),
|
|
annual_revenue: optionalNumber(data.annual_revenue),
|
|
document_file_ids: documentFileIds ?? [],
|
|
tags: {
|
|
keywords: data?.tags?.keywords?.length ? data?.tags?.keywords : [],
|
|
categories: data?.tags?.categories?.length ? data?.tags?.categories : [],
|
|
cpv_codes: data?.tags?.cpv_codes?.length ? data?.tags?.cpv_codes : [],
|
|
specializations: data?.tags?.specializations?.length
|
|
? data?.tags?.specializations
|
|
: [],
|
|
certifications: data?.tags?.certifications?.length
|
|
? data?.tags?.certifications
|
|
: [],
|
|
},
|
|
});
|
|
|
|
const onSubmit: SubmitHandler<ICreateCompanyCredentials> = (data) => {
|
|
const formattedData = formatCompanyData(data);
|
|
if (editMode) {
|
|
updateCompany({
|
|
id: id as string,
|
|
credentials: formattedData as ICreateCompanyCredentials,
|
|
});
|
|
} else {
|
|
createCompany(formattedData as ICreateCompanyCredentials);
|
|
}
|
|
};
|
|
|
|
const persistDocumentFileIds = async (documentFileIds: string[]) => {
|
|
if (!editMode || !id) return;
|
|
|
|
await updateCompanyDocuments({
|
|
id,
|
|
credentials: formatCompanyData(
|
|
watch() as ICreateCompanyCredentials,
|
|
documentFileIds,
|
|
) as ICreateCompanyCredentials,
|
|
});
|
|
};
|
|
|
|
const handleCancel = () => {
|
|
reset();
|
|
router.back();
|
|
};
|
|
return {
|
|
errors,
|
|
register,
|
|
control,
|
|
handleSubmit,
|
|
onSubmit,
|
|
handleCancel,
|
|
isMutating,
|
|
isCreating,
|
|
isUpdating,
|
|
router,
|
|
watch,
|
|
setValue,
|
|
persistDocumentFileIds,
|
|
companyCategories,
|
|
};
|
|
};
|
|
|
|
export default useCreateCompanyPresenter;
|