cleaned up multinode stlying
This commit is contained in:
1 parent
8d804add13
commit
bfc95db6f6
14 files changed
+402
-69
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import "./index.css";
|
||||
import { FC, useState } from 'react';
|
||||
import { AutomationNode, AutomationNodeMapping } from 'types/automations';
|
||||
import { NodeEditor } from 'components/NodeEditor';
|
||||
@@ -39,64 +40,53 @@ export interface MultiNodeEditorProps {
|
||||
export const MultiNodeEditor: FC<MultiNodeEditorProps> = ({
|
||||
sequence: initialState,
|
||||
allowedTypes = ['action', 'condition'],
|
||||
onClose = () => { },
|
||||
onClose: _onClose = () => { },
|
||||
onSave = () => { },
|
||||
}) => {
|
||||
// state
|
||||
const [{ slide, state, isNodeModified, isModified, isReady }, setState] = useState({
|
||||
const [{ slide, state, isModified, isReady }, setState] = useState({
|
||||
slide: 0,
|
||||
state: initialState,
|
||||
isModified: false,
|
||||
isNodeModified: false,
|
||||
isReady: true,
|
||||
})
|
||||
// alias
|
||||
const areYouSureItsModified = () => isNodeModified ?
|
||||
window.confirm("You've made changes, by pressing this you will lose them!") :
|
||||
true
|
||||
const areYouSureItsNotReady = () => !isReady ?
|
||||
window.confirm("The node is not ready, are you sure you want to move?") :
|
||||
true
|
||||
const areYouSureItsModified = () => !isModified ?
|
||||
window.confirm("If you close this without saving, you will lose all progress!") :
|
||||
true
|
||||
const onClose = () => areYouSureItsModified() ?
|
||||
_onClose() :
|
||||
null
|
||||
const onAdd = () => setState({
|
||||
slide: state.length,
|
||||
state: [...state, makeDefault(allowedTypes[0])],
|
||||
isNodeModified: false,
|
||||
isReady,
|
||||
isModified: true,
|
||||
});
|
||||
const onRemove = () => setState({
|
||||
slide: slide > 0 ? slide - 1 : 0,
|
||||
state: [...state.slice(0, slide), ...state.slice(slide + 1)],
|
||||
isNodeModified: false,
|
||||
isReady,
|
||||
isModified: true,
|
||||
});
|
||||
const onNodeSave = (n: AutomationNode) => setState({
|
||||
slide,
|
||||
state: [...state.slice(0, slide), n, ...state.slice(slide + 1)],
|
||||
isNodeModified: false,
|
||||
isReady,
|
||||
isModified: true,
|
||||
});
|
||||
const onSaveAll = () => isNodeModified ?
|
||||
window.alert("Please save the node data first!") :
|
||||
onSave(state)
|
||||
const onAllSave = (n: AutomationNode) => onSave([
|
||||
...state.slice(0, slide), n, ...state.slice(slide + 1)
|
||||
]);
|
||||
const onNextOrAdd = () => {
|
||||
if (!areYouSureItsModified()) {
|
||||
return
|
||||
}
|
||||
if (!areYouSureItsNotReady()) {
|
||||
return
|
||||
}
|
||||
if (slide >= state.length - 1) {
|
||||
onAdd()
|
||||
} else {
|
||||
setState({ slide: slide + 1, state, isNodeModified, isReady, isModified, })
|
||||
setState({ slide: slide + 1, state, isReady, isModified, })
|
||||
}
|
||||
}
|
||||
const onBack = () => areYouSureItsModified() &&
|
||||
areYouSureItsNotReady() &&
|
||||
setState({ slide: slide - 1, state, isNodeModified, isReady, isModified, })
|
||||
const onBack = () => areYouSureItsNotReady() &&
|
||||
setState({ slide: slide - 1, state, isReady, isModified })
|
||||
// components
|
||||
const nextOrAddBtn = <Button
|
||||
onClick={onNextOrAdd}
|
||||
@@ -117,24 +107,15 @@ export const MultiNodeEditor: FC<MultiNodeEditorProps> = ({
|
||||
{nextOrAddBtn}
|
||||
</div>
|
||||
<div className='column center' style={{ marginTop: '1em' }}>
|
||||
<Button onClick={() => areYouSureItsModified() && onClose()}>Close</Button>
|
||||
<Button onClick={onClose}>Close</Button>
|
||||
<Button onClick={() => onSave(state)}>Save</Button>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
return <>
|
||||
<div style={{
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
justifyContent: 'space-evenly',
|
||||
alignItems: 'center',
|
||||
minWidth: '20vw',
|
||||
}}>
|
||||
return <div className={["multinode-editor", isModified ? "modded" : ''].join(' ')}>
|
||||
<div className="multinode-editor--navbar">
|
||||
<Button onClick={onBack} disabled={slide === 0}>{"<<"}</Button>
|
||||
<Button
|
||||
onClick={onRemove}
|
||||
>
|
||||
Delete
|
||||
</Button>
|
||||
<Button onClick={onRemove}> Delete</Button>
|
||||
{nextOrAddBtn}
|
||||
</div>
|
||||
<NodeEditor
|
||||
@@ -142,19 +123,14 @@ export const MultiNodeEditor: FC<MultiNodeEditorProps> = ({
|
||||
node={state[slide]}
|
||||
allowedTypes={allowedTypes}
|
||||
onClose={onClose}
|
||||
onSave={onNodeSave}
|
||||
onSave={onAllSave}
|
||||
onFlags={
|
||||
(ir, inm) => ((inm !== isNodeModified) || (ir !== isReady)) &&
|
||||
setState({ slide, state, isNodeModified: inm, isReady: ir, isModified })
|
||||
(ir) => (ir !== isReady) &&
|
||||
setState({ slide, state, isReady: ir, isModified })
|
||||
}
|
||||
>
|
||||
<Button onClick={onSaveAll} style={{
|
||||
borderColor: isModified ? "var(--change)" : undefined,
|
||||
color: isModified ? "var(--change)" : undefined,
|
||||
}}>Save All</Button>
|
||||
</NodeEditor>
|
||||
/>
|
||||
<div className='column center'>
|
||||
<span>{slide + 1}/{state.length}</span>
|
||||
</div>
|
||||
</>
|
||||
</div>
|
||||
}
|
||||
Reference in new issue
Block a user