This commit is contained in:
@@ -49,6 +49,13 @@ const useCreateCompanyPresenter = ({ defaultValues, editMode, id }: IProps) => {
|
||||
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;
|
||||
@@ -56,27 +63,30 @@ const useCreateCompanyPresenter = ({ defaultValues, editMode, id }: IProps) => {
|
||||
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 = {
|
||||
...data,
|
||||
founded_year: optionalNumber(data.founded_year),
|
||||
employee_count: optionalNumber(data.employee_count),
|
||||
annual_revenue: optionalNumber(data.annual_revenue),
|
||||
document_file_ids: data.document_file_ids ?? [],
|
||||
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 formattedData = formatCompanyData(data);
|
||||
if (editMode) {
|
||||
updateCompany({
|
||||
id: id as string,
|
||||
@@ -87,6 +97,18 @@ const useCreateCompanyPresenter = ({ defaultValues, editMode, id }: IProps) => {
|
||||
}
|
||||
};
|
||||
|
||||
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();
|
||||
@@ -104,6 +126,7 @@ const useCreateCompanyPresenter = ({ defaultValues, editMode, id }: IProps) => {
|
||||
router,
|
||||
watch,
|
||||
setValue,
|
||||
persistDocumentFileIds,
|
||||
companyCategories,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user