diff --git a/www/src/components/fields/Status/Settings.tsx b/www/src/components/fields/Status/Settings.tsx index 945f0d02..0e797e0d 100644 --- a/www/src/components/fields/Status/Settings.tsx +++ b/www/src/components/fields/Status/Settings.tsx @@ -1,22 +1,22 @@ -import React, { useState,useEffect } from "react"; +import React, { useState, useEffect } from "react"; import { ISettingsProps } from "../types"; import Subheading from "components/Table/ColumnMenu/Subheading"; -import Button from '@material-ui/core/Button'; -import IconButton from '@material-ui/core/IconButton'; -import Grid from '@material-ui/core/Grid'; -import Divider from '@material-ui/core/Divider'; +import Button from "@material-ui/core/Button"; +import IconButton from "@material-ui/core/IconButton"; +import Grid from "@material-ui/core/Grid"; +import Divider from "@material-ui/core/Divider"; import _sortBy from "lodash/sortBy"; -import EditIcon from "@material-ui/icons/Edit" -import Modal from "components/Modal" -import DeleteIcon from "@material-ui/icons/Delete" -import Typography from "@material-ui/core/Typography" -import TextField from "@material-ui/core/TextField" -import MultiSelect from "@antlerengineering/multiselect" +import EditIcon from "@material-ui/icons/Edit"; +import Modal from "components/Modal"; +import DeleteIcon from "@material-ui/icons/Delete"; +import Typography from "@material-ui/core/Typography"; +import TextField from "@material-ui/core/TextField"; +import MultiSelect from "@antlerengineering/multiselect"; const EMPTY_STATE: { isOpen: boolean; - index: number| null; + index: number | null; condition: { type: string; value: any; @@ -30,148 +30,198 @@ const EMPTY_STATE: { type: "null", value: null, label: "", - operator: "==" - } -} + operator: "==", + }, +}; const ConditionModal = ({ modal, setModal, conditions, setConditions }) => { const handleClose = () => { - setModal(EMPTY_STATE) - } + setModal(EMPTY_STATE); + }; const handleSave = () => { - let _conditions = [...conditions] - _conditions[modal.index] = modal.condition - setConditions(_conditions) - setModal(EMPTY_STATE) - - } + let _conditions = [...conditions]; + _conditions[modal.index] = modal.condition; + setConditions(_conditions); + setModal(EMPTY_STATE); + }; const handleAdd = () => { - setConditions(conditions ? [...conditions, modal.condition] : [modal.condition]) - setModal(EMPTY_STATE) - } + setConditions( + conditions ? [...conditions, modal.condition] : [modal.condition] + ); + setModal(EMPTY_STATE); + }; const handleRemove = () => { - let _conditions = [...conditions] - delete _conditions[modal.index] - setConditions(_conditions) - setModal(EMPTY_STATE) - } + let _conditions = [...conditions]; + delete _conditions[modal.index]; + setConditions(_conditions); + setModal(EMPTY_STATE); + }; const handleUpdate = (key: string) => (value) => { - setModal({ ...modal, condition: { ...modal.condition, [key]: value } }) - } - useEffect(()=>{ - handleUpdate("operator")('==') - },[modal.condition.type]) + setModal({ ...modal, condition: { ...modal.condition, [key]: value } }); + }; + useEffect(() => { + handleUpdate("operator")("=="); + }, [modal.condition.type]); return ( - {setModal(EMPTY_STATE)}, - }:{ - startIcon:, - children: "Remove Condition", - onClick: handleRemove, - }, + primary: + modal.index === null + ? { + children: "Add Condition", + onClick: handleAdd, + disabled: false, + } + : { + children: "Save Changes", + onClick: handleSave, + disabled: false, + }, + secondary: + modal.index === null + ? { + children: "Cancel", + onClick: () => { + setModal(EMPTY_STATE); + }, + } + : { + startIcon: , + children: "Remove Condition", + onClick: handleRemove, + }, }} - children={<> - - DATA TYPE (input) - - - - Condition - - { modal.condition.type === "boolean" && handleUpdate('value')(v==="true")} - value={modal.condition.value?"true":"false"} - multiple={false} - label={'Select Condition Value'} />} - - { modal.condition.type === "number" && -
=" }, - { label: "More Than", value: ">" }, - ]} - onChange={handleUpdate('operator')} - value={modal.condition.operator} - multiple={false} - label={'Select Operator'} />
- handleUpdate('value')(e.target.value)} /> -
} - { modal.condition.type === "string" && - handleUpdate('value')(e.target.value)} />} + children={ + <> + DATA TYPE (input) + + Condition + {modal.condition.type === "boolean" && ( + handleUpdate("value")(v === "true")} + value={modal.condition.value ? "true" : "false"} + multiple={false} + label={"Select Condition Value"} + /> + )} - - assigned label (output) - - handleUpdate('label')(e.target.value)} /> - } + {modal.condition.type === "number" && ( + +
+ =" }, + { label: "More Than", value: ">" }, + ]} + onChange={handleUpdate("operator")} + value={modal.condition.operator} + multiple={false} + label={"Select Operator"} + /> +
+ handleUpdate("value")(e.target.value)} + /> +
+ )} + {modal.condition.type === "string" && ( + handleUpdate("value")(e.target.value)} + /> + )} + + assigned label (output) + handleUpdate("label")(e.target.value)} + /> + + } /> - ) -} - + ); +}; export default function Settings({ handleChange, config }: ISettingsProps) { - - const [modal, setModal] = useState(EMPTY_STATE) - const { conditions } = config + const [modal, setModal] = useState(EMPTY_STATE); + const { conditions } = config; return ( <> Conditions - {conditions ? conditions.map((condition, index) => { - return (<> - {condition.label}{ - ['undefined','null'].includes(condition.type)?condition.type - :`${condition.type}:${condition.type ==="number"?condition.operator:""}${condition.type==="boolean"?JSON.stringify(condition.value):condition.value}`} - { - setModal({isOpen: true,condition,index}) - }} - > - - ) - }) : <> - no conditions set yet -
- } + {conditions ? ( + conditions.map((condition, index) => { + return ( + <> + + {condition.label} + + {["undefined", "null"].includes(condition.type) + ? condition.type + : `${condition.type}:${ + condition.type === "number" ? condition.operator : "" + }${ + condition.type === "boolean" + ? JSON.stringify(condition.value) + : condition.value + }`} + { + setModal({ isOpen: true, condition, index }); + }} + > + + + + + + + ); + }) + ) : ( + <> + no conditions set yet +
+ + )} - - + ); } - -