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: add aggregation column for import #75

Merged
merged 2 commits into from
May 14, 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
83 changes: 78 additions & 5 deletions src/components/dialogs/IndividualsUploadDialog.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { useEffect, useState } from 'react';
import { Input, Grid, MenuItem } 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 @@ -12,14 +14,15 @@ import {
useModulesManager,
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 IndividualsHistoryUploadDialog from './IndividualsHistoryUploadDialog';
import { EMPTY_STRING } from '../../constants';
import { EMPTY_STRING, INDIVIDUAL_MODULE_NAME, PYTHON_DEFAULT_IMPORT_WORKFLOW } from '../../constants';

const styles = (theme) => ({
item: theme.paper.item,
Expand All @@ -34,21 +37,57 @@ function IndividualsUploadDialog({
const modulesManager = useModulesManager();
const [isOpen, setIsOpen] = useState(false);
const [forms, setForms] = useState({});
const [headers, setHeaders] = useState([]);
const [groupAggregationHeader, setGroupAggregationHeader] = useState(null);

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');
setHeaders(filteredHeaders);
} catch (error) {
setHeaders([]);
}
}
};

const handleOpen = () => {
setIsOpen(true);
};

const handleClose = () => {
setForms({});
setHeaders([]);
setIsOpen(false);
};

useEffect(() => {
fetchWorkflows();
}, []);

const handleFieldChange = (formName, fieldName, value) => {
const handleFieldChange = async (formName, fieldName, value) => {
if (fieldName === 'file') await handleFileInputChange(value);
setForms({
...forms,
[formName]: {
Expand All @@ -58,7 +97,7 @@ function IndividualsUploadDialog({
});
};

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 +109,7 @@ function IndividualsUploadDialog({
if (fileFormat.includes('/csv')) {
formData.append('workflow_name', values.workflow.name);
formData.append('workflow_group', values.workflow.group);
formData.append('group_aggregation_column', groupAggregationHeader);
urlImport = `${baseApiUrl}/individual/import_individuals/`;
}

Expand Down Expand Up @@ -151,7 +191,7 @@ function IndividualsUploadDialog({
</Grid>
<Grid item>
<WorkflowsPicker
module="individual"
module={INDIVIDUAL_MODULE_NAME}
label="workflowPicker"
onChange={(value) => handleFieldChange('workflows', 'workflow', value)}
value={() => getFieldValue()}
Expand All @@ -160,6 +200,39 @@ function IndividualsUploadDialog({
/>
</Grid>
</Grid>
{getFieldValue() === PYTHON_DEFAULT_IMPORT_WORKFLOW ? (
<Grid container direction="row" alignItems="center">
<Grid container spacing={4} direction="row" alignItems="center">
<Grid item>
<Typography>
<FormattedMessage module={INDIVIDUAL_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={INDIVIDUAL_MODULE_NAME} id="groupAggregationInfo" />
</Typography>
</Grid>
</Grid>
) : null}
</Grid>
</div>
</DialogContent>
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ export const UPLOAD_STATUS = {
FAIL: 'FAIL',
};
export const INDIVIDUALS_QUANTITY_LIMIT = 15;
export const PYTHON_DEFAULT_IMPORT_WORKFLOW = 'Python Import Individuals';
4 changes: 3 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,7 @@
"confirm": "Confirm",
"cancel": "Cancel",
"changeGroupButtonTooltip": "Move to another group.",
"moveToNewGroup": "New Group"
"moveToNewGroup": "New Group",
"createGroupFromColumns": "Create groups from column:",
"groupAggregationInfo": "To specify recipients include header 'recipient_info' with possible values: '1' - head, '2' - recipient, '0' or empty - not a recipient."
}
Loading