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

Added functionality to export asset details in csv format #5566

Closed
wants to merge 5 commits into from
Closed
Changes from 3 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
41 changes: 39 additions & 2 deletions src/Components/Assets/AssetsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ const AssetsList = () => {
dispatch,
]
);
const exportToCSV = (data: any) => {
// excluded the unnecessry columns and rows from the csv
const excludedIndices = [3, 4, 5, 13];
const csvData = data.results;
// getiing the headers of csv
const headers =
Object.keys(csvData[0])
.filter((key, index) => !excludedIndices.includes(index))
.join(",") + "\n";
// getting the rows of csv
const rows = csvData
.map((obj: any) =>
Object.values(obj)
.filter((value, index) => !excludedIndices.includes(index))
.join(",")
)
.join("\n");
// returning the headers and rows of csv
return headers + rows;
};

useEffect(() => {
setAssetType(qParams.asset_type);
Expand Down Expand Up @@ -315,7 +335,24 @@ const AssetsList = () => {
},
},
{
label: "Export Assets",
label: "Export Assets as CSV",
action: () =>
authorizedForImportExport &&
listAssets({
...qParams,
csv: true,
limit: totalCount,
}),
type: "csv",
parse: exportToCSV,
filePrefix: `assets_${facility?.name && ""}`,
options: {
icon: <CareIcon className="care-l-export" />,
disabled: totalCount === 0 || !authorizedForImportExport,
},
},
{
label: "Export Assets as JSON",
action: () =>
authorizedForImportExport &&
listAssets({
Expand All @@ -324,7 +361,7 @@ const AssetsList = () => {
limit: totalCount,
}),
type: "json",
filePrefix: `assets_${facility?.name}`,
filePrefix: `assets_${facility?.name && ""}`,
options: {
icon: <CareIcon className="care-l-export" />,
disabled: totalCount === 0 || !authorizedForImportExport,
Expand Down