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 | 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import { showMessage } from "@common/Toast";
import { useGetCurrentMerchantId } from "@hooks/common";
import useSaveJson from "features/Permissions/Modal/api/useSaveJson";
import { useState } from "react";
import { useQueryClient } from "react-query";
import { QKEY_LIST_TEAM_MEMBER_PERMISSIONS } from "@constants/queryKeys";
const useJSONEditor = (
onAdd: (permissionIds: string[]) => void,
memberId: number,
) => {
const [value, setValue] = useState<string>("");
const [disabled, setIsDisabled] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(false);
const { merchantId } = useGetCurrentMerchantId();
const client = useQueryClient();
const { handleSendJson } = useSaveJson(merchantId, memberId);
const onChange = (newValue: string) => setValue(newValue);
const onValidation = (isValid: boolean) => {
if (!isValid) {
setIsDisabled(true);
} else {
setIsDisabled(false);
}
};
const onSubmit = async () => {
if (!value) return;
try {
setIsLoading(true);
const response = await handleSendJson(value);
await client.invalidateQueries(QKEY_LIST_TEAM_MEMBER_PERMISSIONS);
const ids = Array.isArray(response)
? response.map((res) => res.id as string)
: [response.id as string];
onAdd(ids);
} catch (err) {
showMessage("Error", "The JSON is not valid");
}
setIsLoading(false);
};
return {
onChange,
isDisabled: disabled || !value,
onValidation,
onSubmit,
isLoading,
};
};
export default useJSONEditor;
|