From 75eb0c307e3fece5ff6ae16ad43c879d77569cc9 Mon Sep 17 00:00:00 2001 From: Raymond Zhou <56318341+rayzhou-bit@users.noreply.github.com> Date: Fri, 15 Dec 2023 11:52:59 -0800 Subject: [PATCH] fix: datatable state persistence issues (#746) --- src/files-and-videos/generic/FileTable.jsx | 13 +++++++++--- .../generic/table-components/TableActions.jsx | 12 ++++++++++- .../SortAndFilterModal.jsx | 5 ++++- .../generic/table-components/utils.js | 4 ++-- .../generic/table-components/utils.test.js | 21 ++++++++++++++++--- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/files-and-videos/generic/FileTable.jsx b/src/files-and-videos/generic/FileTable.jsx index f50eae6b5..f58f5c29f 100644 --- a/src/files-and-videos/generic/FileTable.jsx +++ b/src/files-and-videos/generic/FileTable.jsx @@ -62,6 +62,14 @@ const FileTable = ({ const [isAddOpen, setAddOpen, setAddClose] = useToggle(false); const [selectedRows, setSelectedRows] = useState([]); const [isDeleteConfirmationOpen, openDeleteConfirmation, closeDeleteConfirmation] = useToggle(false); + const [initialState, setInitialState] = useState({ + filters: [], + hiddenColumns: [], + pageIndex: 0, + pageSize: 50, + selectedRowIds: {}, + sortBy: [], + }); const { loadingStatus, @@ -143,6 +151,7 @@ const FileTable = ({ handleOpenDeleteConfirmation, supportedFileFormats, fileType, + setInitialState, }} /> ); @@ -193,9 +202,7 @@ const FileTable = ({ defaultActiveStateValue: defaultVal, togglePlacement: 'left', }} - initialState={{ - pageSize: 50, - }} + initialState={initialState} tableActions={headerActions} bulkActions={headerActions} columns={tableColumns} diff --git a/src/files-and-videos/generic/table-components/TableActions.jsx b/src/files-and-videos/generic/table-components/TableActions.jsx index 6e775f51c..36d64f587 100644 --- a/src/files-and-videos/generic/table-components/TableActions.jsx +++ b/src/files-and-videos/generic/table-components/TableActions.jsx @@ -1,10 +1,11 @@ -import React from 'react'; +import React, { useContext, useEffect } from 'react'; import _ from 'lodash'; import { PropTypes } from 'prop-types'; import { injectIntl, intlShape, FormattedMessage } from '@edx/frontend-platform/i18n'; import { getConfig } from '@edx/frontend-platform'; import { Button, + DataTableContext, Dropdown, useToggle, } from '@edx/paragon'; @@ -20,10 +21,18 @@ const TableActions = ({ handleOpenDeleteConfirmation, encodingsDownloadUrl, fileType, + setInitialState, // injected intl, }) => { const [isSortOpen, openSort, closeSort] = useToggle(false); + const { state } = useContext(DataTableContext); + + // This useEffect saves DataTable state so it can persist after table re-renders due to data reload. + useEffect(() => { + setInitialState(state); + }, [state]); + return ( <>