From 60cabb9e57cc4d320d0daf07f58fc9b267aba3a4 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Mon, 13 Aug 2018 15:11:36 +0300 Subject: [PATCH] Use mobx actions on client side --- client/components/DeviceView.tsx | 12 ++-- client/components/ProgramSequenceView.tsx | 9 ++- client/components/ProgramTable.tsx | 12 ++-- client/components/ScheduleView/index.tsx | 13 ++-- client/pages/LoginPage.tsx | 17 +++-- client/pages/ProgramPage.tsx | 35 +++++++--- client/sprinklersRpc/WebSocketRpcClient.ts | 78 ++++++++++++++-------- client/state/AppState.ts | 6 +- client/state/HttpApi.ts | 17 +++-- client/state/TokenStore.ts | 6 +- client/state/UiStore.ts | 15 ++++- client/state/UserStore.ts | 15 ++++- common/logger.ts | 3 +- 13 files changed, 161 insertions(+), 77 deletions(-) diff --git a/client/components/DeviceView.tsx b/client/components/DeviceView.tsx index 082d3a4..376fdd8 100644 --- a/client/components/DeviceView.tsx +++ b/client/components/DeviceView.tsx @@ -8,6 +8,7 @@ import { DeviceImage } from "@client/components"; import * as p from "@client/pages"; import * as route from "@client/routePaths"; import { AppState, injectState } from "@client/state"; +import { ISprinklersDevice } from "@common/httpApi"; import { ConnectionState as ConState, SprinklersDevice } from "@common/sprinklersRpc"; import { Route, RouteComponentProps, withRouter } from "react-router"; import { ProgramTable, RunSectionForm, SectionRunnerView, SectionTable } from "."; @@ -52,7 +53,7 @@ interface DeviceViewProps { } class DeviceView extends React.Component> { - renderBody(device: SprinklersDevice) { + renderBody(iDevice: ISprinklersDevice, device: SprinklersDevice) { const { inList, appState: { uiStore, routerStore } } = this.props; const { connectionState, sectionRunner, sections } = device; if (!connectionState.isAvailable || inList) { @@ -71,7 +72,7 @@ class DeviceView extends React.Component - + ); @@ -79,10 +80,7 @@ class DeviceView extends React.Component dev.id === deviceId); + const iDevice = userStore.findDevice(deviceId); let itemContent: React.ReactNode; if (!iDevice || !iDevice.deviceId) { // TODO: better and link back to devices list @@ -107,7 +105,7 @@ class DeviceView extends React.Component Raspberry Pi Grinklers Device - {this.renderBody(device)} + {this.renderBody(iDevice, device)} ); diff --git a/client/components/ProgramSequenceView.tsx b/client/components/ProgramSequenceView.tsx index 7b5ac7d..396df59 100644 --- a/client/components/ProgramSequenceView.tsx +++ b/client/components/ProgramSequenceView.tsx @@ -9,6 +9,7 @@ import { Duration } from "@common/Duration"; import { ProgramItem, Section } from "@common/sprinklersRpc"; import "@client/styles/ProgramSequenceView"; +import { action } from "mobx"; type ItemChangeHandler = (index: number, newItem: ProgramItem) => void; type ItemRemoveHandler = (index: number) => void; @@ -147,15 +148,18 @@ class ProgramSequenceView extends React.Component<{ ); } + @action.bound private changeItem: ItemChangeHandler = (index, newItem) => { this.props.sequence[index] = newItem; } + @action.bound private removeItem: ItemRemoveHandler = (index) => { this.props.sequence.splice(index, 1); } - private addItem = () => { + @action.bound + private addItem() { let sectionId = 0; for (const section of this.props.sections) { const sectionNotIncluded = this.props.sequence @@ -173,7 +177,8 @@ class ProgramSequenceView extends React.Component<{ this.props.sequence.push(item); } - private onSortEnd = ({oldIndex, newIndex}: SortEnd) => { + @action.bound + private onSortEnd({oldIndex, newIndex}: SortEnd) { const { sequence: array } = this.props; if (newIndex >= array.length) { return; diff --git a/client/components/ProgramTable.tsx b/client/components/ProgramTable.tsx index c67eaeb..f3de61d 100644 --- a/client/components/ProgramTable.tsx +++ b/client/components/ProgramTable.tsx @@ -6,22 +6,25 @@ import { Button, ButtonProps, Form, Icon, Table } from "semantic-ui-react"; import { ProgramSequenceView, ScheduleView } from "@client/components"; import * as route from "@client/routePaths"; +import { ISprinklersDevice } from "@common/httpApi"; import { Program, SprinklersDevice } from "@common/sprinklersRpc"; @observer class ProgramRows extends React.Component<{ - program: Program, device: SprinklersDevice, + program: Program, + iDevice: ISprinklersDevice, + device: SprinklersDevice, routerStore: RouterStore, expanded: boolean, toggleExpanded: (program: Program) => void, }> { render() { - const { program, device, expanded } = this.props; + const { program, iDevice, device, expanded } = this.props; const { sections } = device; const { name, running, enabled, schedule, sequence } = program; const buttonStyle: ButtonProps = { size: "small", compact: false }; - const detailUrl = route.program(device.id, program.id); + const detailUrl = route.program(iDevice.id, program.id); const stopStartButton = (