actionscript: subtable support

This commit is contained in:
Shams mosowi
2020-10-09 11:13:14 +11:00
parent 090303c36a
commit 8af774d80f
6 changed files with 21 additions and 60 deletions

View File

@@ -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 {

View File

@@ -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 = "";

View File

@@ -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(",")}]},`;
}, ``);

View File

@@ -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,

View File

@@ -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;

View File

@@ -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")}`;
};