Skip to content

Commit 5dc516b

Browse files
committed
Have request node use canvas store to push updates
1 parent 27edbae commit 5dc516b

File tree

4 files changed

+194
-166
lines changed

4 files changed

+194
-166
lines changed

src/components/molecules/flow/nodes/AuthNode.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,12 @@ import useCanvasStore from 'stores/CanvasStore';
66
const AuthNode = ({ id, data }) => {
77
const setAuthNodeType = useCanvasStore((state) => state.setAuthNodeType);
88
const setBasicAuthValues = useCanvasStore((state) => state.setBasicAuthValues);
9-
/* its better to have no space strings as values since they are less error/bug prone */
10-
// const initState = () => {
11-
// if (data.auth && data.auth.type) {
12-
// return data.auth.type; // should return basic-auth
13-
// } else {
14-
// data.auth = {};
15-
// data.auth.type = 'no-auth';
16-
// return 'no-auth';
17-
// }
18-
// };
199

20-
// // const [anchorEl, setAnchorEl] = React.useState(null);
21-
// const [auth, setAuth] = React.useState(initState());
22-
23-
/**
24-
* Not sure whether you need to set the value for data props/param or not.
25-
* Technically you should not set the value for a prop/param in a component
26-
* Check this and other places in this file once
27-
*/
2810
const handleChange = (value, option) => {
2911
setBasicAuthValues(id, option, value);
3012
};
3113

3214
const handleSelection = (event) => {
33-
//const selectedValue = event.target?.value;
3415
setAuthNodeType(id, event.target?.value);
3516
};
3617

src/components/molecules/flow/nodes/RequestBody.js

Lines changed: 37 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,14 @@ import { PropTypes } from 'prop-types';
33
import { EllipsisVerticalIcon } from '@heroicons/react/24/solid';
44
import { DocumentArrowUpIcon } from '@heroicons/react/24/outline';
55
import { Menu, Transition } from '@headlessui/react';
6+
import useCanvasStore from 'stores/CanvasStore';
67

7-
const requestBodyTypeOptions = ['None', 'form-data', 'raw-json', 'raw-txt'];
8+
const requestBodyTypeOptions = ['None', 'form-data', 'raw-json'];
89

9-
const RequestBody = ({ nodeData }) => {
10-
const uploadFileForRequestNode = useRef(null);
11-
const [bodyType, setBodyType] = useState(nodeData.requestBody ? nodeData.requestBody.type : 'None');
10+
const RequestBody = ({ nodeId, nodeData }) => {
11+
const setRequestNodeBody = useCanvasStore((state) => state.setRequestNodeBody);
1212

13-
// form-data
14-
const [fileName, setFileName] = useState(
15-
nodeData.requestBody && nodeData.requestBody.body && nodeData.requestBody.body.name
16-
? nodeData.requestBody.body.name
17-
: '',
18-
);
19-
const [fileValue, setFileValue] = useState(
20-
nodeData.requestBody && nodeData.requestBody.body && nodeData.requestBody.body.value
21-
? nodeData.requestBody.body.name
22-
: '',
23-
);
13+
const uploadFileForRequestNode = useRef(null);
2414

2515
const handleFileUpload = async (e) => {
2616
if (!e.target.files) return;
@@ -38,66 +28,45 @@ const RequestBody = ({ nodeData }) => {
3828

3929
const value = result;
4030

41-
setFileName(name);
42-
setFileValue(value);
43-
44-
if (!nodeData.requestBody.body) {
45-
nodeData.requestBody.body = {};
46-
}
47-
48-
nodeData.requestBody.body.value = value;
49-
nodeData.requestBody.body.name = name;
31+
setRequestNodeBody(nodeId, 'form-data', {
32+
value: value,
33+
name: name,
34+
});
5035
};
5136
reader.readAsDataURL(file);
5237
}
5338
};
5439

55-
const [fileKey, setFileKey] = useState(
56-
bodyType === 'form-data' && nodeData.requestBody && nodeData.requestBody.body ? nodeData.requestBody.body.key : '',
57-
);
5840
const handleFormDataKey = (e) => {
59-
if (!nodeData.requestBody.body) {
60-
nodeData.requestBody.body = {};
61-
}
62-
setFileKey(e.target.value);
63-
nodeData.requestBody.body.key = e.target.value;
41+
setRequestNodeBody(nodeId, 'form-data', {
42+
key: e.target.value,
43+
});
6444
};
6545

66-
// raw-json
67-
const [jsonValue, setJsonValue] = useState(
68-
bodyType === 'raw-json' && nodeData.requestBody && nodeData.requestBody.body ? nodeData.requestBody.body : '{}',
69-
);
70-
7146
const handleRawJson = (e) => {
72-
setJsonValue(e.target.value);
73-
nodeData.requestBody.body = e.target.value;
47+
setRequestNodeBody(nodeId, 'raw-json', e.target.value);
7448
};
7549

7650
const handleClose = (option) => {
77-
nodeData.requestBody = {};
78-
nodeData.requestBody.type = option;
79-
setBodyType(option);
80-
81-
//console.log(`handleClose:: ${option}`);
82-
8351
if (option == 'raw-json') {
84-
nodeData.requestBody.body = jsonValue;
52+
setRequestNodeBody(nodeId, option, '');
8553
} else if (option == 'form-data') {
86-
nodeData.requestBody.body = {};
87-
nodeData.requestBody.body.key = fileKey;
88-
nodeData.requestBody.body.value = fileValue;
89-
nodeData.requestBody.body.name = fileName;
54+
setRequestNodeBody(nodeId, option, {
55+
key: '',
56+
value: '',
57+
name: '',
58+
});
9059
}
9160
};
9261

9362
return (
9463
<>
95-
<div className='px-2 py-4 border-t border-neutral-300 bg-slate-100'>
64+
<div className='border-t border-neutral-300 bg-slate-100 px-2 py-4'>
9665
<div className='flex items-center justify-between font-medium'>
9766
<h3>Body</h3>
9867
<Menu as='div' className='relative inline-block text-left'>
9968
<Menu.Button data-click-from='body-type-menu' className='p-2'>
100-
<EllipsisVerticalIcon className='w-4 h-4' aria-hidden='true' data-click-from='body-type-menu' />
69+
<EllipsisVerticalIcon className='h-4 w-4' aria-hidden='true' data-click-from='body-type-menu' />
10170
</Menu.Button>
10271
<Transition
10372
as={Fragment}
@@ -109,13 +78,13 @@ const RequestBody = ({ nodeData }) => {
10978
leaveTo='transform opacity-0 scale-95'
11079
>
11180
<Menu.Items
112-
className='absolute right-0 z-10 w-56 px-1 py-1 mt-2 origin-top-right bg-white divide-y divide-gray-100 rounded-md shadow-lg ring-1 ring-black/5 focus:outline-none'
81+
className='absolute right-0 z-10 mt-2 w-56 origin-top-right divide-y divide-gray-100 rounded-md bg-white px-1 py-1 shadow-lg ring-1 ring-black/5 focus:outline-none'
11382
data-click-from='body-type-menu'
11483
>
11584
{requestBodyTypeOptions.map((bodyTypeOption, index) => (
11685
<Menu.Item key={index} data-click-from='body-type-menu' onClick={() => handleClose(bodyTypeOption)}>
11786
<button
118-
className='flex items-center w-full px-2 py-2 text-sm text-gray-900 rounded-md group hover:bg-slate-100'
87+
className='group flex w-full items-center rounded-md px-2 py-2 text-sm text-gray-900 hover:bg-slate-100'
11988
data-click-from='body-type-menu'
12089
>
12190
{bodyTypeOption}
@@ -127,45 +96,45 @@ const RequestBody = ({ nodeData }) => {
12796
</Menu>
12897
</div>
12998
</div>
130-
{bodyType === 'raw-json' && (
131-
<div className='p-2 border border-t border-neutral-300 bg-slate-50'>
99+
{nodeData.requestBody && nodeData.requestBody.type === 'raw-json' && (
100+
<div className='border border-t border-neutral-300 bg-slate-50 p-2'>
132101
<textarea
133-
placeholder={bodyType}
134-
className='w-full p-2 nodrag nowheel'
102+
placeholder='Enter json'
103+
className='nodrag nowheel w-full p-2'
135104
name='username'
136105
onChange={(e) => handleRawJson(e)}
137106
rows={4}
138-
value={jsonValue}
107+
value={nodeData.requestBody.body}
139108
/>
140109
</div>
141110
)}
142-
{bodyType === 'form-data' && (
143-
<div className='p-2 border-t border-neutral-300 bg-slate-50'>
144-
<div className='flex items-center justify-between text-sm border rounded-md border-neutral-500 text-neutral-500 outline-0 focus:ring-0'>
111+
{nodeData.requestBody && nodeData.requestBody.type === 'form-data' && (
112+
<div className='border-t border-neutral-300 bg-slate-50 p-2'>
113+
<div className='flex items-center justify-between rounded-md border border-neutral-500 text-sm text-neutral-500 outline-0 focus:ring-0'>
145114
<input
146115
placeholder='key'
147-
className='pl-4 nodrag nowheel bg-slate-50'
116+
className='nodrag nowheel bg-slate-50 pl-4'
148117
name='variable-value'
149118
onChange={(e) => handleFormDataKey(e)}
150-
value={fileKey}
119+
value={nodeData.requestBody.body.key}
151120
/>
152-
<div className='px-4 py-2 border-l rounded-br-md rounded-tr-md border-l-neutral-500'>File</div>
121+
<div className='rounded-br-md rounded-tr-md border-l border-l-neutral-500 px-4 py-2'>File</div>
153122
</div>
154123
<div className='py-2'>
155124
<button
156-
className='flex items-center justify-center w-full gap-2 p-2 border rounded-md cursor-pointer border-neutral-500 bg-slate-100 hover:bg-slate-200'
125+
className='flex w-full cursor-pointer items-center justify-center gap-2 rounded-md border border-neutral-500 bg-slate-100 p-2 hover:bg-slate-200'
157126
onClick={() => {
158127
uploadFileForRequestNode.current.click();
159128
}}
160129
>
161-
<DocumentArrowUpIcon className='w-4 h-4 text-center' />
130+
<DocumentArrowUpIcon className='h-4 w-4 text-center' />
162131
Upload File
163132
{/* Ref: https://stackoverflow.com/questions/37457128/react-open-file-browser-on-click-a-div */}
164133
<div className='hidden'>
165134
<input type='file' id='file' ref={uploadFileForRequestNode} onChange={handleFileUpload} />
166135
</div>
167136
</button>
168-
{fileName != '' ? fileName : 'Choose a file to upload'}
137+
{nodeData.requestBody.body.name != '' ? nodeData.requestBody.body.name : 'Choose a file to upload'}
169138
</div>
170139
</div>
171140
)}

src/components/molecules/flow/nodes/RequestNode.js

Lines changed: 47 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,101 +6,69 @@ import { getInputType } from 'utils/common';
66
import { PlusIcon, TrashIcon } from '@heroicons/react/24/solid';
77
import { getDefaultValue } from 'utils/common';
88
import AddVariableModal from 'components/molecules/modals/flow/AddVariableModal';
9+
import useCanvasStore from 'stores/CanvasStore';
910

10-
function initialVariables(data) {
11-
if (data.variables != undefined) {
12-
return data.variables;
13-
}
14-
return {};
15-
}
11+
const RequestNode = ({ id, data }) => {
12+
const setRequestNodeUrl = useCanvasStore((state) => state.setRequestNodeUrl);
13+
const requestNodeAddVariable = useCanvasStore((state) => state.requestNodeAddVariable);
14+
const requestNodeDeleteVariable = useCanvasStore((state) => state.requestNodeDeleteVariable);
15+
const requestNodeChangeVariable = useCanvasStore((state) => state.requestNodeChangeVariable);
1616

17-
const RequestNode = ({ data }) => {
1817
const [variableDialogOpen, setVariableDialogOpen] = useState(false);
19-
const [variables, setVariables] = useState(initialVariables(data));
2018

2119
const handleAddVariable = (name, type) => {
22-
const newId = name;
23-
const newVar = {
24-
type: type,
25-
value: getDefaultValue(type),
26-
};
27-
setVariables((prevVariables) => {
28-
return { ...prevVariables, [newId]: newVar };
29-
});
20+
requestNodeAddVariable(id, name, type);
3021
};
3122

32-
const handleDeleteVariable = (event, id) => {
33-
setVariables((currentVariables) => {
34-
// eslint-disable-next-line no-unused-vars
35-
const { [id]: _, ...newVariables } = currentVariables;
36-
return newVariables;
37-
});
23+
const handleDeleteVariable = (event, vId) => {
24+
requestNodeDeleteVariable(id, vId);
3825
};
3926

40-
const handleVariableChange = (event, id) => {
41-
setVariables((currentVariables) => {
42-
const updateVar = {
43-
type: currentVariables[id].type,
44-
value: event.target.value,
45-
};
46-
return { ...currentVariables, [id]: updateVar };
47-
});
27+
const handleVariableChange = (event, vId) => {
28+
requestNodeChangeVariable(id, vId, event.target.value);
4829
};
4930

50-
// const handleBooleanChange = (event, id) => {
51-
// setVariables((currentVariables) => {
52-
// const updateVar = {
53-
// type: currentVariables[id].type,
54-
// value: event.target.checked,
55-
// };
56-
// return { ...currentVariables, [id]: updateVar };
57-
// });
58-
// };
59-
60-
useEffect(() => {
61-
data.variables = variables;
62-
}, [variables]);
63-
6431
const handleUrlInputChange = (event) => {
65-
data.url = event.target.value;
32+
setRequestNodeUrl(id, event.target.value);
6633
};
6734

6835
const renderVariables = () => {
6936
return (
7037
<>
71-
{Object.keys(variables).map((id) => (
72-
<div className='flex items-center justify-between pb-2' key={id}>
73-
<div className='flex items-center justify-between text-sm border rounded-md border-neutral-500 text-neutral-500 outline-0 focus:ring-0'>
74-
{variables[id].type === 'Boolean' ? (
75-
<select
76-
onChange={(e) => handleVariableChange(e, id)}
77-
name='boolean-val'
78-
className='nodrag h-9 w-full rounded-br-md rounded-tr-md p-2.5 px-1 '
79-
value={variables[id].value}
80-
>
81-
<option value='true'>True</option>
82-
<option value='false'>False</option>
83-
</select>
84-
) : (
85-
<input
86-
type={getInputType(variables[id].type)}
87-
className='nodrag nowheel block h-9 w-full rounded-bl-md rounded-tl-md p-2.5'
88-
name='variable-value'
89-
data-type={getInputType(variables[id].type)}
90-
onChange={(e) => handleVariableChange(e, id)}
91-
value={variables[id].value}
92-
/>
93-
)}
94-
<label>{id}</label>
95-
<div className='px-4 py-2 border-l rounded-br-md rounded-tr-md border-l-neutral-500'>
96-
{variables[id].type}
38+
{data.variables &&
39+
Object.keys(data.variables).map((id) => (
40+
<div className='flex items-center justify-between pb-2' key={id}>
41+
<div className='flex items-center justify-between rounded-md border border-neutral-500 text-sm text-neutral-500 outline-0 focus:ring-0'>
42+
{data.variables[id].type === 'Boolean' ? (
43+
<select
44+
onChange={(e) => handleVariableChange(e, id)}
45+
name='boolean-val'
46+
className='nodrag h-9 w-full rounded-br-md rounded-tr-md p-2.5 px-1 '
47+
value={data.variables[id].value}
48+
>
49+
<option value='true'>True</option>
50+
<option value='false'>False</option>
51+
</select>
52+
) : (
53+
<input
54+
type={getInputType(data.variables[id].type)}
55+
className='nodrag nowheel block h-9 w-full rounded-bl-md rounded-tl-md p-2.5'
56+
name='variable-value'
57+
data-type={getInputType(data.variables[id].type)}
58+
onChange={(e) => handleVariableChange(e, id)}
59+
value={data.variables[id].value}
60+
/>
61+
)}
62+
<label>{id}</label>
63+
<div className='rounded-br-md rounded-tr-md border-l border-l-neutral-500 px-4 py-2'>
64+
{data.variables[id].type}
65+
</div>
66+
</div>
67+
<div onClick={(e) => handleDeleteVariable(e, id)} className='p-2 text-neutral-500'>
68+
<TrashIcon className='h-4 w-4' />
9769
</div>
9870
</div>
99-
<div onClick={(e) => handleDeleteVariable(e, id)} className='p-2 text-neutral-500'>
100-
<TrashIcon className='w-4 h-4' />
101-
</div>
102-
</div>
103-
))}
71+
))}
10472
</>
10573
);
10674
};
@@ -124,15 +92,15 @@ const RequestNode = ({ data }) => {
12492
defaultValue={data.url ? data.url : ''}
12593
/>
12694
</div>
127-
<RequestBody nodeData={data} />
95+
<RequestBody nodeId={id} nodeData={data} />
12896
<div className='border-t border-neutral-300 bg-slate-100'>
12997
<div className='flex items-center justify-between px-2 py-4 font-medium'>
13098
<h3>Variables</h3>
13199
<button className='p-2' onClick={() => setVariableDialogOpen(true)}>
132-
<PlusIcon className='w-4 h-4' />
100+
<PlusIcon className='h-4 w-4' />
133101
</button>
134102
</div>
135-
<div className='p-2 pt-4 border-t border-neutral-300 bg-slate-50'>{renderVariables()}</div>
103+
<div className='border-t border-neutral-300 bg-slate-50 p-2 pt-4'>{renderVariables()}</div>
136104
</div>
137105
</div>
138106
<AddVariableModal

0 commit comments

Comments
 (0)