diff --git a/src/components/FormField/components/FileSelect/index.js b/src/components/FormField/components/FileSelect/index.js index 031b6de..659d8a2 100644 --- a/src/components/FormField/components/FileSelect/index.js +++ b/src/components/FormField/components/FileSelect/index.js @@ -1,121 +1,91 @@ -import React, { useCallback, useEffect, useState } from 'react'; +import React, { useCallback, useEffect, useRef, useState } from 'react'; import { classNames } from 'primereact/utils'; +import { __ } from '@wordpress/i18n'; +import { isEmpty, pluck } from 'ramda'; // components import { ListBox } from 'primereact/listbox'; import { Button } from 'primereact/button'; -import { __ } from '@wordpress/i18n'; +import CompanyDocumentsService from '../../../../service/company-documents-service'; const FileSelect = ({ fieldName, label, + setDataFn, register, errors, defaultValue, config = {}, infoText = null, disabled = false, - options = [] + options = [], + sourceId = 0, + source = 'DOCUMENT', + saveFormCallback }) => { - const [stateFieldData, setStateFieldData] = useState([]); - const [selectedUnconfirmed, setSelectedUnconfirmed] = useState(null); - const cities = [ - { - label: 'Company files', - code: 'company-files', - items: [ - { - 'id': 589, - 'name': 'pdf-3-test.pdf', - 'filePath': 'https://mementoresources.s3.eu-west-1.amazonaws.com/gepafin/staging/call/%2Fapplication/%2Fpdf-1-test_8432561.pdf', - 'type': 'DOCUMENT', - 'source': 'APPLICATION', - 'sourceId': 0, - 'createdDate': '2025-02-21T15:33:52.767089176', - 'updatedDate': '2025-02-21T15:33:52.767109885', - 'documentAttachmentId': null - }, - { - 'id': 569, - 'name': 'pdf-2-test.pdf', - 'filePath': 'https://mementoresources.s3.eu-west-1.amazonaws.com/gepafin/staging/call/%2Fapplication/%2Fpdf-1-test_8432561.pdf', - 'type': 'DOCUMENT', - 'source': 'APPLICATION', - 'sourceId': 0, - 'createdDate': '2025-02-21T15:33:52.767089176', - 'updatedDate': '2025-02-21T15:33:52.767109885', - 'documentAttachmentId': null - }, - { - 'id': 519, - 'name': 'pdf-1-test.pdf', - 'filePath': 'https://mementoresources.s3.eu-west-1.amazonaws.com/gepafin/staging/call/%2Fapplication/%2Fpdf-1-test_8432561.pdf', - 'type': 'DOCUMENT', - 'source': 'APPLICATION', - 'sourceId': 0, - 'createdDate': '2025-02-21T15:33:52.767089176', - 'updatedDate': '2025-02-21T15:33:52.767109885', - 'documentAttachmentId': null - } - ] - }, - { - label: 'Personal files', - code: 'personal-files', - items: [ - { - 'id': 179, - 'name': 'pdf-13-test.pdf', - 'filePath': 'https://mementoresources.s3.eu-west-1.amazonaws.com/gepafin/staging/call/%2Fapplication/%2Fpdf-1-test_8432561.pdf', - 'type': 'DOCUMENT', - 'source': 'APPLICATION', - 'sourceId': 0, - 'createdDate': '2025-02-21T15:33:52.767089176', - 'updatedDate': '2025-02-21T15:33:52.767109885', - 'documentAttachmentId': null - }, - { - 'id': 169, - 'name': 'pdf-12-test.pdf', - 'filePath': 'https://mementoresources.s3.eu-west-1.amazonaws.com/gepafin/staging/call/%2Fapplication/%2Fpdf-1-test_8432561.pdf', - 'type': 'DOCUMENT', - 'source': 'APPLICATION', - 'sourceId': 0, - 'createdDate': '2025-02-21T15:33:52.767089176', - 'updatedDate': '2025-02-21T15:33:52.767109885', - 'documentAttachmentId': null - }, - { - 'id': 129, - 'name': 'pdf-11-test.pdf', - 'filePath': 'https://mementoresources.s3.eu-west-1.amazonaws.com/gepafin/staging/call/%2Fapplication/%2Fpdf-1-test_8432561.pdf', - 'type': 'DOCUMENT', - 'source': 'APPLICATION', - 'sourceId': 0, - 'createdDate': '2025-02-21T15:33:52.767089176', - 'updatedDate': '2025-02-21T15:33:52.767109885', - 'documentAttachmentId': null - } - ] - } - ]; + //const [stateFieldData, setStateFieldData] = useState([]); + const stateFieldData = useRef([]); + const [selectedUnconfirmed, setSelectedUnconfirmed] = useState([]); + const [optionsTransformed, setOptionsTransformed] = useState([]); + const [loading, setLoading] = useState(false); const attachSelectedFiles = useCallback(() => { - setStateFieldData([...stateFieldData, ...selectedUnconfirmed]); + const existingIds = pluck('id', stateFieldData.current); + const selectedToBeAdded = selectedUnconfirmed.filter(o => !existingIds.includes(o.id)); setSelectedUnconfirmed([]); - }, [selectedUnconfirmed, stateFieldData]) + + setLoading(true); + selectedToBeAdded.map(o => { + CompanyDocumentsService.attachCompanyDocumentToAppl(o.id, callback, errCallback, [ + ['applicationId', sourceId], + ['documentType', source] + ]) + }); + }, [selectedUnconfirmed]); + + const callback = (resp) => { + if (resp.status === 'SUCCESS') { + stateFieldData.current = [...stateFieldData.current, resp.data]; + setDataFn(fieldName, stateFieldData.current, { shouldValidate: true }); + saveFormCallback(); + } + setLoading(false); + } + + const errCallback = () => { + setLoading(false); + } useEffect(() => { console.log('selectedUnconfirmed', selectedUnconfirmed) }, [selectedUnconfirmed]); useEffect(() => { - setStateFieldData(defaultValue); + stateFieldData.current = defaultValue; register(fieldName, config) }, []); useEffect(() => { - setStateFieldData(defaultValue); + if (!isEmpty(options)) { + const optionsDefault = [ + { + label: __('Documenti dell\'Azienda', 'gepafin'), + code: 'COMPANY_DOCUMENT', + items: options.filter(o => o.type === 'COMPANY_DOCUMENT') + }, + { + label: __('Documenti del Rappresentante Legale', 'gepafin'), + code: 'PERSONAL_DOCUMENT', + items: options.filter(o => o.type === 'PERSONAL_DOCUMENT') + } + ]; + setOptionsTransformed(optionsDefault); + } + }, [options]); + + useEffect(() => { + console.log('defaultValue', defaultValue); + stateFieldData.current = defaultValue; }, [defaultValue]); return ( @@ -129,7 +99,7 @@ const FileSelect = ({ setSelectedUnconfirmed(e.value)} - options={cities} + options={optionsTransformed} optionLabel="name" optionGroupLabel="label" optionGroupChildren="items" @@ -137,6 +107,7 @@ const FileSelect = ({ listStyle={{ maxHeight: '130px' }}/>