Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | 3x 224x 224x 224x 224x 1x 224x 3x 224x 224x 224x 1x 1x 1x 12x 1x 1x 1x 224x 91x 91x 38x 456x 53x 636x 91x 224x | import { useModal } from "@ebay/nice-modal-react";
import { useEffect } from "react";
import { SubmitHandler, useForm } from "react-hook-form";
import { useMutation } from "react-query";
import { customInstance } from "@services/api";
import { isEmpty } from "lodash";
import { useGetEnterpriseConfigurationById } from "./useGetEnterpriseConfigurationById";
import { EnterpriseConfigurationList } from "../constants/EnterpriseConfiguration.constants";
type Props = {
data: { [key: string]: boolean };
enterpriseId: number;
onClose?: (data: { [key: string]: boolean }, id?: number) => void;
};
export const useEnterpriseConfiguration = ({
onClose,
enterpriseId,
data,
}: Props) => {
const modal = useModal();
const open = modal.visible;
const {
data: enterpriseConfiguration,
isLoading,
refetch,
} = useGetEnterpriseConfigurationById(enterpriseId);
const updateEnterpriseConfigurationMutation = useMutation((data: any) => {
return customInstance({
url: `/enterprises/${enterpriseId}/configuration`,
method: "PUT",
data,
});
});
const handleCancel = () => {
modal.hide();
};
const methods = useForm({});
const {
reset,
formState: { isDirty },
} = methods;
const onSubmit: SubmitHandler<{
[key: string]: boolean;
}> = async (data) => {
Iif (onClose && !enterpriseId) {
onClose(data);
handleCancel();
return;
}
Iif (!enterpriseConfiguration?.data) return;
const updatedConfiguration = enterpriseConfiguration?.data?.map(
(item: TEnterpriseConfiguration) => ({
id: item.id,
name: item.name,
isActive: data[item.name],
}),
);
updateEnterpriseConfigurationMutation.mutate(
{
configurations: updatedConfiguration,
},
{
onSuccess: () => {
refetch();
handleCancel();
},
},
);
};
useEffect(() => {
const list: { [key: string]: boolean } = {};
if (enterpriseId && enterpriseConfiguration?.data) {
enterpriseConfiguration?.data?.forEach(
(item: TEnterpriseConfiguration) => {
list[item.name] = item.isActive;
},
);
} else {
EnterpriseConfigurationList.forEach((item) => {
list[item.name] = item.isActive;
});
}
reset(data && !isEmpty(data) ? data : list);
}, [open, isLoading, enterpriseId]);
return {
open,
handleCancel,
methods,
onSubmit,
disabled: updateEnterpriseConfigurationMutation.isLoading || !isDirty,
};
};
export type TEnterpriseConfiguration = {
id?: number;
name: string;
displayName: string;
description: string;
group: string;
isActive: boolean;
configurationID?: number;
restrictedOnCreate?: boolean;
};
|