Skip to content

Commit 2bc7cfc

Browse files
committed
chore: migrate deprecated Table to DataTable for LearnerActivityTable
1 parent 47e10ce commit 2bc7cfc

File tree

14 files changed

+1117
-1794
lines changed

14 files changed

+1117
-1794
lines changed

src/components/Admin/Admin.test.jsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -492,18 +492,6 @@ describe('<Admin />', () => {
492492
csvFetchMethod: 'fetchEnrolledLearnersForInactiveCourses',
493493
csvFetchParams: [enterpriseId, {}, { csv: true }],
494494
},
495-
'learners-active-week': {
496-
csvFetchMethod: 'fetchCourseEnrollments',
497-
csvFetchParams: [enterpriseId, { learnerActivity: 'active_past_week' }, { csv: true }],
498-
},
499-
'learners-inactive-week': {
500-
csvFetchMethod: 'fetchCourseEnrollments',
501-
csvFetchParams: [enterpriseId, { learnerActivity: 'inactive_past_week' }, { csv: true }],
502-
},
503-
'learners-inactive-month': {
504-
csvFetchMethod: 'fetchCourseEnrollments',
505-
csvFetchParams: [enterpriseId, { learnerActivity: 'inactive_past_month' }, { csv: true }],
506-
},
507495
'completed-learners': {
508496
csvFetchMethod: 'fetchCompletedLearners',
509497
csvFetchParams: [enterpriseId, {}, { csv: true }],
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import PropTypes from 'prop-types';
2+
import DownloadCsvButton from '../../containers/DownloadCsvButton';
3+
import { useTableData } from './TableDataContext';
4+
5+
const DownloadButtonWrapper = ({
6+
tableMetadata,
7+
actionSlug,
8+
downloadButtonLabel,
9+
isTableDataMissing,
10+
}) => {
11+
const { tablesWithData } = useTableData();
12+
13+
// Check if this is a LearnerActivityTable
14+
const isLearnerActivityTable = [
15+
'learners-active-week',
16+
'learners-inactive-week',
17+
'learners-inactive-month',
18+
].includes(actionSlug);
19+
20+
return (
21+
<DownloadCsvButton
22+
id={tableMetadata.csvButtonId}
23+
fetchMethod={tableMetadata.csvFetchMethod}
24+
disabled={isLearnerActivityTable ? !tablesWithData[actionSlug] : isTableDataMissing(actionSlug)}
25+
buttonLabel={downloadButtonLabel}
26+
/>
27+
);
28+
};
29+
30+
DownloadButtonWrapper.propTypes = {
31+
tableMetadata: PropTypes.shape({
32+
csvButtonId: PropTypes.string,
33+
csvFetchMethod: PropTypes.func,
34+
}).isRequired,
35+
actionSlug: PropTypes.string,
36+
downloadButtonLabel: PropTypes.string,
37+
isTableDataMissing: PropTypes.func.isRequired,
38+
};
39+
40+
DownloadButtonWrapper.defaultProps = {
41+
actionSlug: undefined,
42+
downloadButtonLabel: undefined,
43+
};
44+
45+
export default DownloadButtonWrapper;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {
2+
createContext, useContext, useState, useMemo,
3+
} from 'react';
4+
import PropTypes from 'prop-types';
5+
6+
const TableDataContext = createContext({
7+
tablesWithData: {},
8+
setTableHasData: () => {},
9+
});
10+
11+
export const TableDataProvider = ({ children }) => {
12+
const [tablesWithData, setTablesWithData] = useState({});
13+
14+
const setTableHasData = (tableId, hasData) => {
15+
setTablesWithData(prev => ({
16+
...prev,
17+
[tableId]: hasData,
18+
}));
19+
};
20+
21+
const value = useMemo(() => ({
22+
tablesWithData,
23+
setTableHasData,
24+
}), [tablesWithData]);
25+
26+
return (
27+
<TableDataContext.Provider value={value}>
28+
{children}
29+
</TableDataContext.Provider>
30+
);
31+
};
32+
33+
TableDataProvider.propTypes = {
34+
children: PropTypes.node.isRequired,
35+
};
36+
37+
export const useTableData = () => useContext(TableDataContext);

src/components/Admin/index.jsx

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import EnrolledLearnersForInactiveCoursesTable from '../EnrolledLearnersForInact
1818
import CompletedLearnersTable from '../CompletedLearnersTable';
1919
import PastWeekPassedLearnersTable from '../PastWeekPassedLearnersTable';
2020
import LearnerActivityTable from '../LearnerActivityTable';
21-
import DownloadCsvButton from '../../containers/DownloadCsvButton';
2221
import AdminCards from '../../containers/AdminCards';
2322
import AdminSearchForm from './AdminSearchForm';
2423
import EnterpriseAppSkeleton from '../EnterpriseApp/EnterpriseAppSkeleton';
@@ -34,6 +33,8 @@ import AIAnalyticsSummary from './AIAnalyticsSummary';
3433
import AIAnalyticsSummarySkeleton from './AIAnalyticsSummarySkeleton';
3534
import BudgetExpiryAlertAndModal from '../BudgetExpiryAlertAndModal';
3635
import ModuleActivityReport from './tabs/ModuleActivityReport';
36+
import { TableDataProvider } from './TableDataContext';
37+
import DownloadButtonWrapper from './DownloadButtonWrapper';
3738

3839
class Admin extends React.Component {
3940
constructor(props) {
@@ -264,18 +265,18 @@ class Admin extends React.Component {
264265
return tableData && tableData.data;
265266
}
266267

267-
displaySearchBar() {
268-
return !this.props.actionSlug;
269-
}
270-
271-
isTableDataMissing(id) {
268+
isTableDataMissing = (id) => {
272269
const tableData = this.getTableData(id);
273270
if (!tableData) {
274271
return true;
275272
}
276273
const isTableLoading = tableData.loading;
277274
const isTableEmpty = tableData.results && !tableData.results.length;
278275
return isTableLoading || isTableEmpty;
276+
};
277+
278+
displaySearchBar() {
279+
return !this.props.actionSlug;
279280
}
280281

281282
hasAnalyticsData() {
@@ -313,11 +314,11 @@ class Admin extends React.Component {
313314
}
314315

315316
return (
316-
<DownloadCsvButton
317-
id={tableMetadata.csvButtonId}
318-
fetchMethod={tableMetadata.csvFetchMethod}
319-
disabled={this.isTableDataMissing(actionSlug)}
320-
buttonLabel={downloadButtonLabel}
317+
<DownloadButtonWrapper
318+
tableMetadata={tableMetadata}
319+
actionSlug={actionSlug}
320+
downloadButtonLabel={downloadButtonLabel}
321+
isTableDataMissing={this.isTableDataMissing}
321322
/>
322323
);
323324
}
@@ -528,24 +529,25 @@ class Admin extends React.Component {
528529
/>
529530
)}
530531
</div>
531-
<Tabs
532-
variant="tabs"
533-
activeKey={activeTab}
534-
onSelect={(tab) => {
535-
this.setState({ activeTab: tab });
536-
}}
537-
>
538-
<Tab
539-
eventKey="learner-progress-report"
540-
title={intl.formatMessage({
541-
id: 'adminPortal.lpr.tab.learnerProgressReport.title',
542-
defaultMessage: 'Learner Progress Report',
543-
description: 'Title for the learner progress report tab in admin portal.',
544-
})}
532+
<TableDataProvider>
533+
<Tabs
534+
variant="tabs"
535+
activeKey={activeTab}
536+
onSelect={(tab) => {
537+
this.setState({ activeTab: tab });
538+
}}
545539
>
546-
<div className="row">
547-
<div className="col">
548-
{!error && !loading && !this.hasEmptyData() && (
540+
<Tab
541+
eventKey="learner-progress-report"
542+
title={intl.formatMessage({
543+
id: 'adminPortal.lpr.tab.learnerProgressReport.title',
544+
defaultMessage: 'Learner Progress Report',
545+
description: 'Title for the learner progress report tab in admin portal.',
546+
})}
547+
>
548+
<div className="row">
549+
<div className="col">
550+
{!error && !loading && !this.hasEmptyData() && (
549551
<>
550552
<div className="row pb-3 mt-2">
551553
<div className="col-12 col-md-12 col-xl-12">
@@ -563,27 +565,28 @@ class Admin extends React.Component {
563565
/>
564566
)}
565567
</>
566-
)}
567-
{csvErrorMessage && this.renderCsvErrorMessage(csvErrorMessage)}
568-
<div className="mt-3 mb-5">
569-
{enterpriseId && tableMetadata.component}
568+
)}
569+
{csvErrorMessage && this.renderCsvErrorMessage(csvErrorMessage)}
570+
<div className="mt-3 mb-5">
571+
{enterpriseId && tableMetadata.component}
572+
</div>
570573
</div>
571574
</div>
572-
</div>
573-
</Tab>
574-
<Tab
575-
eventKey="module-activity"
576-
title={intl.formatMessage({
577-
id: 'adminPortal.lpr.tab.moduleActivity.title',
578-
defaultMessage: 'Module Activity (Executive Education)',
579-
description: 'Title for the module activity tab in admin portal.',
580-
})}
581-
>
582-
<div className="mt-3">
583-
<ModuleActivityReport enterpriseId={enterpriseId} />
584-
</div>
585-
</Tab>
586-
</Tabs>
575+
</Tab>
576+
<Tab
577+
eventKey="module-activity"
578+
title={intl.formatMessage({
579+
id: 'adminPortal.lpr.tab.moduleActivity.title',
580+
defaultMessage: 'Module Activity (Executive Education)',
581+
description: 'Title for the module activity tab in admin portal.',
582+
})}
583+
>
584+
<div className="mt-3">
585+
<ModuleActivityReport enterpriseId={enterpriseId} />
586+
</div>
587+
</Tab>
588+
</Tabs>
589+
</TableDataProvider>
587590
</div>
588591
</div>
589592
</>

0 commit comments

Comments
 (0)