From f10eb6e65acb779363a421ac6bd6429df51efaae Mon Sep 17 00:00:00 2001 From: shamsmosowi Date: Wed, 3 Nov 2021 19:13:41 +1100 Subject: [PATCH] auditing --- .../Table/formatters/FinalColumn.tsx | 11 ++-- src/contexts/ProjectContext.tsx | 60 +++++++++++-------- src/firebase/firebaseui.ts | 8 --- src/hooks/useTable/useTableData.tsx | 17 ++++-- 4 files changed, 53 insertions(+), 43 deletions(-) diff --git a/src/components/Table/formatters/FinalColumn.tsx b/src/components/Table/formatters/FinalColumn.tsx index fa7d4278..04134d0b 100644 --- a/src/components/Table/formatters/FinalColumn.tsx +++ b/src/components/Table/formatters/FinalColumn.tsx @@ -28,11 +28,11 @@ export default function FinalColumn({ row }: FormatterProps) { useStyles(); const { requestConfirmation } = useConfirmation(); - const { tableActions, addRow } = useProjectContext(); + const { deleteRow, addRow } = useProjectContext(); const altPress = useKeyPress("Alt"); - - const handleDelete = () => tableActions!.row.delete(row.id); - + const handleDelete = () => { + if (deleteRow) deleteRow(row.id); + }; return ( @@ -48,7 +48,7 @@ export default function FinalColumn({ row }: FormatterProps) { Object.keys(clonedRow).forEach((key) => { if (clonedRow[key] === undefined) delete clonedRow[key]; }); - if (tableActions) addRow!(clonedRow); + if (addRow) addRow!(clonedRow); }} aria-label="Duplicate row" className="row-hover-iconButton" @@ -61,7 +61,6 @@ export default function FinalColumn({ row }: FormatterProps) { , ignoreRequiredFields?: boolean) => void; + deleteRow: (rowId) => void; updateCell: ( ref: firebase.firestore.DocumentReference, fieldName: string, @@ -137,7 +138,28 @@ export const ProjectContextProvider: React.FC = ({ children }) => { : [], [tables] ); - + const auditChange = ( + type: "ADD_ROW" | "UPDATE_CELL" | "DELETE_ROW", + rowId, + data + ) => { + if (table?.audit !== false) { + _rowyRun({ + route: runRoutes.auditChange, + body: { + rowyUser: rowyUser(currentUser!), + type, + ref: { + rowPath: tableState.tablePath, + rowId, + tableId: table?.id, + collectionPath: tableState.tablePath, + }, + data, + }, + }); + } + }; const addRow: IProjectContext["addRow"] = (data, ignoreRequiredFields) => { const valuesFromFilter = tableState.filters.reduce((acc, curr) => { if (curr.operator === "==") { @@ -170,19 +192,12 @@ export const ProjectContextProvider: React.FC = ({ children }) => { initialData[table?.auditFieldUpdatedBy || "_updatedBy"] = rowyUser( currentUser! ); - // _rowyRun({route:runRoutes.auditChange,body:{ - // rowyUser, - // eventType:"ADD_ROW", - // eventData:{ - // rowPath:ref.path, - // tableId:table?.id, - // } - // }}) } tableActions.row.add( { ...valuesFromFilter, ...initialData, ...data }, - ignoreRequiredFields ? [] : requiredFields + ignoreRequiredFields ? [] : requiredFields, + (rowId: string) => auditChange("ADD_ROW", rowId, {}) ); }; @@ -197,26 +212,16 @@ export const ProjectContextProvider: React.FC = ({ children }) => { const update = { [fieldName]: value }; if (table?.audit !== false) { - const _rowyUser = rowyUser(currentUser!, { updatedField: fieldName }); - update[table?.auditFieldUpdatedBy || "_updatedBy"] = _rowyUser; - _rowyRun({ - route: runRoutes.auditChange, - body: { - rowyUser: _rowyUser, - eventType: "UPDATE_CELL", - eventData: { - rowPath: ref.path, - tableId: table?.id, - updatedField: fieldName, - }, - }, - }); + update[table?.auditFieldUpdatedBy || "_updatedBy"] = rowyUser( + currentUser!, + { updatedField: fieldName } + ); } - tableActions.row.update( ref, update, () => { + auditChange("UPDATE_CELL", ref.id, { updatedField: fieldName }); if (onSuccess) onSuccess(ref, fieldName, value); }, (error) => { @@ -232,6 +237,10 @@ export const ProjectContextProvider: React.FC = ({ children }) => { } ); }; + + const deleteRow = (rowId) => { + tableActions.row.delete(rowId, () => auditChange("DELETE_ROW", rowId, {})); + }; // rowyRun access const _rowyRun: IProjectContext["rowyRun"] = async (args) => { const authToken = await getAuthToken(); @@ -272,6 +281,7 @@ export const ProjectContextProvider: React.FC = ({ children }) => { tableActions, addRow, updateCell, + deleteRow, settingsActions, settings: settings.doc, roles, diff --git a/src/firebase/firebaseui.ts b/src/firebase/firebaseui.ts index 64a75289..b7a9ae52 100644 --- a/src/firebase/firebaseui.ts +++ b/src/firebase/firebaseui.ts @@ -7,14 +7,6 @@ import githubLogo from "@src/assets/logos/github.svg"; import appleLogo from "@src/assets/logos/apple.svg"; import yahooLogo from "@src/assets/logos/yahoo.svg"; -import { mdiGoogle } from "@mdi/js"; -console.log( - `data:image/svg+xml;utf8,` + - encodeURIComponent( - `` - ) -); - export const authOptions = { google: { provider: firebase.auth.GoogleAuthProvider.PROVIDER_ID, diff --git a/src/hooks/useTable/useTableData.tsx b/src/hooks/useTable/useTableData.tsx index 5369dc1d..d8625916 100644 --- a/src/hooks/useTable/useTableData.tsx +++ b/src/hooks/useTable/useTableData.tsx @@ -231,12 +231,12 @@ const useTableData = () => { * @param rowIndex local position * @param documentId firestore document id */ - const deleteRow = (rowId: string) => { + const deleteRow = async (rowId: string, onSuccess: () => void) => { // Remove row locally rowsDispatch({ type: "delete", rowId }); // Delete document try { - db.collection(tableState.path).doc(rowId).delete(); + await db.collection(tableState.path).doc(rowId).delete().then(onSuccess); } catch (error: any) { console.log(error); if (error.code === "permission-denied") { @@ -264,7 +264,11 @@ const useTableData = () => { /** creating new document/row * @param data(optional: default will create empty row) */ - const addRow = async (data: any, requiredFields: string[]) => { + const addRow = ( + data: any, + requiredFields: string[], + onSuccess: (rowId: string) => void + ) => { const missingRequiredFields = requiredFields ? requiredFields.reduce(missingFieldsReducer(data), []) : []; @@ -274,7 +278,12 @@ const useTableData = () => { if (missingRequiredFields.length === 0) { try { - await db.collection(path).doc(newId).set(data, { merge: true }); + db.collection(path) + .doc(newId) + .set(data, { merge: true }) + .then(() => { + onSuccess(newId); + }); } catch (error: any) { if (error.code === "permission-denied") { enqueueSnackbar("You do not have the permissions to add new rows.", {