Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CM-808: aggregate on group beneficiary upload #90

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
86 changes: 82 additions & 4 deletions src/dialogs/BenefitPlanBeneficiariesUploadDialog.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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]: {
Expand All @@ -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;
Expand All @@ -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/`;
}

Expand Down Expand Up @@ -157,6 +202,39 @@ function BenefitPlanBeneficiariesUploadDialog({
/>
</Grid>
</Grid>
{getFieldValue() === PYTHON_DEFAULT_IMPORT_WORKFLOW && isBenefitPlanGroupType() ? (
<Grid container direction="row" alignItems="center">
<Grid container spacing={4} direction="row" alignItems="center">
<Grid item>
<Typography>
<FormattedMessage module={MODULE_NAME} id="createGroupFromColumns" />
</Typography>
</Grid>
<Grid item md={5}>
<Select
id="select"
value={groupAggregationHeader}
onChange={(v) => setGroupAggregationHeader(v.target.value)}
fullWidth
>
{headers.map((header, index) => (
// eslint-disable-next-line react/no-array-index-key
<MenuItem key={index} value={header}>
{header}
</MenuItem>
))}
</Select>
</Grid>
</Grid>
<Grid spacing={4} item>
<Typography style={{ fontSize: '12px' }}>
*
{' '}
<FormattedMessage module={MODULE_NAME} id="groupAggregationInfo" />
</Typography>
</Grid>
</Grid>
) : null}
</Grid>
</div>
</DialogContent>
Expand Down
Loading