diff --git a/src/atoms/tableScope/rowActions.ts b/src/atoms/tableScope/rowActions.ts index df60ec0a..edcb8361 100644 --- a/src/atoms/tableScope/rowActions.ts +++ b/src/atoms/tableScope/rowActions.ts @@ -342,7 +342,7 @@ export interface IUpdateFieldOptions { /** Optionally, used to locate the row in ArraySubTable. */ arrayTableData?: ArrayTableRowData; /** Optionally, explicitly specify to use firestore's update method. Required in case of JSON field update. */ - useUpdate?: boolean; + // useUpdate?: boolean; } /** * Set function updates or deletes a field in a row. @@ -371,7 +371,6 @@ export const updateFieldAtom = atom( useArrayUnion, useArrayRemove, arrayTableData, - useUpdate, }: IUpdateFieldOptions ) => { const updateRowDb = get(_updateRowDbAtom); @@ -493,7 +492,7 @@ export const updateFieldAtom = atom( row._rowy_ref.path, omitRowyFields(newRowValues), deleteField ? [fieldName] : [], - { ...arrayTableData, useUpdate } + arrayTableData ); } } @@ -503,7 +502,7 @@ export const updateFieldAtom = atom( row._rowy_ref.path, omitRowyFields(dbUpdate), deleteField ? [fieldName] : [], - { ...arrayTableData, useUpdate } + arrayTableData ); } diff --git a/src/components/SideDrawer/MemoizedField.tsx b/src/components/SideDrawer/MemoizedField.tsx index 641895e0..ebf97b1c 100644 --- a/src/components/SideDrawer/MemoizedField.tsx +++ b/src/components/SideDrawer/MemoizedField.tsx @@ -3,11 +3,7 @@ import useStateRef from "react-usestateref"; import { isEqual, isEmpty } from "lodash-es"; import FieldWrapper from "./FieldWrapper"; -import { - FieldType, - IFieldConfig, - onSubmitOptions, -} from "@src/components/fields/types"; +import { FieldType, IFieldConfig } from "@src/components/fields/types"; import { getFieldProp } from "@src/components/fields"; import { ColumnConfig, TableRowRef } from "@src/types/table"; import { TableRow } from "@src/types/table"; @@ -20,7 +16,7 @@ export interface IMemoizedFieldProps { _rowy_ref: TableRowRef; isDirty: boolean; onDirty: (fieldName: string) => void; - onSubmit: (fieldName: string, value: any, options?: onSubmitOptions) => void; + onSubmit: (fieldName: string, value: any) => void; row: TableRow; } @@ -43,12 +39,9 @@ export const MemoizedField = memo( if (!isDirty) setLocalValue(value); }, [isDirty, setLocalValue, value]); - const handleSubmit = useCallback( - (options?: onSubmitOptions) => { - onSubmit(field.fieldName, localValueRef.current, options); - }, - [field.fieldName, localValueRef, onSubmit] - ); + const handleSubmit = useCallback(() => { + onSubmit(field.fieldName, localValueRef.current); + }, [field.fieldName, localValueRef, onSubmit]); let type = field.type; if (field.type !== FieldType.formula && field.config?.renderFieldType) { diff --git a/src/components/SideDrawer/SideDrawerFields.tsx b/src/components/SideDrawer/SideDrawerFields.tsx index f78047b3..f2ce5964 100644 --- a/src/components/SideDrawer/SideDrawerFields.tsx +++ b/src/components/SideDrawer/SideDrawerFields.tsx @@ -24,7 +24,6 @@ import { } from "@src/atoms/tableScope"; import { formatSubTableName } from "@src/utils/table"; import { TableRow } from "@src/types/table"; -import { onSubmitOptions } from "@src/components/fields/types"; export interface ISideDrawerFieldsProps { row: TableRow; @@ -55,7 +54,7 @@ export default function SideDrawerFields({ row }: ISideDrawerFieldsProps) { }, []); // Called when an individual field is ready to be saved const onSubmit = useCallback( - async (fieldName: string, value: any, options?: onSubmitOptions) => { + async (fieldName: string, value: any) => { if (!selectedCell) return; const currentValue = get(row, fieldName); @@ -75,7 +74,6 @@ export default function SideDrawerFields({ row }: ISideDrawerFieldsProps) { arrayTableData: { index: selectedCell.arrayIndex, }, - useUpdate: options?.useUpdateFn ?? false, }); setSaveState("saved"); diff --git a/src/components/Table/TableCell/EditorCellController.tsx b/src/components/Table/TableCell/EditorCellController.tsx index 68e95996..217c40eb 100644 --- a/src/components/Table/TableCell/EditorCellController.tsx +++ b/src/components/Table/TableCell/EditorCellController.tsx @@ -8,7 +8,6 @@ import { tableScope, updateFieldAtom } from "@src/atoms/tableScope"; import type { IDisplayCellProps, IEditorCellProps, - onSubmitOptions, } from "@src/components/fields/types"; interface IEditorCellControllerProps extends IDisplayCellProps { @@ -57,7 +56,7 @@ export default function EditorCellController({ }, [isDirty, localValueRef, setLocalValue, value]); // This is where we update the documents - const handleSubmit = async (options?: onSubmitOptions) => { + const handleSubmit = async () => { // props.disabled should always be false as withRenderTableCell would // render DisplayCell instead of EditorCell if (props.disabled || !isDirtyRef.current) return; @@ -68,7 +67,6 @@ export default function EditorCellController({ value: localValueRef.current, deleteField: localValueRef.current === undefined, arrayTableData: props.row?._rowy_ref.arrayTableData, - useUpdate: options?.useUpdateFn, }); } catch (e) { enqueueSnackbar((e as Error).message, { variant: "error" }); diff --git a/src/components/fields/Json/SideDrawerField.tsx b/src/components/fields/Json/SideDrawerField.tsx index bd2e8cb8..03a451c6 100644 --- a/src/components/fields/Json/SideDrawerField.tsx +++ b/src/components/fields/Json/SideDrawerField.tsx @@ -68,9 +68,7 @@ export default function Json({ const handleEdit = (edit: InteractionProps) => { onChange(edit.updated_src); - onSubmit({ - useUpdateFn: true, - }); + onSubmit(); }; return ( @@ -135,11 +133,7 @@ export default function Json({ }} onValidStatusUpdate={({ isValid }) => setCodeValid(isValid)} error={!codeValid} - onBlur={() => - onSubmit({ - useUpdateFn: true, - }) - } + onBlur={onSubmit} fullScreenTitle={ <> {config.icon} diff --git a/src/components/fields/types.ts b/src/components/fields/types.ts index 606382ce..0308112d 100644 --- a/src/components/fields/types.ts +++ b/src/components/fields/types.ts @@ -75,13 +75,6 @@ export interface IEditorCellProps extends IDisplayCellProps { parentRef: PopoverProps["anchorEl"]; } -export type onSubmitOptions = { - /** Use the update function instead of the set function */ - useUpdateFn?: boolean; -}; - -export type onSubmitFunction = (options?: onSubmitOptions) => void; - /** Props to be passed to all SideDrawerFields */ export interface ISideDrawerFieldProps { /** The column config */ @@ -95,7 +88,7 @@ export interface ISideDrawerFieldProps { /** Update the local value. Also calls onDirty */ onChange: (value: T) => void; /** Call when user input is ready to be saved (e.g. onBlur) */ - onSubmit: onSubmitFunction; + onSubmit: () => void; /** Field locked. Do NOT check `column.locked` */ disabled: boolean; diff --git a/src/hooks/useFirestoreCollectionWithAtom.ts b/src/hooks/useFirestoreCollectionWithAtom.ts index 9d256896..dc3520d2 100644 --- a/src/hooks/useFirestoreCollectionWithAtom.ts +++ b/src/hooks/useFirestoreCollectionWithAtom.ts @@ -258,7 +258,7 @@ export function useFirestoreCollectionWithAtom< if (updateDocAtom) { setUpdateDocAtom( () => - ( + async ( path: string, update: T, deleteFields?: string[], @@ -271,10 +271,13 @@ export function useFirestoreCollectionWithAtom< set(updateToDb as any, field, deleteField()); } } - if (options?.useUpdate) { - return updateDoc(doc(firebaseDb, path), updateToDb); + try { + return await updateDoc(doc(firebaseDb, path), updateToDb); + } catch (e) { + return await setDoc(doc(firebaseDb, path), updateToDb, { + merge: true, + }); } - return setDoc(doc(firebaseDb, path), updateToDb, { merge: true }); } ); }