You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hooks.ts 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import get from 'lodash/get';
  2. import { useCallback, useMemo } from 'react';
  3. import { v4 as uuid } from 'uuid';
  4. import { IGenerateParameter } from '../interface';
  5. import useGraphStore from '../store';
  6. export const useHandleOperateParameters = (nodeId: string) => {
  7. const { getNode, updateNodeForm } = useGraphStore((state) => state);
  8. const node = getNode(nodeId);
  9. const dataSource: IGenerateParameter[] = useMemo(
  10. () => get(node, 'data.form.parameters', []) as IGenerateParameter[],
  11. [node],
  12. );
  13. const handleComponentIdChange = useCallback(
  14. (row: IGenerateParameter) => (value: string) => {
  15. const newData = [...dataSource];
  16. const index = newData.findIndex((item) => row.id === item.id);
  17. const item = newData[index];
  18. newData.splice(index, 1, {
  19. ...item,
  20. component_id: value,
  21. });
  22. updateNodeForm(nodeId, { parameters: newData });
  23. },
  24. [updateNodeForm, nodeId, dataSource],
  25. );
  26. const handleRemove = useCallback(
  27. (id?: string) => () => {
  28. const newData = dataSource.filter((item) => item.id !== id);
  29. updateNodeForm(nodeId, { parameters: newData });
  30. },
  31. [updateNodeForm, nodeId, dataSource],
  32. );
  33. const handleAdd = useCallback(() => {
  34. updateNodeForm(nodeId, {
  35. parameters: [
  36. ...dataSource,
  37. {
  38. id: uuid(),
  39. key: '',
  40. component_id: undefined,
  41. },
  42. ],
  43. });
  44. }, [dataSource, nodeId, updateNodeForm]);
  45. const handleSave = (row: IGenerateParameter) => {
  46. const newData = [...dataSource];
  47. const index = newData.findIndex((item) => row.id === item.id);
  48. const item = newData[index];
  49. newData.splice(index, 1, {
  50. ...item,
  51. ...row,
  52. });
  53. updateNodeForm(nodeId, { parameters: newData });
  54. };
  55. return {
  56. handleAdd,
  57. handleRemove,
  58. handleComponentIdChange,
  59. handleSave,
  60. dataSource,
  61. };
  62. };