From 8af774d80fd1b1aee7fe5431027ff896d180de80 Mon Sep 17 00:00:00 2001 From: Shams mosowi Date: Fri, 9 Oct 2020 11:13:14 +1100 Subject: [PATCH] actionscript: subtable support --- .../functions/src/actionScript/index.ts | 8 ++-- .../functions/src/functionConfig.ts | 47 +------------------ .../functions/src/generateConfig.ts | 2 +- .../components/Table/formatters/Action.tsx | 6 ++- www/src/hooks/useFiretable/useTableConfig.ts | 10 +--- www/src/util/fns.ts | 8 ++++ 6 files changed, 21 insertions(+), 60 deletions(-) diff --git a/cloud_functions/functions/src/actionScript/index.ts b/cloud_functions/functions/src/actionScript/index.ts index 2de188ae..fd41cb85 100644 --- a/cloud_functions/functions/src/actionScript/index.ts +++ b/cloud_functions/functions/src/actionScript/index.ts @@ -10,6 +10,7 @@ type ActionData = { parentId: string; tablePath: string; }; + schemaDocPath?: string; row: any; column: any; action: "run" | "redo" | "undo"; @@ -43,10 +44,11 @@ export const actionScript = functions.https.onCall( throw Error(`You are unauthenticated`); } - const { ref, row, column, action } = data; + const { ref, row, column, action, schemaDocPath } = data; - const schemaDocPath = generateSchemaDocPath(ref.tablePath); - const schemaDoc = await db.doc(schemaDocPath).get(); + const _schemaDocPath = + schemaDocPath ?? generateSchemaDocPath(ref.tablePath); + const schemaDoc = await db.doc(_schemaDocPath).get(); const schemaDocData = schemaDoc.data(); if (!schemaDocData) { return { diff --git a/cloud_functions/functions/src/functionConfig.ts b/cloud_functions/functions/src/functionConfig.ts index 2c980256..1481863e 100644 --- a/cloud_functions/functions/src/functionConfig.ts +++ b/cloud_functions/functions/src/functionConfig.ts @@ -1,45 +1,2 @@ -export default [ - { - fieldName: "reviewsSummary", - eval: (db) => async ({ - aggregateState, - incrementor, - triggerType, - change, - afterData, - beforeData, - }) => { - //triggerType: create | update | delete - //aggregateState: the subtable accumenlator stored in the cell of this column - //change: the triggered document change snapshot of the the subcollection - //afterData - //beforeData - //incrementor: short for firebase.firestore.FieldValue.increment(n); - //This script needs to return the new aggregateState cell value. - switch (triggerType) { - case "create": - const rating = afterData?.rating ?? 0; - return { - numberOfReviews: incrementor(1), - accumulatedStars: incrementor(rating), - }; - case "update": - const prevRating = beforeData?.rating ?? 0; - const newRating = afterData?.rating ?? 0; - return { - accumulatedStars: incrementor(newRating - prevRating), - }; - case "delete": - const removeStars = -(beforeData?.rating ?? 0); - return { - numberOfReviews: incrementor(-1), - accumulatedStars: incrementor(removeStars), - }; - default: - return {}; - } - }, - subtables: ["reviews"], - }, -]; -export const collectionPath = "mvpResources"; +export default []; +export const collectionPath = ""; diff --git a/cloud_functions/functions/src/generateConfig.ts b/cloud_functions/functions/src/generateConfig.ts index 3ed9f7c1..e0482c3e 100644 --- a/cloud_functions/functions/src/generateConfig.ts +++ b/cloud_functions/functions/src/generateConfig.ts @@ -38,7 +38,7 @@ if (serviceAccount) { return `${acc}{ fieldName:'${currColumn.key}',eval:(db)=> async (row) =>{${ currColumn.config.script - }},listenerFields:[${currColumn.config.listenerFields + }},listenerFields:[${(currColumn.config.listenerFields ?? []) .map((f) => `"${f}"`) .join(",")}]},`; }, ``); diff --git a/www/src/components/Table/formatters/Action.tsx b/www/src/components/Table/formatters/Action.tsx index a849e207..6baa99fe 100644 --- a/www/src/components/Table/formatters/Action.tsx +++ b/www/src/components/Table/formatters/Action.tsx @@ -13,11 +13,11 @@ import { import PlayIcon from "@material-ui/icons/PlayArrow"; import RefreshIcon from "@material-ui/icons/Refresh"; import UndoIcon from "@material-ui/icons/Undo"; - +import { useFiretableContext } from "contexts/firetableContext"; import { SnackContext } from "contexts/snackContext"; import { cloudFunction } from "firebase/callables"; import { sanitiseCallableName, isUrl, sanitiseRowData } from "util/fns"; - +import { formatPath } from "../../../util/fns"; const useStyles = makeStyles((theme) => createStyles({ root: { padding: theme.spacing(0, 0.375, 0, 1.5) }, @@ -46,6 +46,7 @@ const getStateIcon = (actionState) => { export const ActionFab = ({ row, column, onSubmit, value }) => { const classes = useStyles(); + const { tableState } = useFiretableContext(); const { createdAt, updatedAt, id, ref, ...docData } = row; const { config } = column as any; @@ -69,6 +70,7 @@ export const ActionFab = ({ row, column, onSubmit, value }) => { row: sanitiseRowData(Object.assign({}, docData)), column, action, + schemaDocPath: formatPath(tableState?.tablePath ?? ""), }; cloudFunction( callableName, diff --git a/www/src/hooks/useFiretable/useTableConfig.ts b/www/src/hooks/useFiretable/useTableConfig.ts index 27736968..ed7450c7 100644 --- a/www/src/hooks/useFiretable/useTableConfig.ts +++ b/www/src/hooks/useFiretable/useTableConfig.ts @@ -7,17 +7,9 @@ import _sortBy from "lodash/sortBy"; import useDoc, { DocActions } from "../useDoc"; import { FieldType } from "constants/fields"; -import { arrayMover, isCollectionGroup } from "../../util/fns"; +import { arrayMover, formatPath } from "../../util/fns"; import { db, deleteField } from "../../firebase"; -const formatPathRegex = /\/[^\/]+\/([^\/]+)/g; - -const formatPath = (tablePath: string) => { - return `_FIRETABLE_/settings/${ - isCollectionGroup() ? "groupSchema" : "schema" - }/${tablePath.replace(formatPathRegex, "/subTables/$1")}`; -}; - export type ColumnConfig = { fieldName: string; key: string; diff --git a/www/src/util/fns.ts b/www/src/util/fns.ts index 170d18c7..5685af03 100644 --- a/www/src/util/fns.ts +++ b/www/src/util/fns.ts @@ -82,3 +82,11 @@ export const generateBiggerId = (id: string) => { return characters[indexOfFirstChar + 1] + makeId(id.length - 1); else return id[0] + generateBiggerId(id.substr(1, id.length - 1)); }; + +const formatPathRegex = /\/[^\/]+\/([^\/]+)/g; + +export const formatPath = (tablePath: string) => { + return `_FIRETABLE_/settings/${ + isCollectionGroup() ? "groupSchema" : "schema" + }/${tablePath.replace(formatPathRegex, "/subTables/$1")}`; +};