callable support for action columns

This commit is contained in:
shamsmosowi
2021-09-27 15:27:58 +10:00
parent a1e99a6e88
commit 05369bda5f
5 changed files with 53 additions and 86 deletions

View File

@@ -23,7 +23,6 @@ import ArrowDropUpIcon from "@mui/icons-material/ArrowDropUp";
import { useConfirmation } from "components/ConfirmationDialog/Context";
import { useProjectContext } from "contexts/ProjectContext";
import { formatPath } from "utils/fns";
import { cloudFunction } from "firebase/callables";
const useStyles = makeStyles((theme) =>
createStyles({
@@ -141,8 +140,6 @@ export default function BulkActions({ selectedRows, columns, clearSelection }) {
const executeAction = async (key: string, actionType: string) => {
const actionColumn = _find(actionColumns, { key });
if (!actionColumn) return;
console.log({ actionColumn, selectedRows, actionType });
const callableName = actionColumn.config.callableName ?? "actionScript";
const calls = selectedRows.map((row) => {
@@ -158,25 +155,26 @@ export default function BulkActions({ selectedRows, columns, clearSelection }) {
schemaDocPath: formatPath(tableState?.tablePath ?? ""),
actionParams: {},
};
return cloudFunction(
callableName,
data,
async (response) => {
const { message, cellValue, success } = response.data;
// setIsRunning(false);
enqueueSnackbar(JSON.stringify(message), {
variant: success ? "success" : "error",
});
if (cellValue && cellValue.status) {
return ref.update({ [actionColumn.key]: cellValue });
}
},
(error) => {
console.error("ERROR", callableName, error);
//setIsRunning(false);
enqueueSnackbar(JSON.stringify(error), { variant: "error" });
}
);
return true;
// cloudFunction(
// callableName,
// data,
// async (response) => {
// const { message, cellValue, success } = response.data;
// // setIsRunning(false);
// enqueueSnackbar(JSON.stringify(message), {
// variant: success ? "success" : "error",
// });
// if (cellValue && cellValue.status) {
// return ref.update({ [actionColumn.key]: cellValue });
// }
// },
// (error) => {
// console.error("ERROR", callableName, error);
// //setIsRunning(false);
// enqueueSnackbar(JSON.stringify(error), { variant: "error" });
// }
// );
});
setLoading(true);
const result = await Promise.all(calls);

View File

@@ -17,7 +17,6 @@ import Select from "@mui/material/Select";
import Switch from "@mui/material/Switch";
import CodeEditor from "../editors/CodeEditor";
import { useProjectContext } from "contexts/ProjectContext";
import { WEBHOOK_URL } from "../../../firebase";
import { makeId } from "../../../utils/fns";
const useStyles = makeStyles((theme) =>
@@ -156,8 +155,8 @@ export default function WebhooksDialog({ open, handleClose }) {
<>
<Typography variant="overline">Web hook url:</Typography>
<Typography variant="body1">
{WEBHOOK_URL}?tablePath={tableState?.tablePath}
&type=TYPE_FORM&secret={state.secret}
{/* {WEBHOOK_URL}?tablePath={tableState?.tablePath}
&type=TYPE_FORM&secret={state.secret} */}
</Typography>
<Typography variant="overline">instructions:</Typography>
<Typography variant="body1">

View File

@@ -8,7 +8,7 @@ import RefreshIcon from "@mui/icons-material/Refresh";
import UndoIcon from "@mui/icons-material/Undo";
import { useProjectContext } from "contexts/ProjectContext";
import { cloudFunction } from "firebase/callables";
import { functions } from "@src/firebase";
import { formatPath } from "utils/fns";
import { useConfirmation } from "components/ConfirmationDialog";
import { useActionParams } from "./FormDialog/Context";
@@ -65,45 +65,44 @@ export default function ActionFab({
const callableName: string =
(column as any).callableName ?? config.callableName ?? "actionScript";
const handleRun = async (actionParams = null) => {
const fnParams = (actionParams = null) => ({
ref: { path: ref.path },
column: { ...column, editor: undefined },
action,
schemaDocPath: formatPath(tableState?.tablePath ?? ""),
actionParams,
});
const handleActionScript = async (data) => {
if (!rowyRun) return;
setIsRunning(true);
const data = {
ref: { path: ref.path, id: ref.id, tablePath: window.location.pathname },
column: { ...column, editor: undefined },
action,
schemaDocPath: formatPath(tableState?.tablePath ?? ""),
actionParams,
};
const resp = await rowyRun({
route: runRoutes.actionScript,
body: data,
params: [],
});
const { message, success } = resp;
return resp;
};
const handleCallableAction = async (data) => {
const resp: any = await functions.httpsCallable(callableName)(data);
return resp.data;
};
const handleRun = async (actionParams = null) => {
setIsRunning(true);
const data = fnParams(actionParams);
let result;
if (callableName === "actionScript") {
result = await handleActionScript(data);
} else {
result = await handleCallableAction(data);
}
console.log(result);
const { message, success } = result;
setIsRunning(false);
enqueueSnackbar(JSON.stringify(message), {
variant: success ? "success" : "error",
});
// cloudFunction(
// callableName,
// data,
// async (response) => {
// const { message, cellValue, success } = response.data;
// setIsRunning(false);
// enqueueSnackbar(JSON.stringify(message), {
// variant: success ? "success" : "error",
// });
// },
// (error) => {
// console.error("ERROR", callableName, error);
// setIsRunning(false);
// enqueueSnackbar(JSON.stringify(error), { variant: "error" });
// }
// );
};
const hasRan = value && value.status;
@@ -129,7 +128,7 @@ export default function ActionFab({
: needsConfirmation
? () =>
requestConfirmation({
title: `${column.name as string} Confirmation`,
title: `${column.name ?? column.key} Confirmation`,
body: (actionState === "undo" && config.undoConfirmation
? config.undoConfirmation
: config.confirmation

View File

@@ -1,26 +0,0 @@
import { functions } from "./index";
export enum CLOUD_FUNCTIONS {
getAlgoliaSearchKey = "getAlgoliaSearchKey",
}
export const cloudFunction = (
name: string,
input: any,
success?: Function,
fail?: Function
) =>
new Promise((resolve, reject) => {
const callable = functions.httpsCallable(name);
callable(input)
.then((result) => {
if (success) {
resolve(success(result));
}
})
.catch((error) => {
if (fail) {
reject(fail(error));
}
});
});

View File

@@ -21,9 +21,6 @@ export const bucket = firebase.storage();
export const functions = firebase.functions();
export const projectId = process.env.REACT_APP_FIREBASE_PROJECT_ID!;
export const WEBHOOK_URL = `https://${(functions as any).region_}-${
appConfig.projectId
}.cloudfunctions.net/webhook`;
export const googleProvider =
new firebase.auth.GoogleAuthProvider().setCustomParameters({