forked from primeroIMS/primero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.jsx
134 lines (114 loc) · 4.15 KB
/
container.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2014 - 2023 UNICEF. All rights reserved.
/* eslint-disable react/no-multi-comp, react/display-name */
import PropTypes from "prop-types";
import DownloadIcon from "@material-ui/icons/GetApp";
import CircularProgress from "@material-ui/core/CircularProgress";
import startCase from "lodash/startCase";
import DisableOffline from "../../disable-offline";
import PageContainer, { PageHeading, PageContent } from "../../page";
import IndexTable from "../../index-table";
import { useI18n } from "../../i18n";
import { DATE_TIME_FORMAT, FETCH_PARAM } from "../../../config";
import { getMetadata } from "../../record-list";
import { useMetadata } from "../../records";
import { useMemoizedSelector } from "../../../libs";
import { fetchExports } from "./action-creators";
import css from "./styles.css";
import { selectListHeaders } from "./selectors";
import { NAME, EXPORT_STATUS, EXPORT_COLUMNS } from "./constants";
const ExportList = () => {
const i18n = useI18n();
const recordType = "bulk_exports";
const listHeaders = useMemoizedSelector(state => selectListHeaders(state, recordType));
const metadata = useMemoizedSelector(state => getMetadata(state, recordType));
const defaultFilters = metadata;
const isRecordProcessing = status => status === EXPORT_STATUS.processing;
const onRowClick = record => (!isRecordProcessing(record.status) ? window.open(record.export_file, "_self") : null);
const columns = data => {
const handleClick = exportRecord => () => onRowClick(exportRecord);
return listHeaders.map(c => {
const options = {
...{
...(c.name === EXPORT_COLUMNS.fileName
? {
id: true,
customBodyRender: (value, tableMeta) => {
const exportRecord = data.getIn(["data", tableMeta.rowIndex]);
const { status } = exportRecord;
const exportIcon = isRecordProcessing(status) ? (
<CircularProgress color="inherit" className={css.loading} />
) : (
<DownloadIcon fontSize="small" />
);
return (
<DisableOffline>
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events */}
<div
className={css.link}
onClick={handleClick(exportRecord)}
role="button"
tabIndex={tableMeta.rowIndex}
>
{exportIcon}
<span>{value}</span>
</div>
</DisableOffline>
);
}
}
: {}),
...(c.name === EXPORT_COLUMNS.recordType
? {
customBodyRender: value => (
<DisableOffline>
<span>{startCase(value)}</span>
</DisableOffline>
)
}
: {}),
...(c.name === EXPORT_COLUMNS.startedOn
? {
customBodyRender: value => (
<DisableOffline>
<span>{i18n.localizeDate(value, DATE_TIME_FORMAT)}</span>
</DisableOffline>
)
}
: {})
}
};
return {
name: c.field_name,
label: i18n.t(`bulk_export.${c.name}`),
options
};
});
};
const options = {
selectableRows: "none"
};
useMetadata(recordType, metadata, fetchExports, FETCH_PARAM.DATA);
const tableOptions = {
recordType,
columns,
options,
defaultFilters,
onTableChange: fetchExports,
rowHover: false,
onRowClick: record => onRowClick(record),
bypassInitialFetch: true
};
return (
<PageContainer>
<PageHeading data-testid="page-heading" title={i18n.t("navigation.bulk_exports")} />
<PageContent>
<IndexTable title={i18n.t("navigation.bulk_exports")} {...tableOptions} />
</PageContent>
</PageContainer>
);
};
ExportList.displayName = NAME;
ExportList.propTypes = {
match: PropTypes.object
};
export default ExportList;