diff --git a/src/constants.js b/src/constants.js index 512c0f1..7cb1889 100644 --- a/src/constants.js +++ b/src/constants.js @@ -120,4 +120,5 @@ export const BENEFIT_PLAN_CHANGELOG_TAB_VALUE = 'BenefitPlanChangelogTab'; export const TASK_CONTRIBUTION_KEY = 'tasksManagement.tasks'; export const BENEFIT_PLAN_TASK_TAB_VALUE = 'BenefitPlanTaskTab'; export const BENEFITS_CONTRIBUTION_KEY = 'payroll.benefitConsumptionPayrollSearcher'; +export const PYTHON_DEFAULT_IMPORT_WORKFLOW = 'Python Beneficiaries Upload'; export const BENEFICIARIES_QUANTITY_LIMIT = 100; diff --git a/src/dialogs/BenefitPlanBeneficiariesUploadDialog.js b/src/dialogs/BenefitPlanBeneficiariesUploadDialog.js index 2e7ed30..74911d2 100644 --- a/src/dialogs/BenefitPlanBeneficiariesUploadDialog.js +++ b/src/dialogs/BenefitPlanBeneficiariesUploadDialog.js @@ -1,5 +1,7 @@ import React, { useEffect, useState } from 'react'; -import { Input, Grid } from '@material-ui/core'; +import { + Input, Grid, MenuItem, Typography, Select, +} from '@material-ui/core'; import { injectIntl } from 'react-intl'; import Button from '@material-ui/core/Button'; import Dialog from '@material-ui/core/Dialog'; @@ -11,13 +13,16 @@ import { baseApiUrl, formatMessage, coreAlert, + FormattedMessage, } from '@openimis/fe-core'; import { withTheme, withStyles } from '@material-ui/core/styles'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import WorkflowsPicker from '../pickers/WorkflowsPicker'; import { fetchWorkflows } from '../actions'; -import { EMPTY_STRING } from '../constants'; +import { + BENEFIT_PLAN_TYPE, EMPTY_STRING, MODULE_NAME, PYTHON_DEFAULT_IMPORT_WORKFLOW, +} from '../constants'; const styles = (theme) => ({ item: theme.paper.item, @@ -33,6 +38,8 @@ function BenefitPlanBeneficiariesUploadDialog({ }) { const [isOpen, setIsOpen] = useState(false); const [forms, setForms] = useState({}); + const [headers, setHeaders] = useState([]); + const [groupAggregationHeader, setGroupAggregationHeader] = useState(null); const handleOpen = () => { setIsOpen(true); @@ -47,7 +54,44 @@ function BenefitPlanBeneficiariesUploadDialog({ fetchWorkflows(); }, []); - const handleFieldChange = (formName, fieldName, value) => { + const isBenefitPlanGroupType = () => benefitPlan.type === BENEFIT_PLAN_TYPE.GROUP; + + const getHeadersFromCSV = async (file) => new Promise((resolve, reject) => { + const reader = new FileReader(); + + reader.onload = (event) => { + const csvData = event.target.result; + const firstLine = csvData.substring(0, csvData.indexOf('\n')); // Get only the first line + const headers = firstLine.split(','); + if (headers.length && !headers.some((item) => !item)) { + headers.unshift(''); + } + resolve(headers); + }; + + reader.onerror = (error) => { + reject(error); + }; + + reader.readAsText(file); + }); + + const handleFileInputChange = async (selectedFile) => { + if (selectedFile) { + try { + const fileHeaders = await getHeadersFromCSV(selectedFile); + const filteredHeaders = fileHeaders.filter( + (header) => header !== 'recipient_info' && header !== 'group_id', + ); + setHeaders(filteredHeaders); + } catch (error) { + setHeaders([]); + } + } + }; + + const handleFieldChange = async (formName, fieldName, value) => { + if (fieldName === 'file') await handleFileInputChange(value); setForms({ ...forms, [formName]: { @@ -57,7 +101,7 @@ function BenefitPlanBeneficiariesUploadDialog({ }); }; - const getFieldValue = () => forms?.workflows?.values?.workflow?.label ?? {}; + const getFieldValue = () => forms?.workflows?.workflow?.name ?? {}; const onSubmit = async (values) => { const fileFormat = values.file.type; @@ -70,6 +114,7 @@ function BenefitPlanBeneficiariesUploadDialog({ formData.append('benefit_plan', benefitPlan.id); formData.append('workflow_name', values.workflow.name); formData.append('workflow_group', values.workflow.group); + formData.append('group_aggregation_column', groupAggregationHeader); urlImport = `${baseApiUrl}/social_protection/import_beneficiaries/`; } @@ -157,6 +202,39 @@ function BenefitPlanBeneficiariesUploadDialog({ /> + {getFieldValue() === PYTHON_DEFAULT_IMPORT_WORKFLOW && isBenefitPlanGroupType() ? ( + + + + + + + + + + + + + + * + {' '} + + + + + ) : null}