Good changes
This commit is contained in:
parent
79de78c78d
commit
d71ca58414
@ -3,9 +3,10 @@ import { observer } from "mobx-react";
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import FontAwesome = require("react-fontawesome");
|
import FontAwesome = require("react-fontawesome");
|
||||||
import { Header, Item } from "semantic-ui-react";
|
import { Header, Item } from "semantic-ui-react";
|
||||||
import { ProgramTable, RunSectionForm, SectionRunnerView, SectionTable } from ".";
|
|
||||||
|
|
||||||
|
import { injectState, State } from "@app/state";
|
||||||
import { SprinklersDevice } from "@common/sprinklers";
|
import { SprinklersDevice } from "@common/sprinklers";
|
||||||
|
import { ProgramTable, RunSectionForm, SectionRunnerView, SectionTable } from ".";
|
||||||
|
|
||||||
const ConnectionState = ({ connected }: { connected: boolean }) => {
|
const ConnectionState = ({ connected }: { connected: boolean }) => {
|
||||||
const classes = classNames({
|
const classes = classNames({
|
||||||
@ -21,12 +22,27 @@ const ConnectionState = ({ connected }: { connected: boolean }) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
class DeviceView extends React.Component<{ device: SprinklersDevice }> {
|
interface DeviceViewProps {
|
||||||
|
deviceId: string;
|
||||||
|
state: State;
|
||||||
|
}
|
||||||
|
|
||||||
|
class DeviceView extends React.Component<DeviceViewProps> {
|
||||||
|
device: SprinklersDevice;
|
||||||
|
|
||||||
|
componentWillMount() {
|
||||||
|
this.updateDevice();
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUpdate() {
|
||||||
|
this.updateDevice();
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { id, connected, sections, programs, sectionRunner } = this.props.device;
|
const { id, connected, sections, programs, sectionRunner } = this.device;
|
||||||
return (
|
return (
|
||||||
<Item>
|
<Item>
|
||||||
<Item.Image src={require<string>("@app/images/raspberry_pi.png")} />
|
<Item.Image src={require("@app/images/raspberry_pi.png")} />
|
||||||
<Item.Content>
|
<Item.Content>
|
||||||
<Header as="h1">
|
<Header as="h1">
|
||||||
<span>Device </span><kbd>{id}</kbd>
|
<span>Device </span><kbd>{id}</kbd>
|
||||||
@ -43,6 +59,13 @@ class DeviceView extends React.Component<{ device: SprinklersDevice }> {
|
|||||||
</Item>
|
</Item>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private updateDevice() {
|
||||||
|
const { state, deviceId } = this.props;
|
||||||
|
if (!this.device || this.device.id !== deviceId) {
|
||||||
|
this.device = state.sprinklersApi.getDevice(deviceId);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default observer(DeviceView);
|
export default injectState(observer(DeviceView));
|
||||||
|
@ -2,13 +2,11 @@ import { observer } from "mobx-react";
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
|
|
||||||
import DeviceView from "@app/components/DeviceView";
|
import DeviceView from "@app/components/DeviceView";
|
||||||
import { injectState, State } from "@app/state";
|
|
||||||
|
|
||||||
class DevicesView extends React.Component<{ state: State }> {
|
class DevicesView extends React.Component {
|
||||||
render() {
|
render() {
|
||||||
const { device } = this.props.state;
|
return <DeviceView deviceId="grinklers" />;
|
||||||
return <DeviceView device={device} />;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default injectState(observer(DevicesView));
|
export default observer(DevicesView);
|
||||||
|
@ -5,18 +5,21 @@ import { UiMessage, UiStore } from "./ui";
|
|||||||
export { UiMessage, UiStore };
|
export { UiMessage, UiStore };
|
||||||
export * from "./inject";
|
export * from "./inject";
|
||||||
|
|
||||||
export class State {
|
export interface IState {
|
||||||
client: ISprinklersApi = new MqttApiClient();
|
sprinklersApi: ISprinklersApi;
|
||||||
device: SprinklersDevice;
|
uiStore: UiStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class State implements IState {
|
||||||
|
sprinklersApi: ISprinklersApi = new MqttApiClient();
|
||||||
uiStore = new UiStore();
|
uiStore = new UiStore();
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.device = this.client.getDevice("grinklers");
|
|
||||||
this.uiStore.addMessage({ header: "asdf", content: "boo!", error: true });
|
this.uiStore.addMessage({ header: "asdf", content: "boo!", error: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
this.client.start();
|
this.sprinklersApi.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ export class ProvideState extends React.Component<{
|
|||||||
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T];
|
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T];
|
||||||
type Omit<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]};
|
type Omit<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]};
|
||||||
|
|
||||||
export function injectState<P extends { "state": State }, T extends React.ComponentClass<P>>(Component: T) {
|
export function injectState<P extends { "state": State }>(Component: React.ComponentType<P>) {
|
||||||
return class extends React.Component<Omit<P, "state">> {
|
return class extends React.Component<Omit<P, "state">> {
|
||||||
static contextTypes = providedStateContextTypes;
|
static contextTypes = providedStateContextTypes;
|
||||||
context: IProvidedStateContext;
|
context: IProvidedStateContext;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user