@@ -3,24 +3,14 @@ import { PropTypes } from 'prop-types';
33import { EllipsisVerticalIcon } from '@heroicons/react/24/solid' ;
44import { DocumentArrowUpIcon } from '@heroicons/react/24/outline' ;
55import { 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 ) }
0 commit comments