https://dev.kronoz.win/r/marzneshin.git
1import { useState } from "react";
2import { NodeType, useNodesSettingsMutation, useNodesSettingsQuery } from "../..";
3
4export const useNodesSettings = (entity: NodeType) => {
5 const { data } = useNodesSettingsQuery(entity);
6 const [config, setConfig] = useState<any>(data)
7 const [payloadValidity, setPayloadValidity] = useState<boolean>(true)
8 const mutate = useNodesSettingsMutation();
9
10 const handleEditorValidation = (markers: any[]) => {
11 setPayloadValidity(!markers.length)
12 }
13
14 const handleConfigSave = () => {
15 mutate.mutate({ node: entity, config })
16 }
17
18 const handleConfigChange = (newConfig: string | undefined) => {
19 if (newConfig) {
20 try {
21 const parsedConfig = JSON.parse(newConfig);
22 setConfig(parsedConfig);
23 } catch (error) {
24 setPayloadValidity(false)
25 }
26 }
27 };
28
29 return { payloadValidity, data, handleConfigSave, handleConfigChange, handleEditorValidation }
30}