diff --git a/web/src/automations/defaults.ts b/web/src/automations/defaults.ts new file mode 100644 index 0000000..9af48c3 --- /dev/null +++ b/web/src/automations/defaults.ts @@ -0,0 +1,41 @@ +import { AutomationCondition, LogicCondition, NumericCondition, StateCondition, TemplateCondition, TimeCondition, TriggerCondition, ZoneCondition } from "./types/conditions"; + + +export const getConditionDefaultValues = (condition: AutomationCondition['condition']): AutomationCondition['condition_data'] => { + if ( + (condition === 'or') || + (condition === 'and') || + (condition === 'not') + ) { + return { + conditions: [] + } as LogicCondition['condition_data'] + } else if (condition === 'template') { + return { + value_template: "" + } as TemplateCondition['condition_data'] + } else if (condition === 'numeric_state') { + return { + entity_id: [] + } as NumericCondition['condition_data'] + } else if (condition === 'state') { + return { + entity_id: [], + state: [], + } as StateCondition['condition_data'] + } else if (condition === 'time') { + return { + } as TimeCondition['condition_data'] + } else if (condition === 'trigger') { + return { + id: [] + } as TriggerCondition['condition_data'] + } else if (condition === 'zone') { + return { + zone: "", + entity_id: [], + state: [], + } as ZoneCondition['condition_data'] + } + throw new Error("NOT IMPLEMENTED!") +} \ No newline at end of file diff --git a/web/src/automations/types/conditions.ts b/web/src/automations/types/conditions.ts index 93e8fab..26b7e5c 100644 --- a/web/src/automations/types/conditions.ts +++ b/web/src/automations/types/conditions.ts @@ -48,13 +48,3 @@ export type AutomationCondition = | TriggerCondition | ZoneCondition ; -export const conditionTypes = [ - 'Logic', - 'Numeric State', - 'State', - 'Template', - 'Time', - 'Trigger', - 'Zone', -] as const; -export type ConditionType = typeof conditionTypes[number]; \ No newline at end of file diff --git a/web/src/components/ConditionEditor/ConditionNode.tsx b/web/src/components/ConditionEditor/ConditionNode.tsx index 44dec07..9679d06 100644 --- a/web/src/components/ConditionEditor/ConditionNode.tsx +++ b/web/src/components/ConditionEditor/ConditionNode.tsx @@ -1,7 +1,9 @@ import { Classes } from "jss"; import { FC, useState } from "react"; +import { getConditionDefaultValues } from "~/automations/defaults"; import { AutomationCondition } from "~/automations/types/conditions"; import { CheckMarkIcon, PencilIcon } from "~/icons/icons"; +import InputList from "../Inputs/InputList"; import { getEditor } from "./editorRender"; import { useStyles } from "./style"; import { getViewer } from "./viewRender"; @@ -13,7 +15,6 @@ export const ConditionNode: FC<{ displayMode: boolean; showDelete?: boolean; onDelete?: (which: 'root' | number) => void; - onAddChild?: () => void; onUpdate?: (data: AutomationCondition) => void; children?: (props: { classes: Classes @@ -24,14 +25,33 @@ export const ConditionNode: FC<{ onDelete=() => {}, onUpdate=() => {}, displayMode=() => {}, - onAddChild=() => {}, children=() => {}, }) => { // state const [internalDisplayMode, setInternalDisplayMode] = useState(true); // alias const effectiveDM = displayMode && internalDisplayMode; - console.log({effectiveDM}) + const onAddChild = () => { + if( + (condition.condition === 'and') || + (condition.condition === 'or') || + (condition.condition === 'not') + ) { + onUpdate({ + ...condition, + condition_data: { + ...condition.condition_data, + conditions: condition.condition_data.conditions.concat({ + $smType: 'condition', + condition: 'or', + condition_data: { + conditions: [] + } + }) + } + }) + } + } // children let childrenConditions: JSX.Element; if (effectiveDM) { @@ -54,6 +74,7 @@ export const ConditionNode: FC<{ } else if (condition.condition ==='or') { hasChildren = true } + console.log({condition: condition.condition, hasChildren}) const {classes} = useStyles({ hasChildren }); // components @@ -63,13 +84,36 @@ export const ConditionNode: FC<{ return
{(showDelete || !effectiveDM) && onDelete('root')}/>}
- {condition.condition.replace('_', ' ')} + {/* {condition.condition.replace('_', ' ')} */} + onUpdate({ + condition: n, + condition_data: { + ...getConditionDefaultValues(n), + ...condition.condition_data, + } + } as any)} + />
{childrenConditions} + {hasChildren &&
+ +
}
{children({classes})}
diff --git a/web/src/components/ConditionEditor/index.stories.tsx b/web/src/components/ConditionEditor/index.stories.tsx index 637f64f..2ee803a 100644 --- a/web/src/components/ConditionEditor/index.stories.tsx +++ b/web/src/components/ConditionEditor/index.stories.tsx @@ -15,9 +15,12 @@ const Template: ComponentStory = ({condition, ...args}) const {classes} = usePageTheme({}); const [data, setData] = useState(condition); return
- { - setData(data) - }} /> + { + setData(data) + }} + />
} diff --git a/web/src/components/ConditionEditor/index.tsx b/web/src/components/ConditionEditor/index.tsx index 421ca49..b63e651 100644 --- a/web/src/components/ConditionEditor/index.tsx +++ b/web/src/components/ConditionEditor/index.tsx @@ -8,13 +8,11 @@ import { ConditionNode } from "./ConditionNode"; export const ConditionEditor: FC<{ condition: AutomationCondition; onDelete: (which: 'root' | number) => void; - onAddChild: () => void; onUpdate: (data: AutomationCondition) => void; }> = ({ condition, onDelete, onUpdate, - onAddChild, }) => { // state return { onUpdate(update) }} - onAddChild={onAddChild} displayMode showDelete /> diff --git a/web/src/components/ConditionEditor/style.ts b/web/src/components/ConditionEditor/style.ts index d5ae9a5..08f1d0b 100644 --- a/web/src/components/ConditionEditor/style.ts +++ b/web/src/components/ConditionEditor/style.ts @@ -45,10 +45,12 @@ export const useStyles = createAppUseStyles<{ borderBottomRightRadius: 0, borderBottomLeftRadius: hasChildren ? 0 : 10, padding: 10, - maxWidth: 80, + maxWidth: 120, minWidth: 30, textOverflow: 'ellipsis', textAlign: 'center', + cursor: 'pointer', + color: theme.primaryAccent, }), deleteBtnRoot: { ...deleteBtn, @@ -99,6 +101,28 @@ export const useStyles = createAppUseStyles<{ border: `1px solid ${theme.primaryAccent}`, borderBottomRightRadius: hasChildren ? 10 : 0, borderBottomLeftRadius: hasChildren ? 10 : 0, - }) + }), + addBtnContainer: { + display: 'flex', + justifyContent: 'center', + }, + addBtn: { + backgroundColor: theme.secondary, + borderWidth: 1, + borderStyle: 'solid', + borderColor: theme.secondaryAccent, + color: theme.secondaryAccent, + borderRadius: 5, + marginTop: 5, + marginBottom: 5, + marginRight: 5, + paddingLeft: 20, + paddingRight: 20, + cursor: 'pointer', + "&:hover": { + backgroundColor: Color(theme.primary).brighten().hex(), + fill: Color(theme.primaryAccent).brighten().hex(), + }, + } } }) \ No newline at end of file diff --git a/web/src/styles/theme.ts b/web/src/styles/theme.ts index 402f2b5..acbdf70 100644 --- a/web/src/styles/theme.ts +++ b/web/src/styles/theme.ts @@ -17,7 +17,7 @@ export const { primaryColor: '#4f4f77', }, red: '#ed0c0c', - redAccent: '#9f3a3a' + redAccent: '#9f3a3a', })); export type GetThemeType> = C extends Context ? T : unknown;