From 6b465219a9eddec6cff67544a22b3dc39b33d844 Mon Sep 17 00:00:00 2001 From: Ari Date: Fri, 15 Jul 2022 13:25:43 -0400 Subject: [PATCH] upping limits of display --- webapp/src/apiService/util.ts | 36 ++-- .../AutomationList/AutomationList.stories.tsx | 191 ++++++++++-------- .../AutomationList/AutomationListBox.css | 18 +- .../AutomationListBox.stories.tsx | 78 +++---- .../AutomationList/AutomationListBox.tsx | 58 +++++- .../src/components/AutomationList/index.tsx | 18 +- 6 files changed, 243 insertions(+), 156 deletions(-) diff --git a/webapp/src/apiService/util.ts b/webapp/src/apiService/util.ts index a406c4a..9d39a08 100644 --- a/webapp/src/apiService/util.ts +++ b/webapp/src/apiService/util.ts @@ -1,42 +1,40 @@ import { useState } from "react"; import { ApiState } from "./types"; -import { AutomationAPI } from './automations'; +import { AutomationAPI } from "./automations"; - - -export const useDefaultApiState = () => useState({ - automations: { ready: false }, -}); +export const useDefaultApiState = () => + useState({ + automations: { ready: false }, + }); export const makeReloadAutomations = ( automationAPI: AutomationAPI, state: ApiState, - setState: (s: ApiState) => void, + setState: (s: ApiState) => void ) => { const reload = async () => { - let params = { offset: 0, limit: 100 }; + let params = { offset: 0, limit: 10000 }; if (state.automations.ready && state.automations.ok) { - params = state.automations.data.params + params = state.automations.data.params; } const resp = await automationAPI.list(params); setState({ - ...state, automations: { + ...state, + automations: { ready: true, ...resp, - } + }, }); - } - const wrapCall = < - Call extends (a: any) => Promise - >(fcn: Call) => { + }; + const wrapCall = Promise>(fcn: Call) => { return async (args: Parameters[0]): Promise> => { const resp = await fcn(args); await reload(); return resp; - } - } + }; + }; return { reload, wrapCall, - } -} + }; +}; diff --git a/webapp/src/components/AutomationList/AutomationList.stories.tsx b/webapp/src/components/AutomationList/AutomationList.stories.tsx index 0249d90..3752a33 100644 --- a/webapp/src/components/AutomationList/AutomationList.stories.tsx +++ b/webapp/src/components/AutomationList/AutomationList.stories.tsx @@ -5,6 +5,7 @@ import { AutomationList } from "."; import { bigMockAutoList } from "utils/mocks"; import { Page } from "components/Page"; import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants"; +import { AutomationData } from "types/automations"; export default { title: "App/AutomationList", @@ -16,12 +17,15 @@ export default { } as ComponentMeta; const Template: ComponentStory = (args) => { - const [autos, setAutos] = useState(args.automations); + const [autos, setAutos] = useState(args.automations.data); return ( { args.onAdd(a); setAutos([...autos, a]); @@ -39,107 +43,115 @@ const Template: ComponentStory = (args) => { ); }; -export const EmptyStart = Template.bind({}); -EmptyStart.args = { - ...EmptyStart.args, - automations: [], +const make = (autos: AutomationData[]) => { + const NewTemp = Template.bind({}); + NewTemp.args = { + ...NewTemp.args, + automations: { + data: autos, + params: { + offset: 0, + limit: 100, + }, + totalItems: autos.length, + }, + }; + + return NewTemp; }; -export const FewAutos = Template.bind({}); -FewAutos.args = { - ...FewAutos.args, - automations: bigMockAutoList, -}; +export const EmptyStart = make([]); -export const BadAutmations = Template.bind({}); -BadAutmations.args = { - ...BadAutmations.args, - automations: [ - { - metadata: { - alias: "Set Thermostat to 76 at 8am", - description: "", - mode: "single", - } as any, - trigger: [ - { - at: "08:00:00", - platform: "time", +export const FewAutos = make(bigMockAutoList); + +export const BadAutmations = make([ + { + metadata: { + alias: "Set Thermostat to 76 at 8am", + description: "", + mode: "single", + } as any, + trigger: [ + { + at: "08:00:00", + platform: "time", + }, + ], + condition: [ + { + condition: "state", + entity_id: "climate.thermostat", + state: "cool", + }, + ], + sequence: [ + { + data: { + temperature: 76, }, - ], - condition: [ - { - condition: "state", + service: "climate.set_temperature", + target: { entity_id: "climate.thermostat", - state: "cool", }, - ], - sequence: [ - { - data: { - temperature: 76, - }, - service: "climate.set_temperature", - target: { - entity_id: "climate.thermostat", - }, - }, - ], - tags: {}, - }, - { - condition: [], - tags: {}, - metadata: { - id: "Bad Choose Sequence", - alias: "Bad Choose", - description: "Example Metadata", - trigger_variables: { - wowo: "!", - }, - mode: "single", }, - trigger: [], - sequence: [ - { - choose: {}, - }, - ] as any, - }, - { - condition: [], - tags: {}, - metadata: { - id: "random", - alias: "Bad Trigger", - description: "Example Metadata", - trigger_variables: { - wowo: "!", - }, - mode: "single", + ], + tags: {}, + }, + { + condition: [], + tags: {}, + metadata: { + id: "Bad Choose Sequence", + alias: "Bad Choose", + description: "Example Metadata", + trigger_variables: { + wowo: "!", }, - trigger: ["haha I am a string"] as any, - sequence: [], + mode: "single", }, - { - condition: [], - tags: {}, - metadata: {} as any, - trigger: [], - sequence: [], + trigger: [], + sequence: [ + { + choose: {}, + }, + ] as any, + }, + { + condition: [], + tags: {}, + metadata: { + id: "random", + alias: "Bad Trigger", + description: "Example Metadata", + trigger_variables: { + wowo: "!", + }, + mode: "single", }, - ], -}; + trigger: ["haha I am a string"] as any, + sequence: [], + }, + { + condition: [], + tags: {}, + metadata: {} as any, + trigger: [], + sequence: [], + }, +]); export const FewAutosWithSlowLoader: ComponentStory = ( args ) => { - const [autos, setAutos] = useState(args.automations); + const [autos, setAutos] = useState(args.automations.data); return ( { window.setTimeout(() => { args.onAdd(a); @@ -164,5 +176,12 @@ export const FewAutosWithSlowLoader: ComponentStory = ( }; FewAutosWithSlowLoader.args = { ...FewAutosWithSlowLoader.args, - automations: bigMockAutoList, + automations: { + data: bigMockAutoList, + params: { + limit: 100, + offset: 0, + }, + totalItems: 100, + }, }; diff --git a/webapp/src/components/AutomationList/AutomationListBox.css b/webapp/src/components/AutomationList/AutomationListBox.css index 4c07559..9859faf 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.css +++ b/webapp/src/components/AutomationList/AutomationListBox.css @@ -65,8 +65,20 @@ align-items: center; } -.automation-list-box--bottom>button { - background-color: #070; - color: var(--mui-grey-400); +.automation-list-box--bottom>.automation-list-box--bottom--add { + background-color: var(--mui-primary-main); + color: var(--mui-primary-contrastText); padding: 0.5em 2em; +} + +.automation-list-box--bottom>.automation-list-box--bottom--add:hover { + color: var(--mui-primary-light); +} + +.automation-list-box--bottom--load svg { + color: var(--mui-primary-main); +} + +.automation-list-box--bottom--load.less svg { + transform: rotate(180deg); } \ No newline at end of file diff --git a/webapp/src/components/AutomationList/AutomationListBox.stories.tsx b/webapp/src/components/AutomationList/AutomationListBox.stories.tsx index 326552f..f326e0d 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.stories.tsx +++ b/webapp/src/components/AutomationList/AutomationListBox.stories.tsx @@ -5,6 +5,7 @@ import { createMockAuto } from "utils/mocks"; import { Page } from "components/Page"; import { CookiesProvider } from "react-cookie"; import { makeTagDB } from "./TagDB"; +import { AutomationData } from "types/automations"; export default { title: "App/AutomationList/Box", @@ -20,7 +21,7 @@ const Template: ComponentStory = (args) => { { @@ -33,41 +34,46 @@ const Template: ComponentStory = (args) => { ); }; -export const EmptyStart = Template.bind({}); -EmptyStart.args = { - ...EmptyStart.args, - automations: [], +const make = (autos: AutomationData[]) => { + const NewTemp = Template.bind({}); + NewTemp.args = { + ...NewTemp.args, + automations: { + data: autos, + params: { + offset: 0, + limit: 100, + }, + totalItems: autos.length, + }, + }; + + return NewTemp; }; -export const FewAutos = Template.bind({}); -FewAutos.args = { - ...FewAutos.args, - automations: [ - createMockAuto({ - Room: "Bathroom", - Type: "Lights", - NewDevice: "Yes", - Remote: "No", - Floor: "1", - }), - createMockAuto({ Room: "Bathroom", Type: "Climate" }), - createMockAuto({ Room: "Living Room", Type: "Climate" }), - createMockAuto({ Type: "Climate" }), - createMockAuto({ Type: "Lights", Routine: "Myself" }), - createMockAuto({ Routine: "DNS", Type: "SSL" }), - createMockAuto({ Room: "Bedroom", Type: "Lights", Routine: "Baby" }), - createMockAuto({ Room: "Office", Type: "Lights" }), - createMockAuto({ Room: "Office" }), - createMockAuto(), - ], -}; +export const EmptyStart = make([]); -export const OneAuto = Template.bind({}); -OneAuto.args = { - ...OneAuto.args, - automations: [ - createMockAuto({ - Type: "Climate", - }), - ], -}; +export const FewAutos = make([ + createMockAuto({ + Room: "Bathroom", + Type: "Lights", + NewDevice: "Yes", + Remote: "No", + Floor: "1", + }), + createMockAuto({ Room: "Bathroom", Type: "Climate" }), + createMockAuto({ Room: "Living Room", Type: "Climate" }), + createMockAuto({ Type: "Climate" }), + createMockAuto({ Type: "Lights", Routine: "Myself" }), + createMockAuto({ Routine: "DNS", Type: "SSL" }), + createMockAuto({ Room: "Bedroom", Type: "Lights", Routine: "Baby" }), + createMockAuto({ Room: "Office", Type: "Lights" }), + createMockAuto({ Room: "Office" }), + createMockAuto(), +]); + +export const OneAuto = make([ + createMockAuto({ + Type: "Climate", + }), +]); diff --git a/webapp/src/components/AutomationList/AutomationListBox.tsx b/webapp/src/components/AutomationList/AutomationListBox.tsx index 999722b..fc005a1 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.tsx +++ b/webapp/src/components/AutomationList/AutomationListBox.tsx @@ -8,14 +8,20 @@ import { makeGrouping } from "./automationGrouper"; import { AutomationListBoxGroup } from "./AutomationListBoxGroup"; import { useCookies } from "react-cookie"; import { TagDB } from "components/AutomationList/TagDB"; +// import { ButtonIcon } from "components/Icons/ButtonIcons"; +// import ReadMoreOutlinedIcon from "@mui/icons-material/ReadMoreOutlined"; +import { ListData, ListParams } from "apiService/types"; +import Chip from "@mui/material/Chip"; +import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined"; export type AutomationListBoxProps = { - automations: AutomationData[]; + automations: ListData; selected: number; onSelectAutomation: (i: number) => void; onRemove: (i: number) => void; onAdd: () => void; onUpdate: (a: AutomationData, i: number) => void; + onLoadMore: (p: ListParams) => void; tagsDB: TagDB; }; @@ -54,11 +60,16 @@ const useAutomationListBoxState = () => { }; export const AutomationListBox: FC = ({ - automations, + automations: { + data: automations, + params: autoParams, + totalItems: totalAutomations, + }, selected, onSelectAutomation, onAdd, onRemove, + onLoadMore, tagsDB, onUpdate, }) => { @@ -101,11 +112,44 @@ export const AutomationListBox: FC = ({ />
- - - Showing Automations: {filteredAutomations.length} out of{" "} - {automations.length} - + + {automations.length < totalAutomations ? ( + <> + } + onClick={() => + window.open( + "https://github.com/asosnovsky/Shortumation/issues/78", + "_blank" + ) + } + /> + support for more is not yet implemented + + ) : ( + "" + )} + {/* {autoParams.offset > 0 && ( + } + onClick={() => onLoadMore()} + /> + )} */} + {/* {filteredAutomations.length} out of {totalAutomations} */} + {/* {autoParams.offset + automations.length < totalAutomations && ( + } + /> + )} */}
); diff --git a/webapp/src/components/AutomationList/index.tsx b/webapp/src/components/AutomationList/index.tsx index 3aad184..af61960 100644 --- a/webapp/src/components/AutomationList/index.tsx +++ b/webapp/src/components/AutomationList/index.tsx @@ -16,12 +16,14 @@ import { useHA } from "haService"; import { ArrowBack } from "@mui/icons-material"; import { useSnackbar } from "notistack"; import { DAGDims } from "components/DAGGraph/elements/types"; +import { ListData, ListParams } from "apiService/types"; interface AutomationListParams { - automations: AutomationData[]; + automations: ListData; onUpdate: (i: number, auto: AutomationData) => void; onAdd: (auto: AutomationData) => void; onRemove: (i: number) => void; + onLoadMore: (p: ListParams) => void; dims: DAGDims; children?: ReactNode; } @@ -54,6 +56,7 @@ export const useAutomationListState = () => { export const AutomationList: FC = ({ automations, + onLoadMore, onUpdate, onAdd, onRemove, @@ -67,8 +70,11 @@ export const AutomationList: FC = ({ const { isMobile } = useWindowSize(); const firstRender = useRef(true); // alias - const currentAuto = automations.length > 0 ? automations[current] : null; - const tagDB = makeTagDB(automations); + const currentAuto = + automations.data.length > 0 + ? automations.data[current - automations.params.offset] + : null; + const tagDB = makeTagDB(automations.data); // effect useEffect(() => { if (firstRender.current) { @@ -92,7 +98,7 @@ export const AutomationList: FC = ({ onUpdate={(a, i) => onUpdate(i, a)} onAdd={() => { onAdd(defaultAutomation(String(Date.now()))); - setCurrent(automations.length); + setCurrent(automations.data.length); if (isMobile) { setHideList(true); } @@ -103,6 +109,7 @@ export const AutomationList: FC = ({ setHideList(true); } }} + onLoadMore={onLoadMore} selected={current} onRemove={onRemove} tagsDB={tagDB} @@ -171,7 +178,8 @@ export const ConnectedAutomationList: FC = ({ return ( {}} onAdd={(auto) => updateAutomation({ auto, index: automations.data.totalItems + 1 }) }