-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1206 from openedx/asheehan-edx/ENT-8562-members-d…
…ownload-csv feat: subsidy group members download csv functionality
- Loading branch information
Showing
9 changed files
with
270 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
src/components/learner-credit-management/members-tab/GroupMembersCsvDownloadTableAction.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { ActionRow, AlertModal, Button } from '@edx/paragon'; | ||
import { Download } from '@edx/paragon/icons'; | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
import snakeCase from 'lodash/snakeCase'; | ||
import { saveAs } from 'file-saver'; | ||
import EnterpriseAccessApiService from '../../../data/services/EnterpriseAccessApiService'; | ||
import { useBudgetId, useSubsidyAccessPolicy } from '../data'; | ||
|
||
const GroupMembersCsvDownloadTableAction = ({ | ||
tableInstance, | ||
}) => { | ||
const [alertModalOpen, setAlertModalOpen] = useState(false); | ||
const [alertModalExc, setAlertModalException] = useState(''); | ||
const { subsidyAccessPolicyId } = useBudgetId(); | ||
const { data: subsidyAccessPolicy } = useSubsidyAccessPolicy(subsidyAccessPolicyId); | ||
const groupId = subsidyAccessPolicy.groupAssociations[0]; | ||
|
||
const getCsvFileName = () => { | ||
const titleNoWhitespace = subsidyAccessPolicy.displayName.replace(/\s+/g, ''); | ||
const currentDate = new Date(); | ||
const year = currentDate.getUTCFullYear(); | ||
const month = currentDate.getUTCMonth() + 1; | ||
const day = currentDate.getUTCDate(); | ||
return `${titleNoWhitespace}-${year}-${month}-${day}.csv`; | ||
}; | ||
|
||
const csvDownloadOnClick = () => { | ||
const options = { | ||
format_csv: true, | ||
traverse_pagination: true, | ||
group_uuid: groupId, | ||
}; | ||
// Apply the table state to the request args | ||
// sortBy can support multiple values, the members table will only ever have one applied | ||
// so we can grab the data from the first index should it exist | ||
if (tableInstance.state.sortBy[0]) { | ||
options.sort_by = snakeCase(tableInstance.state.sortBy[0].id); | ||
// IFF we're doing sorting, check if it's in reverse order | ||
if (!tableInstance.state.sortBy[0].desc) { | ||
options.is_reversed = !tableInstance.state.sortBy[0].desc; | ||
} | ||
} | ||
tableInstance.state.filters.forEach((filter) => { | ||
if (filter.id === 'status') { | ||
options.show_removed = filter.value; | ||
} else if (filter.id === 'memberDetails') { | ||
options.user_query = snakeCase(filter.value); | ||
} | ||
}); | ||
|
||
EnterpriseAccessApiService.fetchSubsidyHydratedGroupMembersData( | ||
subsidyAccessPolicyId, | ||
options, | ||
).then(response => { | ||
// download CSV | ||
const blob = new Blob([response.data], { | ||
type: 'text/csv', | ||
}); | ||
saveAs(blob, getCsvFileName()); | ||
}).catch(err => { | ||
logError(err); | ||
setAlertModalOpen(true); | ||
setAlertModalException(err.message); | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<AlertModal | ||
title="Something went wrong" | ||
isOpen={alertModalOpen} | ||
onClose={() => setAlertModalOpen(false)} | ||
footerNode={( | ||
<ActionRow> | ||
<Button | ||
variant="tertiary" | ||
onClick={() => setAlertModalOpen(false)} | ||
> | ||
Close | ||
</Button> | ||
</ActionRow> | ||
)} | ||
> | ||
<p> | ||
We're sorry but something went wrong while downloading your CSV. | ||
Please refer to the error below and try again later. | ||
</p> | ||
<p>{alertModalExc}</p> | ||
</AlertModal> | ||
<Button | ||
onClick={csvDownloadOnClick} | ||
iconBefore={Download} | ||
variant="inverse-primary" | ||
className="border rounded-0 border-dark-500" | ||
disabled={tableInstance.itemCount === 0} | ||
> | ||
Download all ({tableInstance.itemCount}) | ||
</Button> | ||
</> | ||
); | ||
}; | ||
|
||
GroupMembersCsvDownloadTableAction.propTypes = { | ||
tableInstance: PropTypes.shape({ | ||
itemCount: PropTypes.number, | ||
state: PropTypes.shape({ | ||
filters: PropTypes.arrayOf(PropTypes.shape({ | ||
id: PropTypes.string, | ||
// Can be a string for user queries or bool for show removed toggle | ||
value: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.bool, | ||
]), | ||
})), | ||
sortBy: PropTypes.arrayOf(PropTypes.shape({ | ||
id: PropTypes.string, | ||
desc: PropTypes.bool, | ||
})), | ||
}), | ||
}), | ||
}; | ||
|
||
GroupMembersCsvDownloadTableAction.defaultProps = { | ||
tableInstance: { | ||
itemCount: 0, | ||
state: {}, | ||
}, | ||
}; | ||
|
||
export default GroupMembersCsvDownloadTableAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/components/learner-credit-management/members-tab/MembersTableSwitchFilter.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Form } from '@edx/paragon'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const MembersTableSwitchFilter = ({ column: { filterValue, setFilter } }) => ( | ||
<Form.Switch | ||
className="ml-2.5 mt-2.5" | ||
checked={filterValue || false} | ||
onChange={() => { | ||
setFilter(!filterValue || false); // Set undefined to remove the filter entirely | ||
}} | ||
data-testid="show-removed-toggle" | ||
> | ||
Show removed | ||
</Form.Switch> | ||
); | ||
|
||
MembersTableSwitchFilter.propTypes = { | ||
/** | ||
* Specifies a column object. | ||
* | ||
* `setFilter`: Function to set the filter value. | ||
* | ||
* `filterValue`: Value for the filter input. | ||
*/ | ||
column: PropTypes.shape({ | ||
setFilter: PropTypes.func.isRequired, | ||
Header: PropTypes.oneOfType([PropTypes.elementType, PropTypes.node]).isRequired, | ||
filterValue: PropTypes.bool, | ||
}).isRequired, | ||
}; | ||
|
||
export default MembersTableSwitchFilter; |
Oops, something went wrong.