From 47f57c313ec6ba587905e15438c605c80331d0b4 Mon Sep 17 00:00:00 2001 From: Ari Date: Wed, 29 Jun 2022 21:20:37 -0400 Subject: [PATCH] removed all window.confirm --- .../src/components/MultiNodeEditors/state.ts | 347 ++++++++++-------- webapp/src/components/NodeEditor/index.tsx | 54 ++- 2 files changed, 229 insertions(+), 172 deletions(-) diff --git a/webapp/src/components/MultiNodeEditors/state.ts b/webapp/src/components/MultiNodeEditors/state.ts index 972f4ba..872c047 100644 --- a/webapp/src/components/MultiNodeEditors/state.ts +++ b/webapp/src/components/MultiNodeEditors/state.ts @@ -1,167 +1,202 @@ import { MultiNodeEditorProps } from "./types"; -import { useState, useRef } from 'react'; -import { AutomationNode } from '../../types/automations/index'; +import { useState, useRef } from "react"; +import { AutomationNode } from "../../types/automations/index"; import { makeDefault } from "utils/automations"; - +import { useConfirm, ConfirmOptions } from "material-ui-confirm"; +import { useSnackbar } from "notistack"; export type MultiNodeEditorInternalState = { - slides: AutomationNode[]; - slide: number; - multiNode: { - isModified: boolean, - isSaved: boolean, - }, - internalNodeModified: boolean, + slides: AutomationNode[]; + slide: number; + multiNode: { + isModified: boolean; + isSaved: boolean; + }; + internalNodeModified: boolean; }; export const useMultiNodeEditorState = (props: MultiNodeEditorProps) => { - const [state, _setState] = useState(makeInitialState({ - slides: props.sequence - })) - const justSaved = useRef(false); + // state + const confirm = useConfirm(); + const snackbr = useSnackbar(); + const [state, _setState] = useState( + makeInitialState({ + slides: props.sequence, + }) + ); + const justSaved = useRef(false); - const { - slide, - slides, - multiNode, - internalNodeModified, - } = state; - const setState = (a: MultiNodeEditorInternalState) => { - _setState(a) + // alias + const { slide, slides, multiNode, internalNodeModified } = state; + const setState = (a: MultiNodeEditorInternalState) => { + _setState(a); + }; + const askThenDo = async ( + options: ConfirmOptions, + action: () => void, + condition = () => internalNodeModified && !multiNode.isSaved + ) => { + if (condition()) { + return await confirm(options) + .then(action) + .catch(() => { + snackbr.enqueueSnackbar("Cancelling...", { + variant: "info", + }); + }); + } else { + return action(); } - return { - get totalSlides() { - return slides.length - }, - get currentSlideNumber() { - return slide - }, - get currentSlide() { - return slides[slide] - }, - get isLast() { - return slide >= slides.length - 1 - }, - get isEmpty() { - return slides.length === 0 - }, - get isModified() { - return (internalNodeModified || multiNode.isModified) && !multiNode.isSaved - }, - moveBack() { - if (slide <= 0) { - return - } - if (internalNodeModified && !multiNode.isSaved) { - if (!window.confirm("Moving back without saving will cause you to lose the work, are you sure you want to do that?")) { - return - } - } - setState({ ...state, slide: slide - 1, internalNodeModified: false, }) - }, - moveForward() { - const createNew = slide >= slides.length - 1; - if (internalNodeModified && !multiNode.isSaved) { - const action = createNew ? 'Creating a new new node' : 'Moving forward' - if (!window.confirm(`${action} without saving will cause you to lose the work, are you sure you want to do that?`)) { - return - } - } - if (createNew) { - setState({ - ...state, - slide: slides.length, - slides: slides.concat([makeDefault(props.allowedTypes[0])]), - internalNodeModified: false, - multiNode: { - isModified: true, - isSaved: false, - } - }) - } else { - setState({ ...state, slide: slide + 1, internalNodeModified: false, }) - } - }, - updateInternalNode(isReady: boolean, isModified: boolean) { - if (!isReady || isModified) { - if (!state.multiNode.isModified || state.multiNode.isSaved) { - if (justSaved.current) { - justSaved.current = false - } else { - setState({ - ...state, - multiNode: { isModified: true, isSaved: false, }, - internalNodeModified: true, - }) - } - } - } - }, - onRemove() { - setState({ - ...state, - slide: Math.max(slide - 1, 0), - slides: slides - .slice(0, slide) - .concat( - slides.slice(slide + 1) - ), - multiNode: { - isModified: true, - isSaved: false, - }, - }) - }, - onSave(node: AutomationNode) { - justSaved.current = true - setState({ - ...state, - slides: slides - .slice(0, slide) - .concat([node]) - .concat( - slides.slice(slide + 1) - ), - multiNode: { - isModified: multiNode.isModified, - isSaved: true, - }, - internalNodeModified: false, - }) - }, - onSaveEmpty() { - justSaved.current = true - setState({ - ...state, - multiNode: { - isModified: multiNode.isModified, - isSaved: true, - }, - internalNodeModified: false, - }) - }, - onClose() { - if (!multiNode.isSaved && multiNode.isModified) { - if (!window.confirm("You haven't saved your work, are you ok with losing it?")) { - return - } - props.onSave && props.onSave(slides) - } - if (justSaved.current) { - props.onSave && props.onSave(slides) - } - props.onClose && props.onClose() - } - } -} + }; -const makeInitialState = (props: Partial): MultiNodeEditorInternalState => ({ - slides: [], - slide: 0, - multiNode: { - isModified: false, - isSaved: false, + return { + get totalSlides() { + return slides.length; }, - internalNodeModified: false, - ...props, -}) \ No newline at end of file + get currentSlideNumber() { + return slide; + }, + get currentSlide() { + return slides[slide]; + }, + get isLast() { + return slide >= slides.length - 1; + }, + get isEmpty() { + return slides.length === 0; + }, + get isModified() { + return ( + (internalNodeModified || multiNode.isModified) && !multiNode.isSaved + ); + }, + moveBack() { + if (slide <= 0) { + return; + } + return askThenDo( + { + description: + "Moving back without saving will cause you to lose the work, are you sure you want to do that?", + }, + () => + setState({ + ...state, + slide: slide - 1, + internalNodeModified: false, + }) + ); + }, + moveForward() { + const createNew = slide >= slides.length - 1; + const action = createNew ? "Creating a new new node" : "Moving forward"; + return askThenDo( + { + description: `${action} without saving will cause you to lose the work, are you sure you want to do that?`, + }, + () => { + if (createNew) { + setState({ + ...state, + slide: slides.length, + slides: slides.concat([makeDefault(props.allowedTypes[0])]), + internalNodeModified: false, + multiNode: { + isModified: true, + isSaved: false, + }, + }); + } else { + setState({ + ...state, + slide: slide + 1, + internalNodeModified: false, + }); + } + } + ); + }, + updateInternalNode(isReady: boolean, isModified: boolean) { + if (!isReady || isModified) { + if (!state.multiNode.isModified || state.multiNode.isSaved) { + if (justSaved.current) { + justSaved.current = false; + } else { + setState({ + ...state, + multiNode: { isModified: true, isSaved: false }, + internalNodeModified: true, + }); + } + } + } + }, + onRemove() { + setState({ + ...state, + slide: Math.max(slide - 1, 0), + slides: slides.slice(0, slide).concat(slides.slice(slide + 1)), + multiNode: { + isModified: true, + isSaved: false, + }, + }); + }, + onSave(node: AutomationNode) { + justSaved.current = true; + setState({ + ...state, + slides: slides + .slice(0, slide) + .concat([node]) + .concat(slides.slice(slide + 1)), + multiNode: { + isModified: multiNode.isModified, + isSaved: true, + }, + internalNodeModified: false, + }); + }, + onSaveEmpty() { + justSaved.current = true; + setState({ + ...state, + multiNode: { + isModified: multiNode.isModified, + isSaved: true, + }, + internalNodeModified: false, + }); + }, + async onClose() { + if (!multiNode.isSaved && multiNode.isModified) { + try { + await confirm({ + description: + "You haven't saved your work, are you ok with losing it?", + }); + props.onSave && props.onSave(slides); + } catch (_: any) { + return; + } + } + if (justSaved.current) { + props.onSave && props.onSave(slides); + } + props.onClose && props.onClose(); + }, + }; +}; + +const makeInitialState = ( + props: Partial +): MultiNodeEditorInternalState => ({ + slides: [], + slide: 0, + multiNode: { + isModified: false, + isSaved: false, + }, + internalNodeModified: false, + ...props, +}); diff --git a/webapp/src/components/NodeEditor/index.tsx b/webapp/src/components/NodeEditor/index.tsx index 62279a7..4539519 100644 --- a/webapp/src/components/NodeEditor/index.tsx +++ b/webapp/src/components/NodeEditor/index.tsx @@ -6,6 +6,7 @@ import { useEditorNodeState } from "./OptionManager"; import { InputList } from "components/Inputs/InputList"; import { Button } from "components/Inputs/Button"; import InputBoolean from "components/Inputs/InputBoolean"; +import { useConfirm } from "material-ui-confirm"; export interface NodeEditorProps { node: AutomationNode; @@ -30,20 +31,41 @@ export const NodeEditor: FC = ({ }) => { // state const state = useEditorNodeState(node, isErrored, allowedTypes); + const confirm = useConfirm(); // alias const isModified = state.isModified; const isReady = state.isReady(); - const areYouSureNotReady = (what: string) => - !isReady - ? window.confirm( - `This node is missing some values, are you sure you want to ${what}?` - ) - : state.isErrored - ? window.confirm( - `This node contains errors, are you sure you want to ${what}?` - ) - : true; + const areYouSureNotReady = async (what: string) => { + if (isModified) { + try { + await confirm({ + description: `This node is has some unsaved work, are you sure you want to ${what}?`, + }); + } catch (_: any) { + return false; + } + } + if (!isReady) { + try { + await confirm({ + description: `This node is missing some values, are you sure you want to ${what}?`, + }); + } catch (_: any) { + return false; + } + } + if (state.isErrored) { + try { + await confirm({ + description: `This node contains errors, are you sure you want to ${what}?`, + }); + } catch (_: any) { + return false; + } + } + return true; + }; // events onFlags(isReady, isModified); @@ -103,17 +125,17 @@ export const NodeEditor: FC = ({