From 5d9c04072b077dcad2191e3e0127b2607eee0c63 Mon Sep 17 00:00:00 2001 From: Ari Date: Wed, 27 Jul 2022 19:34:45 -0400 Subject: [PATCH] moved cleanupundefined moved tags --- .../AutomationListV2/MetadataBox/Tags.tsx | 36 +++++++++++++++++++ .../AutomationListV2/MetadataBox/index.tsx | 26 ++------------ webapp/src/components/DeviceEditor/index.tsx | 2 +- .../NodeEditor/OptionManager/OptionManager.ts | 11 +----- webapp/src/components/ServiceEditor/index.tsx | 2 +- webapp/src/utils/helpers.ts | 9 +++++ 6 files changed, 51 insertions(+), 35 deletions(-) create mode 100644 webapp/src/components/AutomationListV2/MetadataBox/Tags.tsx create mode 100644 webapp/src/utils/helpers.ts diff --git a/webapp/src/components/AutomationListV2/MetadataBox/Tags.tsx b/webapp/src/components/AutomationListV2/MetadataBox/Tags.tsx new file mode 100644 index 0000000..9a9acc1 --- /dev/null +++ b/webapp/src/components/AutomationListV2/MetadataBox/Tags.tsx @@ -0,0 +1,36 @@ +import { FC } from "react"; + +import DeleteIcon from "@mui/icons-material/ClearTwoTone"; + +import { cleanUpUndefined } from "utils/helpers"; + +export type TagsProps = { + tags: Record; + onUpdate: (t: Record) => void; +}; + +export const Tags: FC = ({ tags, onUpdate }) => { + return ( +
+ {Object.entries(tags).map(([tagName, tagValue]) => ( + + + onUpdate( + cleanUpUndefined({ + ...tags, + [tagName]: undefined, + }) + ) + } + className="delete-tag" + /> + {tagName}: {tagValue} + + ))} + {}}> + + + +
+ ); +}; diff --git a/webapp/src/components/AutomationListV2/MetadataBox/index.tsx b/webapp/src/components/AutomationListV2/MetadataBox/index.tsx index eb88168..485352d 100644 --- a/webapp/src/components/AutomationListV2/MetadataBox/index.tsx +++ b/webapp/src/components/AutomationListV2/MetadataBox/index.tsx @@ -4,14 +4,14 @@ import { FC } from "react"; import Switch from "@mui/material/Switch"; import Alert from "@mui/material/Alert"; -import DeleteIcon from "@mui/icons-material/ClearTwoTone"; import DeleteForeverIcon from "@mui/icons-material/DeleteForever"; import SelectIcon from "@mui/icons-material/HighlightAltSharp"; import { InputTextView } from "components/Inputs/InputTextView"; import { ButtonIcon } from "components/Icons/ButtonIcons"; -import { cleanUpUndefined } from "components/NodeEditor/OptionManager/OptionManager"; +import { cleanUpUndefined } from "utils/helpers"; import { AutomationListAuto } from "../types"; +import { Tags } from "./Tags"; export type MetadataBoxProps = AutomationListAuto & { onDelete: () => void; @@ -57,27 +57,7 @@ export const MetadataBox: FC = (props) => { value={props.description} onChange={props.onDescriptionUpdate} /> -
- {Object.entries(props.tags).map(([tagName, tagValue]) => ( - - - props.onTagUpdate( - cleanUpUndefined({ - ...props.tags, - [tagName]: undefined, - }) - ) - } - className="delete-tag" - /> - {tagName}: {tagValue} - - ))} - {}}> - + - -
+ {props.issue ? ( {props.issue} diff --git a/webapp/src/components/DeviceEditor/index.tsx b/webapp/src/components/DeviceEditor/index.tsx index d2651ec..594ab63 100644 --- a/webapp/src/components/DeviceEditor/index.tsx +++ b/webapp/src/components/DeviceEditor/index.tsx @@ -5,7 +5,7 @@ import { import { InputDevice } from "components/Inputs/InputDevice"; import { InputDeviceCapabilties } from "components/Inputs/InputDeviceCapabilties"; import { InputEntity } from "components/Inputs/InputEntities"; -import { cleanUpUndefined } from "components/NodeEditor/OptionManager/OptionManager"; +import { cleanUpUndefined } from "utils/helpers"; import { DeviceBaseType, DeviceTypeCapability } from "haService/types"; import { AutomationDeviceState } from "types/automations/common"; diff --git a/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts b/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts index 1b0225b..17a9fe9 100644 --- a/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts +++ b/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts @@ -3,6 +3,7 @@ import { HAEntitiesState } from "haService/HAEntities"; import { FC } from "react"; import { AutomationAction } from "types/automations/actions"; import { AutomationCondition } from "types/automations/conditions"; +import { cleanUpUndefined } from "utils/helpers"; export type OptionManager = { defaultState(): T; isReady(s: T): boolean; @@ -24,16 +25,6 @@ export type OptionManager = { } ); -export const cleanUpUndefined = (data: Record): any => { - const clean: any = {}; - Object.keys(data).forEach((k) => { - if (data[k] !== undefined && data[k] !== null) { - clean[k] = data[k]; - } - }); - return clean; -}; - export const updateConditionData = (setState: (s: T) => void) => (state: T, data: Partial) => diff --git a/webapp/src/components/ServiceEditor/index.tsx b/webapp/src/components/ServiceEditor/index.tsx index 77a8edb..5d4bfdf 100644 --- a/webapp/src/components/ServiceEditor/index.tsx +++ b/webapp/src/components/ServiceEditor/index.tsx @@ -1,6 +1,6 @@ import "./index.css"; -import { cleanUpUndefined } from "components/NodeEditor/OptionManager/OptionManager"; +import { cleanUpUndefined } from "utils/helpers"; import { TypedHassService } from "haService/fieldTypes"; import { FC, useState } from "react"; import { diff --git a/webapp/src/utils/helpers.ts b/webapp/src/utils/helpers.ts new file mode 100644 index 0000000..872ed13 --- /dev/null +++ b/webapp/src/utils/helpers.ts @@ -0,0 +1,9 @@ +export const cleanUpUndefined = (data: Record): any => { + const clean: any = {}; + Object.keys(data).forEach((k) => { + if (data[k] !== undefined && data[k] !== null) { + clean[k] = data[k]; + } + }); + return clean; +};