removed all window.confirm
This commit is contained in:
1 parent
dc3470fdf5
commit
47f57c313e
2 files changed
+229
-172
No files matched your search
@@ -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>): MultiNodeEditorInternalState => ({
|
||||
slides: [],
|
||||
slide: 0,
|
||||
multiNode: {
|
||||
isModified: false,
|
||||
isSaved: false,
|
||||
return {
|
||||
get totalSlides() {
|
||||
return slides.length;
|
||||
},
|
||||
internalNodeModified: false,
|
||||
...props,
|
||||
})
|
||||
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>
|
||||
): MultiNodeEditorInternalState => ({
|
||||
slides: [],
|
||||
slide: 0,
|
||||
multiNode: {
|
||||
isModified: false,
|
||||
isSaved: false,
|
||||
},
|
||||
internalNodeModified: false,
|
||||
...props,
|
||||
});
|
||||
@@ -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<NodeEditorProps> = ({
|
||||
}) => {
|
||||
// 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<NodeEditorProps> = ({
|
||||
<div className="node-editor--footer">
|
||||
<Button
|
||||
className="node-editor--footer--close"
|
||||
onClick={() => areYouSureNotReady("close") && onClose()}
|
||||
onClick={() =>
|
||||
areYouSureNotReady("close").then((ok) => ok && onClose())
|
||||
}
|
||||
>
|
||||
Close
|
||||
</Button>
|
||||
<Button
|
||||
className="node-editor--footer--save"
|
||||
onClick={() => {
|
||||
if (areYouSureNotReady("save")) {
|
||||
onSave(state.data);
|
||||
}
|
||||
}}
|
||||
onClick={() =>
|
||||
areYouSureNotReady("save").then((ok) => ok && onSave(state.data))
|
||||
}
|
||||
title={!isReady ? "Some fields have not been properly filled up" : ""}
|
||||
>
|
||||
{saveBtnCreateText ? "Create" : "Save"}
|
||||
|
||||
Reference in new issue
Block a user