Skip to content

Commit

Permalink
fix: adding csv download
Browse files Browse the repository at this point in the history
  • Loading branch information
kiram15 committed Dec 12, 2024
1 parent 8abd6de commit 7cf109c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import {
Toast, StatefulButton, Icon, Spinner, useToggle,
} from '@openedx/paragon';
import { Download, Check } from '@openedx/paragon/icons';
import { logError } from '@edx/frontend-platform/logging';
import { jsonToCsv } from '../utils';
import GeneralErrorModal from '../GeneralErrorModal';

const DownloadCsvButton = ({ data, testId, fetchData }) => {
const DownloadCsvButton = ({ data, testId }) => {
const [buttonState, setButtonState] = useState('pageLoading');
const [isOpen, open, close] = useToggle(false);
const [isErrorModalOpen, openErrorModal, closeErrorModal] = useToggle(false);
const intl = useIntl();

useEffect(() => {
Expand All @@ -28,18 +30,28 @@ const DownloadCsvButton = ({ data, testId, fetchData }) => {
return `${year}-${month}-${day}-group-detail-report.csv`;

Check warning on line 30 in src/components/PeopleManagement/GroupDetailPage/DownloadCsvButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/PeopleManagement/GroupDetailPage/DownloadCsvButton.jsx#L26-L30

Added lines #L26 - L30 were not covered by tests
};

const createCsvData = (jsonData) => jsonToCsv(jsonData.map(row => ({
Email: row.memberDetails.userEmail,
Username: row.memberDetails.userName,
Enrollments: row.enrollments,
// we have to strip out the comma so it doesn't mess up the csv parsing
'Recent action': row.recent_action.replace(/,/g, ''),
})));

const handleClick = async () => {
setButtonState('pending');
fetchData().then((response) => {
const blob = new Blob([response.data.results], {
try {
const csv = createCsvData(data);
const blob = new Blob([csv], {

Check warning on line 45 in src/components/PeopleManagement/GroupDetailPage/DownloadCsvButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/PeopleManagement/GroupDetailPage/DownloadCsvButton.jsx#L42-L45

Added lines #L42 - L45 were not covered by tests
type: 'text/csv',
});
saveAs(blob, getCsvFileName());
open();

Check warning on line 49 in src/components/PeopleManagement/GroupDetailPage/DownloadCsvButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/PeopleManagement/GroupDetailPage/DownloadCsvButton.jsx#L48-L49

Added lines #L48 - L49 were not covered by tests
} catch {
openErrorModal();

Check warning on line 51 in src/components/PeopleManagement/GroupDetailPage/DownloadCsvButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/PeopleManagement/GroupDetailPage/DownloadCsvButton.jsx#L51

Added line #L51 was not covered by tests
} finally {
setButtonState('complete');

Check warning on line 53 in src/components/PeopleManagement/GroupDetailPage/DownloadCsvButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/PeopleManagement/GroupDetailPage/DownloadCsvButton.jsx#L53

Added line #L53 was not covered by tests
}).catch((err) => {
logError(err);
});
}
};

const toastText = intl.formatMessage({
Expand All @@ -55,6 +67,10 @@ const DownloadCsvButton = ({ data, testId, fetchData }) => {
{toastText}
</Toast>
)}
<GeneralErrorModal
isOpen={isErrorModalOpen}
close={closeErrorModal}
/>
<StatefulButton
state={buttonState}
className="download-button"
Expand Down Expand Up @@ -103,7 +119,6 @@ DownloadCsvButton.propTypes = {
data: PropTypes.arrayOf(
PropTypes.object,
),
fetchData: PropTypes.func.isRequired,
testId: PropTypes.string,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useEnterpriseGroupLearnersTableData, useEnterpriseGroupUuid } from '../
import { ROUTE_NAMES } from '../../EnterpriseApp/data/constants';
import DeleteGroupModal from './DeleteGroupModal';
import EditGroupNameModal from './EditGroupNameModal';
import formatDates from '../utils';
import { formatDates } from '../utils';
import GroupMembersTable from './GroupMembersTable';

const GroupDetailPage = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
} from '../constants';
import RecentActionTableCell from '../RecentActionTableCell';
import DownloadCsvButton from './DownloadCsvButton';
import LmsApiService from '../../../data/services/LmsApiService';
import RemoveMemberModal from './RemoveMemberModal';
import GeneralErrorModal from '../GeneralErrorModal';

Expand Down Expand Up @@ -91,11 +90,6 @@ const GroupMembersTable = ({
refresh,
setRefresh,
}) => {
const fetchCsvData = async () => LmsApiService.fetchEnterpriseGroupLearners(
groupUuid,
// { ...currentFilters, search: searchQuery },
{ csv: true },
);
const intl = useIntl();
return (
<span className="budget-detail-assignments">
Expand Down Expand Up @@ -169,7 +163,6 @@ const GroupMembersTable = ({
]}
tableActions={[
<DownloadCsvButton
fetchData={fetchCsvData}
data={tableData.results}
testId="group-members-download"
/>,
Expand Down
2 changes: 1 addition & 1 deletion src/components/PeopleManagement/RecentActionTableCell.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import formatDates from './utils';
import { formatDates } from './utils';

const RecentActionTableCell = ({
row,
Expand Down
14 changes: 13 additions & 1 deletion src/components/PeopleManagement/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ import dayjs from 'dayjs';
* @param {string} timestamp unformatted date timestamp
* @returns Formatted date string for display.
*/
export default function formatDates(timestamp) {
export function formatDates(timestamp) {
const DATE_FORMAT = 'MMMM DD, YYYY';
return dayjs(timestamp).format(DATE_FORMAT);
}

export function jsonToCsv(data) {
let csv = '';
const headers = Object.keys(data[0]);
csv += `${headers.join(',')}\n`;

Check warning on line 17 in src/components/PeopleManagement/utils.js

View check run for this annotation

Codecov / codecov/patch

src/components/PeopleManagement/utils.js#L14-L17

Added lines #L14 - L17 were not covered by tests

data.forEach((row) => {
const rows = headers.map(header => row[header]).join(',');
csv += `${rows}\n`;

Check warning on line 21 in src/components/PeopleManagement/utils.js

View check run for this annotation

Codecov / codecov/patch

src/components/PeopleManagement/utils.js#L19-L21

Added lines #L19 - L21 were not covered by tests
});
return csv;

Check warning on line 23 in src/components/PeopleManagement/utils.js

View check run for this annotation

Codecov / codecov/patch

src/components/PeopleManagement/utils.js#L23

Added line #L23 was not covered by tests
}

0 comments on commit 7cf109c

Please sign in to comment.