mirror of
https://github.com/rowyio/rowy.git
synced 2026-02-24 04:01:17 +01:00
removed useUpdate; using try catch
This commit is contained in:
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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" });
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -75,13 +75,6 @@ export interface IEditorCellProps<T = any> extends IDisplayCellProps<T> {
|
||||
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<T = any> {
|
||||
/** The column config */
|
||||
@@ -95,7 +88,7 @@ export interface ISideDrawerFieldProps<T = any> {
|
||||
/** 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;
|
||||
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user