Skip to content

Commit

Permalink
Only disable page-index-reset for edits (#3160)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Aug 13, 2024
1 parent fa18e58 commit 9feb899
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/goals/ReviewEntries/ReviewEntriesTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default function ReviewEntriesTable(props: {
state.currentProjectState.project.grammaticalInfoEnabled
);

const autoResetPageIndexRef = useRef(true);
const rowVirtualizerInstanceRef = useRef<MRT_RowVirtualizer>(null);

const [data, setData] = useState<Word[]>([]);
Expand All @@ -97,6 +98,11 @@ export default function ReviewEntriesTable(props: {

const { i18n, t } = useTranslation();

useEffect(() => {
// https://tanstack.com/table/latest/docs/faq#how-do-i-stop-my-table-state-from-automatically-resetting-when-my-data-changes
autoResetPageIndexRef.current = true;
});

useEffect(() => {
getAllSpeakers().then((list) =>
setSpeakers(
Expand All @@ -116,14 +122,22 @@ export default function ReviewEntriesTable(props: {

/** Removes word with given `id` from the state. */
const deleteWord = (id: string): void => {
setData((prev) => prev.filter((w) => w.id !== id));
setData((prev) => {
// Prevent table from jumping back to first page
autoResetPageIndexRef.current = false;
return prev.filter((w) => w.id !== id);
});
};

/** Replaces word (`.id === oldId`) in the state
* with word (`.id === newId`) fetched from the backend. */
const replaceWord = async (oldId: string, newId: string): Promise<void> => {
const newWord = await getWord(newId);
setData((prev) => prev.map((w) => (w.id === oldId ? newWord : w)));
setData((prev) => {
// Prevent table from jumping back to first page
autoResetPageIndexRef.current = false;
return prev.map((w) => (w.id === oldId ? newWord : w));
});
};

/** Checks if there are any entries and, if so, scrolls to the top of the current page. */
Expand Down Expand Up @@ -317,7 +331,7 @@ export default function ReviewEntriesTable(props: {
const table = useMaterialReactTable({
columns,
data,
autoResetPageIndex: false,
autoResetPageIndex: autoResetPageIndexRef.current,
columnFilterDisplayMode: "popover",
enableColumnActions: false,
enableColumnDragging: false,
Expand Down

0 comments on commit 9feb899

Please sign in to comment.