From 540373dbfd211820480bb94108c31a44d314dc37 Mon Sep 17 00:00:00 2001 From: Vishnu Nithin Reddy <78612244+nithinrdy@users.noreply.github.com> Date: Thu, 21 Sep 2023 20:51:21 +0530 Subject: [PATCH] Create atom to fetch `defaultTableSettings` from. Use atom in useSave*** hooks to determine if popups are to be shown (and if not, whether changes are to be applied automatically). --- src/atoms/projectScope/user.ts | 7 +++++ .../Table/ColumnHeader/useSaveTableSorts.tsx | 24 ++++++++++++++--- src/components/Table/useSaveColumnSizing.tsx | 26 +++++++++++++++++-- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/atoms/projectScope/user.ts b/src/atoms/projectScope/user.ts index 62bcc1756..e6337df73 100644 --- a/src/atoms/projectScope/user.ts +++ b/src/atoms/projectScope/user.ts @@ -30,6 +30,13 @@ export const themeOverriddenAtom = atomWithStorage( false ); +/** User's default table settings (affecting saving and popup behavior) */ +export const defaultTableSettingsAtom = atom((get) => { + const userSettings = get(userSettingsAtom); + console.log("defaultTableSettings", userSettings.defaultTableSettings); + return userSettings.defaultTableSettings; +}); + /** Customized base theme based on project and user settings */ export const customizedThemesAtom = atom((get) => { const publicSettings = get(publicSettingsAtom); diff --git a/src/components/Table/ColumnHeader/useSaveTableSorts.tsx b/src/components/Table/ColumnHeader/useSaveTableSorts.tsx index c0c239ed1..0d6e88216 100644 --- a/src/components/Table/ColumnHeader/useSaveTableSorts.tsx +++ b/src/components/Table/ColumnHeader/useSaveTableSorts.tsx @@ -1,5 +1,5 @@ import { useCallback, useState } from "react"; -import { useAtom } from "jotai"; +import { useAtom, useAtomValue } from "jotai"; import { SnackbarKey, useSnackbar } from "notistack"; import LoadingButton from "@mui/lab/LoadingButton"; @@ -11,17 +11,25 @@ import { tableScope, updateTableSchemaAtom, } from "@src/atoms/tableScope"; -import { projectScope, updateUserSettingsAtom } from "@src/atoms/projectScope"; +import { + defaultTableSettingsAtom, + projectScope, + updateUserSettingsAtom, +} from "@src/atoms/projectScope"; import { TableSort } from "@src/types/table"; function useSaveTableSorts(canEditColumns: boolean) { const [updateTableSchema] = useAtom(updateTableSchemaAtom, tableScope); const [updateUserSettings] = useAtom(updateUserSettingsAtom, projectScope); const [tableId] = useAtom(tableIdAtom, tableScope); + const defaultTableSettings = useAtomValue( + defaultTableSettingsAtom, + projectScope + ); const { enqueueSnackbar, closeSnackbar } = useSnackbar(); const [snackbarId, setSnackbarId] = useState(null); - // Offer to save when table sorts changes + // Offer to save when table sorts changes, depending on user settings const trigger = useCallback( (sorts: TableSort[]) => { if (!updateTableSchema) throw new Error("Cannot update table schema"); @@ -33,6 +41,15 @@ function useSaveTableSorts(canEditColumns: boolean) { }); } if (!canEditColumns) return; + // If the user has disabled the popup, return early + if (defaultTableSettings?.saveSortingPopupDisabled) { + // If the user has `automaticallyApplySorting` set to true, apply the sorting before returning + if (defaultTableSettings?.automaticallyApplySorting) { + const updateTable = async () => await updateTableSchema({ sorts }); + updateTable(); + } + return; + } if (snackbarId) { closeSnackbar(snackbarId); } @@ -57,6 +74,7 @@ function useSaveTableSorts(canEditColumns: boolean) { tableId, closeSnackbar, updateTableSchema, + defaultTableSettings, ] ); diff --git a/src/components/Table/useSaveColumnSizing.tsx b/src/components/Table/useSaveColumnSizing.tsx index af1a6e378..bae451185 100644 --- a/src/components/Table/useSaveColumnSizing.tsx +++ b/src/components/Table/useSaveColumnSizing.tsx @@ -1,5 +1,5 @@ import { useEffect, useState } from "react"; -import { useSetAtom } from "jotai"; +import { useAtomValue, useSetAtom } from "jotai"; import { useSnackbar } from "notistack"; import { useDebounce } from "use-debounce"; import { isEqual, isEmpty } from "lodash-es"; @@ -13,6 +13,10 @@ import { updateColumnAtom, IUpdateColumnOptions, } from "@src/atoms/tableScope"; +import { + defaultTableSettingsAtom, + projectScope, +} from "@src/atoms/projectScope"; import { DEBOUNCE_DELAY } from "./Table"; import { ColumnSizingState } from "@tanstack/react-table"; @@ -26,14 +30,31 @@ export function useSaveColumnSizing( ) { const { enqueueSnackbar, closeSnackbar } = useSnackbar(); const updateColumn = useSetAtom(updateColumnAtom, tableScope); + const defaultTableSettings = useAtomValue( + defaultTableSettingsAtom, + projectScope + ); // Debounce for saving to schema const [debouncedColumnSizing] = useDebounce(columnSizing, DEBOUNCE_DELAY, { equalityFn: isEqual, }); - // Offer to save when column sizing changes + // Offer to save when column sizing changes, depending on user settings useEffect(() => { if (!canEditColumns || isEmpty(debouncedColumnSizing)) return; + // If the user has disabled the popup, return early + if (defaultTableSettings?.saveColumnWidthPopupDisabled) { + // If the user has `automaticallyApplyColumnWidth` set to true, apply the column width before returning + if (defaultTableSettings?.automaticallyApplyColumnWidth) { + const updateTable = async () => { + for (const [key, value] of Object.entries(debouncedColumnSizing)) { + await updateColumn({ key, config: { width: value } }); + } + }; + updateTable(); + } + return; + } const snackbarId = enqueueSnackbar("Save column sizes for all users?", { action: ( @@ -52,6 +73,7 @@ export function useSaveColumnSizing( enqueueSnackbar, closeSnackbar, updateColumn, + defaultTableSettings, ]); return null;