Skip to content

Commit

Permalink
CM-983: added downloading template file for upload (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
sniedzielski authored Aug 1, 2024
1 parent 61bfe4b commit 481bfde
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/components/dialogs/IndividualsUploadDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import WorkflowsPicker from '../../pickers/WorkflowsPicker';
import { fetchWorkflows } from '../../actions';
import IndividualsHistoryUploadDialog from './IndividualsHistoryUploadDialog';
import { EMPTY_STRING, INDIVIDUAL_MODULE_NAME, PYTHON_DEFAULT_IMPORT_WORKFLOW } from '../../constants';
import downloadTemplate from '../../util/export';

const styles = (theme) => ({
item: theme.paper.item,
Expand Down Expand Up @@ -143,6 +144,10 @@ function IndividualsUploadDialog({
return `${modulesManager.getRef('individual.route.enrollment')}`;
}

const downloadExampleTemplate = () => {
downloadTemplate();
};

return (
<>
<MenuItem>
Expand Down Expand Up @@ -261,6 +266,15 @@ function IndividualsUploadDialog({
</Button>
</div>
<div style={{ float: 'right', paddingRight: '16px' }}>
<Button
variant="contained"
color="primary"
onClick={() => downloadExampleTemplate()}
style={{ marginRight: '8px' }}
>
{formatMessage(intl, 'individual', 'individual.upload.template')}
</Button>
<></><></>

Check failure on line 277 in src/components/dialogs/IndividualsUploadDialog.js

View workflow job for this annotation

GitHub Actions / lint

Fragments should contain more than one child - otherwise, there’s no need for a Fragment at all

Check failure on line 277 in src/components/dialogs/IndividualsUploadDialog.js

View workflow job for this annotation

GitHub Actions / lint

Passing a fragment to an HTML element is useless

Check failure on line 277 in src/components/dialogs/IndividualsUploadDialog.js

View workflow job for this annotation

GitHub Actions / lint

`<></>` must be placed on a new line

Check failure on line 277 in src/components/dialogs/IndividualsUploadDialog.js

View workflow job for this annotation

GitHub Actions / lint

Fragments should contain more than one child - otherwise, there’s no need for a Fragment at all

Check failure on line 277 in src/components/dialogs/IndividualsUploadDialog.js

View workflow job for this annotation

GitHub Actions / lint

Passing a fragment to an HTML element is useless
<Button
variant="contained"
color="primary"
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"alert.header": "Error during file upload.",
"workflowPicker": "Workflow",
"cancel": "Cancel",
"template": "Template",
"uploadHistoryTable": {
"workflow": "Workflow",
"dateCreated": "Date Created",
Expand Down
22 changes: 22 additions & 0 deletions src/util/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { baseApiUrl } from '@openimis/fe-core';

export default function downloadTemplate() {
const url = new URL(
`${window.location.origin}${baseApiUrl}/individual/download_template_file/`,
);

fetch(url)
.then((response) => response.blob())
.then((blob) => {
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'individual_upload_template.csv';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
})
.catch((error) => {
// eslint-disable-next-line no-console
console.error('Export failed, reason: ', error);
});
}

0 comments on commit 481bfde

Please sign in to comment.