From 831de545a2d8a8814b6ba2ed991fec6842ffbfa3 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Tue, 16 Nov 2021 21:17:22 -0500 Subject: [PATCH] nested updater --- .../ConditionEditor/ConditionNode.tsx | 19 +++++-- .../ConditionEditor/editorRender.tsx | 46 +---------------- web/src/components/ConditionEditor/index.tsx | 4 +- .../ConditionEditor/nestedUpdater.ts | 49 +++++++++++++++++++ web/src/components/ConditionEditor/style.ts | 17 ++++--- .../components/ConditionEditor/viewRender.tsx | 15 ++++-- 6 files changed, 87 insertions(+), 63 deletions(-) create mode 100644 web/src/components/ConditionEditor/nestedUpdater.ts diff --git a/web/src/components/ConditionEditor/ConditionNode.tsx b/web/src/components/ConditionEditor/ConditionNode.tsx index a5ec662..44dec07 100644 --- a/web/src/components/ConditionEditor/ConditionNode.tsx +++ b/web/src/components/ConditionEditor/ConditionNode.tsx @@ -1,6 +1,7 @@ import { Classes } from "jss"; -import { FC } from "react"; +import { FC, useState } from "react"; import { AutomationCondition } from "~/automations/types/conditions"; +import { CheckMarkIcon, PencilIcon } from "~/icons/icons"; import { getEditor } from "./editorRender"; import { useStyles } from "./style"; import { getViewer } from "./viewRender"; @@ -26,11 +27,18 @@ export const ConditionNode: FC<{ onAddChild=() => {}, children=() => {}, }) => { + // state + const [internalDisplayMode, setInternalDisplayMode] = useState(true); + // alias + const effectiveDM = displayMode && internalDisplayMode; + console.log({effectiveDM}) // children let childrenConditions: JSX.Element; - if (displayMode) { + if (effectiveDM) { const Viewer = getViewer(condition); - childrenConditions = + childrenConditions = { + onUpdate(data) + }}/> } else { const Editor = getEditor(condition); childrenConditions = { @@ -53,9 +61,12 @@ export const ConditionNode: FC<{ return
- {(showDelete || !displayMode) && onDelete('root')}/>} + {(showDelete || !effectiveDM) && onDelete('root')}/>}
{condition.condition.replace('_', ' ')} +
{childrenConditions} diff --git a/web/src/components/ConditionEditor/editorRender.tsx b/web/src/components/ConditionEditor/editorRender.tsx index a7eb950..b8260db 100644 --- a/web/src/components/ConditionEditor/editorRender.tsx +++ b/web/src/components/ConditionEditor/editorRender.tsx @@ -6,6 +6,7 @@ import InputText from "../Inputs/InputText"; import InputTextArea from "../Inputs/InputTextArea"; import { InputEntity } from "../Inputs/InputTextBubble"; import { ConditionNode } from "./ConditionNode"; +import { genUpdateMethods } from "./nestedUpdater"; interface Editor extends FC<{ @@ -127,50 +128,7 @@ export const LogicViewer: Editor = ({ key={i} condition={c} displayMode={false} - onUpdate={update => onChange({ - ...condition, - condition_data: { - ...condition.condition_data, - conditions: [ - ...condition.condition_data.conditions.slice(0, i), - update, - ...condition.condition_data.conditions.slice(i+1) - ] - } - })} - onDelete={which => { - if (which === 'root') { - onChange({ - ...condition, - condition_data: { - ...condition.condition_data, - conditions: condition.condition_data.conditions.slice(0, i).concat( - condition.condition_data.conditions.slice(i+1) - ) - } - }) - } else { - const data = (c as LogicCondition).condition_data; - const update = { - ...condition.condition_data, - conditions: [ - ...condition.condition_data.conditions.slice(0, i), - { - ...c, - condition_data: { - ...data, - conditions: data.conditions.slice(0, which).concat(data.conditions.concat(which+1)) - } - }, - ...condition.condition_data.conditions.slice(i+1) - ] - } - onChange({ - ...condition, - condition_data: update as any - }) - } - }} + {...genUpdateMethods(condition, onChange)(i)} /> } )} diff --git a/web/src/components/ConditionEditor/index.tsx b/web/src/components/ConditionEditor/index.tsx index 24ade54..0fe7f8d 100644 --- a/web/src/components/ConditionEditor/index.tsx +++ b/web/src/components/ConditionEditor/index.tsx @@ -33,8 +33,8 @@ export const ConditionEditor: FC<{ onAddChild={onAddChild} showDelete > - {({classes}) => } + } */} } \ No newline at end of file diff --git a/web/src/components/ConditionEditor/nestedUpdater.ts b/web/src/components/ConditionEditor/nestedUpdater.ts new file mode 100644 index 0000000..71fdb66 --- /dev/null +++ b/web/src/components/ConditionEditor/nestedUpdater.ts @@ -0,0 +1,49 @@ +import { AutomationCondition, LogicCondition } from "~/automations/types/conditions"; + + +export const genUpdateMethods = (condition: LogicCondition, onChange: (c: LogicCondition) => void) => (i: number) => ({ + onUpdate: (update: AutomationCondition) => onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + conditions: [ + ...condition.condition_data.conditions.slice(0, i), + update, + ...condition.condition_data.conditions.slice(i+1) + ] + } + }), + onDelete: (which: 'root' | number) => { + if (which === 'root') { + onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + conditions: condition.condition_data.conditions.slice(0, i).concat( + condition.condition_data.conditions.slice(i+1) + ) + } + }) + } else { + const data = (c as LogicCondition).condition_data; + const update = { + ...condition.condition_data, + conditions: [ + ...condition.condition_data.conditions.slice(0, i), + { + ...c, + condition_data: { + ...data, + conditions: data.conditions.slice(0, which).concat(data.conditions.concat(which+1)) + } + }, + ...condition.condition_data.conditions.slice(i+1) + ] + } + onChange({ + ...condition, + condition_data: update as any + }) + } + } +}) \ No newline at end of file diff --git a/web/src/components/ConditionEditor/style.ts b/web/src/components/ConditionEditor/style.ts index 9ff9c07..d5ae9a5 100644 --- a/web/src/components/ConditionEditor/style.ts +++ b/web/src/components/ConditionEditor/style.ts @@ -41,8 +41,8 @@ export const useStyles = createAppUseStyles<{ background: theme.condition.primaryColor, border: `1px solid ${theme.primaryAccent}`, borderRadius: 10, - borderTopRightRadius: hasChildren ? 10 : 0, - borderBottomRightRadius: hasChildren ? 10 : 0, + borderTopRightRadius: 0, + borderBottomRightRadius: 0, borderBottomLeftRadius: hasChildren ? 0 : 10, padding: 10, maxWidth: 80, @@ -60,18 +60,19 @@ export const useStyles = createAppUseStyles<{ // styling backgroundColor: theme.primary, fill: theme.primaryAccent, - border: hasChildren ? 'none' : `1px solid ${theme.primaryAccent}`, - borderRadius: 10, - borderTopLeftRadius: hasChildren ? 10 : 0, - borderBottomLeftRadius: hasChildren ? 10 : 0, + borderColor: theme.primaryAccent, + borderWidth: 1, + borderRadius: hasChildren ? 10 : 0, + borderTopLeftRadius: 0, + borderBottomLeftRadius: 0, cursor: 'pointer', // positioning display: 'flex', justifyContent: 'center', alignItems: 'center', - padding: 5, - position: hasChildren ? 'absolute' : 'block', + padding: '0px 10px', + // position: hasChildren ? 'absolute' : 'block', right: 1, top: 10, diff --git a/web/src/components/ConditionEditor/viewRender.tsx b/web/src/components/ConditionEditor/viewRender.tsx index e34043c..108b72d 100644 --- a/web/src/components/ConditionEditor/viewRender.tsx +++ b/web/src/components/ConditionEditor/viewRender.tsx @@ -2,10 +2,12 @@ import { FC } from "react"; import { AutomationCondition, LogicCondition, TemplateCondition } from "~/automations/types/conditions"; import { getDescriptionFromAutomationNode } from "~/automations/utils"; import { ConditionNode } from "./ConditionNode"; +import { genUpdateMethods } from "./nestedUpdater"; interface Viewer extends FC<{ - condition: C, + condition: C; + onChange: (condition: C) => void; }> {} export const getViewer = (condition: AutomationCondition): Viewer => { @@ -23,17 +25,20 @@ export const getViewer = (condition: AutomationCondition): Viewer => { export const GenericViewer: Viewer = ({ condition, }) => { - return
+ return <> {getDescriptionFromAutomationNode(condition)} -
+ } export const LogicViewer: Viewer = ({ condition, + onChange, }) => { return <> - {condition.condition_data.conditions.map( (condition, i) => { - return + {condition.condition_data.conditions.map( (c, i) => { + return } )} } \ No newline at end of file