Skip to content

Commit

Permalink
Merge pull request rowyio#1139 from iamanishroy/save-sort-on-update
Browse files Browse the repository at this point in the history
Bug fix: rerendering
  • Loading branch information
shamsmosowi authored Feb 26, 2023
2 parents 4bbe1e9 + eafb209 commit 2301718
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 37 deletions.
20 changes: 20 additions & 0 deletions src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import {
tablePageAtom,
updateColumnAtom,
selectedCellAtom,
tableSortsAtom,
tableIdAtom,
} from "@src/atoms/tableScope";
import { projectScope, userSettingsAtom } from "@src/atoms/projectScope";
import { getFieldType, getFieldProp } from "@src/components/fields";
import { useKeyboardNavigation } from "./useKeyboardNavigation";
import { useMenuAction } from "./useMenuAction";
Expand Down Expand Up @@ -98,6 +101,11 @@ export default function Table({

const updateColumn = useSetAtom(updateColumnAtom, tableScope);

// Get user settings and tableId for applying sort sorting
const [userSettings] = useAtom(userSettingsAtom, projectScope);
const [tableId] = useAtom(tableIdAtom, tableScope);
const setTableSorts = useSetAtom(tableSortsAtom, tableScope);

// Store a **state** and reference to the container element
// so the state can re-render `TableBody`, preventing virtualization
// not detecting scroll if the container element was initially `null`
Expand Down Expand Up @@ -233,6 +241,18 @@ export default function Table({
containerRef,
]);

// apply user default sort on first render
const [applySort, setApplySort] = useState(true);
useEffect(() => {
if (applySort && Object.keys(tableSchema).length) {
const userDefaultSort = userSettings.tables?.[tableId]?.sorts || [];
setTableSorts(
userDefaultSort.length ? userDefaultSort : tableSchema.sorts || []
);
setApplySort(false);
}
}, [tableSchema, userSettings, tableId, setTableSorts, applySort]);

return (
<div
ref={(el) => setContainerEl(el)}
Expand Down
2 changes: 0 additions & 2 deletions src/sources/TableSourceFirestore/TableSourceFirestore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { getTableSchemaPath } from "@src/utils/table";
import { TableSchema } from "@src/types/table";
import { firebaseDbAtom } from "@src/sources/ProjectSourceFirebase";
import { projectScope } from "@src/atoms/projectScope";
import useApplySorts from "./useApplySorts";

/**
* When rendered, provides atom values for top-level tables and sub-tables
Expand Down Expand Up @@ -142,7 +141,6 @@ export const TableSourceFirestore = memo(function TableSourceFirestore() {
}
);

useApplySorts();
useAuditChange();
useBulkWriteDb();

Expand Down
35 changes: 0 additions & 35 deletions src/sources/TableSourceFirestore/useApplySorts.ts

This file was deleted.

0 comments on commit 2301718

Please sign in to comment.