@visns-studio/visns-components 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1058 @@
1
+ import React, { useState, useEffect } from 'react';
2
+ import { motion } from 'framer-motion';
3
+ import { toast } from 'react-toastify';
4
+ import moment from 'moment';
5
+ import parse from 'html-react-parser';
6
+ import _ from 'lodash';
7
+ import { CircleX } from 'akar-icons';
8
+
9
+ import CustomFetch from './visns-fetch';
10
+ import Field from './visns-field';
11
+
12
+ function Form(props) {
13
+ const {
14
+ formType,
15
+ formSettings,
16
+ columnId,
17
+ closeModal,
18
+ fetchTable,
19
+ childDropdownCallback,
20
+ updateForm,
21
+ } = props;
22
+ const [inputClass, setInputClass] = useState({});
23
+ const [formData, setFormData] = useState({});
24
+ const [fetchTrigger, setFetchTrigger] = useState(false);
25
+ const [uploadProgress, setUploadProgress] = useState(0);
26
+
27
+ const handleChange = (e) => {
28
+ let id = e.target.dataset.hasOwnProperty('jsonkey')
29
+ ? e.target.dataset.jsonkey
30
+ : e.target.dataset.name;
31
+ let value = e.target.value;
32
+ let _dataset;
33
+
34
+ if (e.target[e.target.selectedIndex]) {
35
+ _dataset = e.target[e.target.selectedIndex].dataset;
36
+ }
37
+
38
+ if (e.target.files) {
39
+ setFormData((prevState) => ({
40
+ ...prevState,
41
+ [id]: e.target.files[0],
42
+ }));
43
+ } else {
44
+ setFormData((prevState) => ({
45
+ ...prevState,
46
+ [id]: value,
47
+ }));
48
+ }
49
+
50
+ if (!_.isEmpty(_dataset)) {
51
+ Object.keys(_dataset).forEach(function (c) {
52
+ if (formData.hasOwnProperty(c)) {
53
+ setFormData((prevState) => ({
54
+ ...prevState,
55
+ [c]: _dataset[c],
56
+ }));
57
+ }
58
+ });
59
+ }
60
+
61
+ if (e.target.dataset.hasOwnProperty('show')) {
62
+ let _show =
63
+ e.target.dataset.show !== ''
64
+ ? JSON.parse(e.target.dataset.show)
65
+ : [];
66
+
67
+ if (_show.length > 0) {
68
+ _show.forEach((showObject) => {
69
+ if (
70
+ showObject.hasOwnProperty('id') &&
71
+ showObject.hasOwnProperty('value')
72
+ ) {
73
+ if (
74
+ showObject.id !== '' &&
75
+ showObject.value.length > 0
76
+ ) {
77
+ let _fields = formSettings.fields;
78
+ let _counter;
79
+ _fields.forEach((a, b) => {
80
+ if (a.id === showObject.id) {
81
+ _counter = b;
82
+ }
83
+ });
84
+
85
+ let _valueChecker = parseInt(e.target.value);
86
+
87
+ if (
88
+ _counter >= 0 &&
89
+ showObject.value.includes(_valueChecker) ===
90
+ true
91
+ ) {
92
+ _fields[_counter].hide = false;
93
+ } else {
94
+ _fields[_counter].hide = true;
95
+ }
96
+
97
+ updateForm((prevState) => ({
98
+ ...prevState,
99
+ fields: _fields,
100
+ }));
101
+ }
102
+ }
103
+ });
104
+ }
105
+ }
106
+ };
107
+
108
+ const handleChangeNumberFormat = (value, id) => {
109
+ setFormData((prevState) => ({
110
+ ...prevState,
111
+ [id]: value.value,
112
+ }));
113
+ };
114
+
115
+ const handleChangeDate = (date, id) => {
116
+ setFormData((prevState) => ({
117
+ ...prevState,
118
+ [id]: date,
119
+ }));
120
+ };
121
+
122
+ const handleChangeRicheditor = (e, value, id) => {
123
+ if (e) {
124
+ setFormData((prevState) => ({
125
+ ...prevState,
126
+ [id]: value,
127
+ }));
128
+ }
129
+ };
130
+
131
+ const handleChangeSelect = (inputValue, action, id) => {
132
+ setFormData((prevState) => ({
133
+ ...prevState,
134
+ [id]: inputValue,
135
+ }));
136
+
137
+ Object.keys(inputValue).forEach(function (a) {
138
+ if (formData.hasOwnProperty(a)) {
139
+ setFormData((prevState) => ({
140
+ ...prevState,
141
+ [a]: inputValue[a],
142
+ }));
143
+ }
144
+ });
145
+ };
146
+
147
+ const handleChangeToggle = (e) => {
148
+ if (e) {
149
+ let id = e.target.dataset.hasOwnProperty('jsonkey')
150
+ ? e.target.dataset.jsonkey
151
+ : e.target.dataset.name;
152
+
153
+ setFormData((prevState) => ({
154
+ ...prevState,
155
+ [id]: e.target.checked === true ? true : false,
156
+ }));
157
+
158
+ if (e.target.dataset.hasOwnProperty('show')) {
159
+ let _show =
160
+ e.target.dataset.show !== ''
161
+ ? JSON.parse(e.target.dataset.show)
162
+ : [];
163
+
164
+ if (_show.length > 0) {
165
+ _show.forEach((showObject) => {
166
+ if (
167
+ showObject.hasOwnProperty('id') &&
168
+ showObject.hasOwnProperty('value')
169
+ ) {
170
+ if (
171
+ showObject.id !== '' &&
172
+ showObject.value.length > 0
173
+ ) {
174
+ let _fields = formSettings.fields;
175
+ let _counter;
176
+ _fields.forEach((a, b) => {
177
+ if (a.id === showObject.id) {
178
+ _counter = b;
179
+ }
180
+ });
181
+
182
+ let _valueChecker =
183
+ e.target.checked === true ? true : false;
184
+
185
+ if (
186
+ _counter >= 0 &&
187
+ showObject.value.includes(_valueChecker) ===
188
+ true
189
+ ) {
190
+ _fields[_counter].hide = false;
191
+ } else {
192
+ _fields[_counter].hide = true;
193
+ }
194
+
195
+ updateForm((prevState) => ({
196
+ ...prevState,
197
+ fields: _fields,
198
+ }));
199
+ }
200
+ }
201
+ });
202
+ }
203
+ }
204
+ }
205
+ };
206
+
207
+ const autocompleteSelect = (place) => {
208
+ let address1 = place.address
209
+ ? place.address + ' ' + place.text
210
+ : place.text;
211
+ let address2 = '';
212
+ let suburb = '';
213
+ let postcode = '';
214
+ let state = '';
215
+ let country = '';
216
+
217
+ if (place.context[1]) {
218
+ suburb = place.context[1].text;
219
+ }
220
+
221
+ if (place.context[0]) {
222
+ postcode = place.context[0].text;
223
+ }
224
+
225
+ if (place.context.length === 5) {
226
+ if (place.context[3]) {
227
+ state = place.context[3].text;
228
+ }
229
+
230
+ if (place.context[4]) {
231
+ country = place.context[4].text;
232
+ }
233
+ } else {
234
+ if (place.context[2]) {
235
+ state = place.context[2].text;
236
+ }
237
+
238
+ if (place.context[3]) {
239
+ country = place.context[3].text;
240
+ }
241
+ }
242
+
243
+ setFormData((prevState) => ({
244
+ ...prevState,
245
+ address1: address1,
246
+ address2: address2,
247
+ suburb: suburb,
248
+ postcode: postcode,
249
+ state: state,
250
+ country: country,
251
+ }));
252
+ };
253
+
254
+ const handleSubmit = (e) => {
255
+ e.preventDefault();
256
+ let fields = formSettings.fields;
257
+ let _inputClass = inputClass;
258
+ let validation = '';
259
+
260
+ if (formType === 'create' || formType === 'update') {
261
+ fields.forEach((item) => {
262
+ if (item.required === true) {
263
+ if (formData[item.id] === '') {
264
+ if (
265
+ item.hasOwnProperty('required_check') &&
266
+ item.required_check !== ''
267
+ ) {
268
+ if (formData[item.required_check] === '') {
269
+ if (
270
+ !validation.includes(
271
+ item.label +
272
+ ' is a required field.<br/>'
273
+ )
274
+ ) {
275
+ validation +=
276
+ item.label +
277
+ ' is a required field.<br/>';
278
+ }
279
+
280
+ _inputClass[item.id] = 'inputError';
281
+ }
282
+ } else {
283
+ if (
284
+ !validation.includes(
285
+ item.label + ' is a required field.<br/>'
286
+ )
287
+ ) {
288
+ validation +=
289
+ item.label + ' is a required field.<br/>';
290
+ }
291
+ _inputClass[item.id] = 'inputError';
292
+ }
293
+ } else {
294
+ if (
295
+ item.hasOwnProperty('required_check') &&
296
+ item.required_check !== ''
297
+ ) {
298
+ _inputClass[item.required_check] = '';
299
+ }
300
+
301
+ _inputClass[item.id] = '';
302
+ }
303
+ } else {
304
+ if (
305
+ item.hasOwnProperty('required_rely') &&
306
+ item.required_rely !== ''
307
+ ) {
308
+ if (formData[item.required_rely] === '') {
309
+ _inputClass[item.id] = '';
310
+ } else {
311
+ if (formData[item.id] === '') {
312
+ if (
313
+ !validation.includes(
314
+ item.label +
315
+ ' is a required field.<br/>'
316
+ )
317
+ ) {
318
+ validation +=
319
+ item.label +
320
+ ' is a required field.<br/>';
321
+ }
322
+
323
+ _inputClass[item.id] = 'inputError';
324
+ } else {
325
+ _inputClass[item.id] = '';
326
+ }
327
+ }
328
+ } else {
329
+ _inputClass[item.id] = '';
330
+ }
331
+ }
332
+ });
333
+
334
+ setInputClass(_inputClass);
335
+ }
336
+
337
+ if (validation !== '') {
338
+ toast.error(<div>{parse(validation)}</div>);
339
+ } else {
340
+ let url;
341
+ let method;
342
+ let fileUpload = false;
343
+ let fileKey = '';
344
+
345
+ switch (formType) {
346
+ case 'create':
347
+ url = formSettings.url;
348
+ method = 'POST';
349
+ break;
350
+ case 'update':
351
+ url =
352
+ formSettings.url +
353
+ '/' +
354
+ formData[formSettings.primaryKey];
355
+ method = 'PUT';
356
+ break;
357
+ case 'delete':
358
+ url =
359
+ formSettings.url +
360
+ '/' +
361
+ formData[formSettings.primaryKey];
362
+ method = 'DELETE';
363
+ break;
364
+ default:
365
+ url = '';
366
+ method = '';
367
+ break;
368
+ }
369
+
370
+ if (!_.isEmpty(formData)) {
371
+ Object.keys(formData).forEach((item) => {
372
+ if (formData[item] !== undefined) {
373
+ if (item === 'file') {
374
+ fileUpload = true;
375
+ fileKey = item;
376
+ }
377
+
378
+ if (
379
+ formData[item] instanceof File &&
380
+ formData[item] !== null
381
+ ) {
382
+ fileUpload = true;
383
+ fileKey = item;
384
+ }
385
+ }
386
+ });
387
+ }
388
+
389
+ if (fileUpload === true) {
390
+ Vapor.store(formData[fileKey], {
391
+ progress: (progress) => {
392
+ setUploadProgress(progress * 100);
393
+ },
394
+ }).then((response) => {
395
+ formData['uuid'] = response.uuid;
396
+ formData['key'] = response.key;
397
+ formData['bucket'] = response.bucket;
398
+ formData['filename'] = formData[fileKey].name;
399
+ formData['filesize'] = formData[fileKey].size;
400
+ formData['extension'] = response.extension;
401
+
402
+ CustomFetch(url, method, formData, function (result) {
403
+ if (result.error === '') {
404
+ fetchTable();
405
+ closeModal();
406
+
407
+ toast.success(
408
+ 'Entry successfully saved into the system.'
409
+ );
410
+
411
+ if (
412
+ result.hasOwnProperty('myob') &&
413
+ result.myob.hasOwnProperty('id') &&
414
+ result.myob.hasOwnProperty('url')
415
+ ) {
416
+ CustomFetch(
417
+ '/ajax' + result.myob.url,
418
+ 'POST',
419
+ {
420
+ id: result.myob.id,
421
+ },
422
+ function (result) {
423
+ if (result.error === '') {
424
+ toast.success(
425
+ 'Entry successfully saved into MYOB.'
426
+ );
427
+ } else {
428
+ toast.error(
429
+ result.error !== undefined
430
+ ? result.error
431
+ : ''
432
+ );
433
+ }
434
+ }
435
+ );
436
+ }
437
+ } else {
438
+ toast.error(
439
+ result.error !== undefined ? result.error : ''
440
+ );
441
+ }
442
+ });
443
+ });
444
+ } else {
445
+ CustomFetch(url, method, formData, function (result) {
446
+ if (result.error === '') {
447
+ fetchTable();
448
+ closeModal();
449
+
450
+ toast.success(
451
+ 'Entry successfully saved into the system.'
452
+ );
453
+
454
+ if (
455
+ result.hasOwnProperty('myob') &&
456
+ result.myob.hasOwnProperty('id') &&
457
+ result.myob.hasOwnProperty('url')
458
+ ) {
459
+ CustomFetch(
460
+ '/ajax' + result.myob.url,
461
+ 'POST',
462
+ {
463
+ id: result.myob.id,
464
+ },
465
+ function (result) {
466
+ if (result.error === '') {
467
+ toast.success(
468
+ 'Entry successfully saved into MYOB.'
469
+ );
470
+ } else {
471
+ toast.error(
472
+ result.error !== undefined
473
+ ? result.error
474
+ : ''
475
+ );
476
+ }
477
+ }
478
+ );
479
+ }
480
+ } else {
481
+ toast.error(
482
+ result.error !== undefined ? result.error : ''
483
+ );
484
+ }
485
+ });
486
+ }
487
+ }
488
+ };
489
+
490
+ useEffect(() => {
491
+ let _formData = formData;
492
+ let _date = false;
493
+
494
+ if (formSettings.fields.length > 0) {
495
+ formSettings.fields.forEach((item) => {
496
+ if (item.hasOwnProperty('value') && item.value !== '') {
497
+ if (
498
+ item.type === 'date' ||
499
+ item.type === 'datetime' ||
500
+ item.type === 'time'
501
+ ) {
502
+ _formData[item.id] =
503
+ item.value instanceof Date
504
+ ? item.value
505
+ : moment(item.value).toDate();
506
+ } else {
507
+ _formData[item.id] = item.value;
508
+ }
509
+ } else {
510
+ _formData[item.id] = '';
511
+ }
512
+ });
513
+ }
514
+
515
+ if (formType !== 'create') {
516
+ if (columnId > 0) {
517
+ CustomFetch(
518
+ formSettings.url + '/' + columnId,
519
+ 'GET',
520
+ {},
521
+ function (result) {
522
+ result = result.hasOwnProperty('data')
523
+ ? result.data
524
+ : result;
525
+
526
+ Object.keys(formData).forEach((item) => {
527
+ if (result[item] === undefined) {
528
+ let tempRes = '';
529
+ let tempResKey = '';
530
+
531
+ formSettings.fields.forEach((a) => {
532
+ if (a.id === item) {
533
+ if (a.hasOwnProperty('relation')) {
534
+ let tempRes = result;
535
+
536
+ a.relation.forEach((c) => {
537
+ tempRes = tempRes[c];
538
+ });
539
+
540
+ if (
541
+ a.type ===
542
+ 'multi-dropdown-ajax' ||
543
+ a.type === 'async-dropdown-ajax'
544
+ ) {
545
+ if (
546
+ Array.isArray(tempRes) ===
547
+ true
548
+ ) {
549
+ let _tr = [];
550
+
551
+ tempRes.forEach((a) => {
552
+ _tr.push({
553
+ value: a.name,
554
+ label: a.name,
555
+ });
556
+ });
557
+
558
+ tempRes = _tr;
559
+ }
560
+ }
561
+
562
+ _formData[item] = tempRes
563
+ ? a.hasOwnProperty(
564
+ 'relation_from'
565
+ )
566
+ ? Array.isArray(tempRes) ===
567
+ true
568
+ ? tempRes[0] !==
569
+ undefined
570
+ ? a.type ===
571
+ 'multi-dropdown-ajax'
572
+ ? tempRes
573
+ : tempRes[0][
574
+ a
575
+ .relation_from
576
+ ]
577
+ : ''
578
+ : tempRes[
579
+ a.relation_from
580
+ ]
581
+ : tempRes[a.id]
582
+ : '';
583
+ }
584
+ }
585
+
586
+ if (a.hasOwnProperty('relation')) {
587
+ if (
588
+ a.type === 'multi-dropdown-ajax' ||
589
+ a.type === 'async-dropdown-ajax'
590
+ ) {
591
+ if (a.id === item) {
592
+ if (
593
+ Array.isArray(
594
+ a.relation
595
+ ) === true
596
+ ) {
597
+ switch (a.relation.length) {
598
+ case 1:
599
+ tempRes = {
600
+ value: result[
601
+ a
602
+ .relation[0]
603
+ ].id,
604
+ label: result[
605
+ a
606
+ .relation[0]
607
+ ][
608
+ a
609
+ .relationName
610
+ ],
611
+ };
612
+ break;
613
+ case 2:
614
+ tempRes = {
615
+ value: result[
616
+ a
617
+ .relation[0]
618
+ ][a.relation[1]]
619
+ .id,
620
+ label: result[
621
+ a
622
+ .relation[0]
623
+ ][
624
+ a
625
+ .relation[1]
626
+ ][
627
+ a
628
+ .relationName
629
+ ],
630
+ };
631
+ break;
632
+ case 3:
633
+ tempRes = {
634
+ value: result[
635
+ a
636
+ .relation[0]
637
+ ][
638
+ a
639
+ .relation[1]
640
+ ][a.relation[2]]
641
+ .id,
642
+ label: result[
643
+ a
644
+ .relation[0]
645
+ ][
646
+ a
647
+ .relation[1]
648
+ ][
649
+ a
650
+ .relation[2]
651
+ ][
652
+ a
653
+ .relationName
654
+ ],
655
+ };
656
+ break;
657
+ case 4:
658
+ tempRes = {
659
+ value: result[
660
+ a
661
+ .relation[0]
662
+ ][
663
+ a
664
+ .relation[1]
665
+ ][
666
+ a
667
+ .relation[2]
668
+ ][a.relation[3]]
669
+ .id,
670
+ label: result[
671
+ a
672
+ .relation[0]
673
+ ][
674
+ a
675
+ .relation[1]
676
+ ][
677
+ a
678
+ .relation[2]
679
+ ][
680
+ a
681
+ .relation[3]
682
+ ][
683
+ a
684
+ .relationName
685
+ ],
686
+ };
687
+ break;
688
+ }
689
+ } else {
690
+ tempRes = {
691
+ value: result[
692
+ a.relation
693
+ ].id,
694
+ label: result[
695
+ a.relation
696
+ ][a.relationName],
697
+ };
698
+ }
699
+
700
+ tempResKey = a.id;
701
+ }
702
+ }
703
+ }
704
+ });
705
+
706
+ if (item === tempResKey) {
707
+ if (
708
+ tempRes &&
709
+ tempRes.hasOwnProperty('value') &&
710
+ tempRes.hasOwnProperty('label')
711
+ ) {
712
+ _formData[item] = tempRes;
713
+ }
714
+ }
715
+ } else {
716
+ let tempRes = '';
717
+ let tempResKey = '';
718
+
719
+ formSettings.fields.forEach((a) => {
720
+ if (a.id === item) {
721
+ if (
722
+ a.type === 'date' ||
723
+ a.type === 'datetime' ||
724
+ a.type === 'time'
725
+ ) {
726
+ _date = true;
727
+ }
728
+ }
729
+
730
+ if (a.hasOwnProperty('relation')) {
731
+ if (
732
+ a.type === 'multi-dropdown-ajax' ||
733
+ a.type === 'async-dropdown-ajax'
734
+ ) {
735
+ if (a.id === item) {
736
+ if (
737
+ Array.isArray(
738
+ a.relation
739
+ ) === true
740
+ ) {
741
+ switch (a.relation.length) {
742
+ case 1:
743
+ tempRes = {
744
+ value: result[
745
+ a
746
+ .relation[0]
747
+ ].id,
748
+ label: result[
749
+ a
750
+ .relation[0]
751
+ ][
752
+ a
753
+ .relationName
754
+ ],
755
+ };
756
+ break;
757
+ case 2:
758
+ tempRes = {
759
+ value: result[
760
+ a
761
+ .relation[0]
762
+ ][a.relation[1]]
763
+ .id,
764
+ label: result[
765
+ a
766
+ .relation[0]
767
+ ][
768
+ a
769
+ .relation[1]
770
+ ][
771
+ a
772
+ .relationName
773
+ ],
774
+ };
775
+ break;
776
+ case 3:
777
+ tempRes = {
778
+ value: result[
779
+ a
780
+ .relation[0]
781
+ ][
782
+ a
783
+ .relation[1]
784
+ ][a.relation[2]]
785
+ .id,
786
+ label: result[
787
+ a
788
+ .relation[0]
789
+ ][
790
+ a
791
+ .relation[1]
792
+ ][
793
+ a
794
+ .relation[2]
795
+ ][
796
+ a
797
+ .relationName
798
+ ],
799
+ };
800
+ break;
801
+ case 4:
802
+ tempRes = {
803
+ value: result[
804
+ a
805
+ .relation[0]
806
+ ][
807
+ a
808
+ .relation[1]
809
+ ][
810
+ a
811
+ .relation[2]
812
+ ][a.relation[3]]
813
+ .id,
814
+ label: result[
815
+ a
816
+ .relation[0]
817
+ ][
818
+ a
819
+ .relation[1]
820
+ ][
821
+ a
822
+ .relation[2]
823
+ ][
824
+ a
825
+ .relation[3]
826
+ ][
827
+ a
828
+ .relationName
829
+ ],
830
+ };
831
+ break;
832
+ }
833
+ } else {
834
+ tempRes = {
835
+ value: result[
836
+ a.relation
837
+ ].id,
838
+ label: result[
839
+ a.relation
840
+ ][a.relationName],
841
+ };
842
+ }
843
+ tempResKey = a.id;
844
+ }
845
+ }
846
+ }
847
+ });
848
+
849
+ if (_date === true) {
850
+ if (result[item] === null) {
851
+ result[item] = '';
852
+ }
853
+
854
+ _formData[item] =
855
+ result[item] !== ''
856
+ ? result[item] instanceof Date
857
+ ? result[item]
858
+ : moment(result[item]).toDate()
859
+ : result[item];
860
+
861
+ _date = false;
862
+ } else {
863
+ _formData[item] = result[item];
864
+ }
865
+
866
+ if (item === tempResKey) {
867
+ if (
868
+ tempRes &&
869
+ tempRes.hasOwnProperty('value') &&
870
+ tempRes.hasOwnProperty('label')
871
+ ) {
872
+ _formData[item] = tempRes;
873
+ }
874
+ }
875
+ }
876
+ });
877
+
878
+ setFormData((prevState) => ({
879
+ ...prevState,
880
+ ..._formData,
881
+ }));
882
+
883
+ setFetchTrigger(fetchTrigger === true ? false : true);
884
+ }
885
+ );
886
+ }
887
+ } else {
888
+ setFormData((prevState) => ({
889
+ ...prevState,
890
+ ..._formData,
891
+ }));
892
+ }
893
+ }, []);
894
+
895
+ useEffect(() => {
896
+ formSettings.fields.forEach((a) => {
897
+ if (a.hasOwnProperty('show') && a.show.length > 0) {
898
+ let _show = a.show;
899
+ if (_show.length > 0) {
900
+ _show.forEach((showObject) => {
901
+ if (
902
+ showObject.hasOwnProperty('id') &&
903
+ showObject.hasOwnProperty('value')
904
+ ) {
905
+ if (
906
+ showObject.id !== '' &&
907
+ showObject.value.length > 0
908
+ ) {
909
+ let _fields = formSettings.fields;
910
+ let _counter;
911
+ _fields.forEach((a, b) => {
912
+ if (a.id === showObject.id) {
913
+ _counter = b;
914
+ }
915
+ });
916
+
917
+ let _valueChecker = formData[a.id];
918
+
919
+ if (
920
+ _counter >= 0 &&
921
+ showObject.value.includes(_valueChecker) ===
922
+ true
923
+ ) {
924
+ _fields[_counter].hide = false;
925
+ } else {
926
+ _fields[_counter].hide = true;
927
+ }
928
+
929
+ updateForm((prevState) => ({
930
+ ...prevState,
931
+ fields: _fields,
932
+ }));
933
+ }
934
+ }
935
+ });
936
+ }
937
+ }
938
+ });
939
+ }, [fetchTrigger]);
940
+
941
+ return (
942
+ <div className="modalwrap top--modal">
943
+ <div className="modal">
944
+ <div className="modal__header">
945
+ <h1>{formSettings[formType].title}</h1>
946
+ <button className="modal__close" onClick={closeModal}>
947
+ <CircleX strokeWidth={1} size={24} />
948
+ </button>
949
+ </div>
950
+ <div className="modal__content">
951
+ <div className="formcontainer">
952
+ <form className="modalForm" onSubmit={handleSubmit}>
953
+ {formSettings.fields.map((item, index) =>
954
+ item.hasOwnProperty('hide') === false ? (
955
+ item.hasOwnProperty('createOnly') &&
956
+ item.createOnly === true &&
957
+ formType === 'update' ? null : (
958
+ <Field
959
+ settings={item}
960
+ key={
961
+ index +
962
+ '-fields-' +
963
+ (item.hasOwnProperty('key')
964
+ ? item.id + '-' + item.key
965
+ : item.id)
966
+ }
967
+ inputValue={
968
+ item.hasOwnProperty('key')
969
+ ? formData[item.key]
970
+ : formData[item.id]
971
+ }
972
+ inputClass={inputClass}
973
+ onChange={handleChange}
974
+ onChangeDate={handleChangeDate}
975
+ onChangeSelect={handleChangeSelect}
976
+ onChangeRicheditor={
977
+ handleChangeRicheditor
978
+ }
979
+ onChangeToggle={handleChangeToggle}
980
+ onChangeNumberFormat={
981
+ handleChangeNumberFormat
982
+ }
983
+ autocompleteCallback={
984
+ autocompleteSelect
985
+ }
986
+ childDropdownCallback={
987
+ childDropdownCallback
988
+ }
989
+ setFormData={setFormData}
990
+ />
991
+ )
992
+ ) : item.hide === false ? (
993
+ item.hasOwnProperty('createOnly') &&
994
+ item.createOnly === true &&
995
+ formType === 'update' ? null : (
996
+ <Field
997
+ settings={item}
998
+ key={
999
+ index +
1000
+ '-fields-' +
1001
+ (item.hasOwnProperty('key')
1002
+ ? item.id + '-' + item.key
1003
+ : item.id)
1004
+ }
1005
+ inputValue={
1006
+ item.hasOwnProperty('key')
1007
+ ? formData[item.key]
1008
+ : formData[item.id]
1009
+ }
1010
+ inputClass={inputClass}
1011
+ onChange={handleChange}
1012
+ onChangeDate={handleChangeDate}
1013
+ onChangeSelect={handleChangeSelect}
1014
+ onChangeRicheditor={
1015
+ handleChangeRicheditor
1016
+ }
1017
+ onChangeNumberFormat={
1018
+ handleChangeNumberFormat
1019
+ }
1020
+ autocompleteCallback={
1021
+ autocompleteSelect
1022
+ }
1023
+ childDropdownCallback={
1024
+ childDropdownCallback
1025
+ }
1026
+ uploadProgress={uploadProgress}
1027
+ setFormData={setFormData}
1028
+ />
1029
+ )
1030
+ ) : null
1031
+ )}
1032
+ <div className="formItem fwItem">
1033
+ <motion.div
1034
+ className="progress__bar"
1035
+ initial={{ width: '0%' }}
1036
+ animate={{
1037
+ width: uploadProgress + '%',
1038
+ }}
1039
+ transition={{
1040
+ duration: 3,
1041
+ ease: [0.165, 0.84, 0.44, 1.0],
1042
+ }}
1043
+ />
1044
+ </div>
1045
+ <div className="formItem fwItem lastItem">
1046
+ <button className="btn modalsave" type="submit">
1047
+ Save
1048
+ </button>
1049
+ </div>
1050
+ </form>
1051
+ </div>
1052
+ </div>
1053
+ </div>
1054
+ </div>
1055
+ );
1056
+ }
1057
+
1058
+ export default Form;