diff --git a/src/pages/DomandaEditPreInstructor/components/EvaluationExtraFiles/components/CompanyDocsTable/index.js b/src/pages/DomandaEditPreInstructor/components/EvaluationExtraFiles/components/CompanyDocsTable/index.js new file mode 100644 index 0000000..a708c24 --- /dev/null +++ b/src/pages/DomandaEditPreInstructor/components/EvaluationExtraFiles/components/CompanyDocsTable/index.js @@ -0,0 +1,223 @@ +import React, { useState, useEffect, useRef } from 'react'; +import { __ } from '@wordpress/i18n'; +import { head, is, uniq } from 'ramda'; + +// store +import { useStoreValue } from '../../../../../../store'; + +// tools +import getBandoSeverity from '../../../../../../helpers/getBandoSeverity'; +import getBandoLabel from '../../../../../../helpers/getBandoLabel'; +import getDateFromISOstring from '../../../../../../helpers/getDateFromISOstring'; + +// api +import CompanyDocumentsService from '../../../../../../service/company-documents-service'; +import ApplicationService from '../../../../../../service/application-service'; + +// components +import { FilterMatchMode, FilterOperator } from 'primereact/api'; +import { DataTable } from 'primereact/datatable'; +import { Column } from 'primereact/column'; +import { Dropdown } from 'primereact/dropdown'; +import { Button } from 'primereact/button'; +import { Calendar } from 'primereact/calendar'; +import { Tag } from 'primereact/tag'; +import { Toast } from 'primereact/toast'; +import ProperBandoLabel from '../../../../../../components/ProperBandoLabel'; + +const CompanyDocsTable = ({ type, reload = 0, companyId: companyIdProp, applicationId }) => { + const storedCompanyId = useStoreValue('chosenCompanyId'); + const effectiveCompanyId = companyIdProp || storedCompanyId; + const companies = useStoreValue('companies'); + const [docs, setDocs] = useState([]); + const [filters, setFilters] = useState(null); + const [loading, setLoading] = useState(false); + const [statuses, setStatuses] = useState([]); + const toast = useRef(null); + + useEffect(() => { + const shouldFetch = companyIdProp + ? !!effectiveCompanyId + : !!head(companies.filter(o => o.id === effectiveCompanyId)); + + if (!loading && shouldFetch && reload !== 0) { + setLoading(true); + CompanyDocumentsService.getCompanyDocuments(effectiveCompanyId, getCallback, errGetCallbacks, [ + ['documentType', type] + ]); + } + }, [effectiveCompanyId, reload, companies]); + + useEffect(() => { + const shouldFetch = companyIdProp + ? !!effectiveCompanyId + : !!head(companies.filter(o => o.id === effectiveCompanyId)); + + if (shouldFetch) { + setLoading(true); + CompanyDocumentsService.getCompanyDocuments(effectiveCompanyId, getCallback, errGetCallbacks, [ + ['documentType', type] + ]); + } + }, []); + + const getCallback = (resp) => { + if (resp.status === 'SUCCESS') { + setDocs(getFormattedData(resp.data)); + setStatuses(uniq(resp.data.map(o => o.status))) + initFilters(); + } + setLoading(false); + } + + const errGetCallbacks = () => { + setLoading(false); + } + + const getFormattedData = (data) => { + return data.map((d) => { + d.callEndDate = is(String, d.callEndDate) ? new Date(d.callEndDate) : (d.callEndDate ? d.callEndDate : ''); + return d; + }); + }; + + const clearFilter = () => { + initFilters(); + }; + + const initFilters = () => { + setFilters({ + name: { + operator: FilterOperator.AND, + constraints: [{ value: null, matchMode: FilterMatchMode.STARTS_WITH }] + }, + createdDate: { + operator: FilterOperator.AND, + constraints: [{ value: null, matchMode: FilterMatchMode.DATE_IS }] + }, + expirationDate: { + operator: FilterOperator.AND, + constraints: [{ value: null, matchMode: FilterMatchMode.DATE_IS }] + } + }); + }; + + const renderHeader = () => { + return ( +