refactor: use node status to determine Anvil status, and add set_power for an anvil
This commit is contained in:
parent
982a8e4f00
commit
ff9460cc49
@ -1,4 +1,4 @@
|
|||||||
import { useState, useContext } from 'react';
|
import { useContext } from 'react';
|
||||||
import { Switch, Box } from '@material-ui/core';
|
import { Switch, Box } from '@material-ui/core';
|
||||||
import { makeStyles } from '@material-ui/core/styles';
|
import { makeStyles } from '@material-ui/core/styles';
|
||||||
import { HeaderText } from '../Text';
|
import { HeaderText } from '../Text';
|
||||||
@ -6,6 +6,7 @@ import { SELECTED_ANVIL } from '../../lib/consts/DEFAULT_THEME';
|
|||||||
import anvilState from '../../lib/consts/ANVILS';
|
import anvilState from '../../lib/consts/ANVILS';
|
||||||
import { AnvilContext } from '../AnvilContext';
|
import { AnvilContext } from '../AnvilContext';
|
||||||
import Decorator, { Colours } from '../Decorator';
|
import Decorator, { Colours } from '../Decorator';
|
||||||
|
import putJSON from '../../lib/fetchers/putJSON';
|
||||||
|
|
||||||
const useStyles = makeStyles(() => ({
|
const useStyles = makeStyles(() => ({
|
||||||
root: {
|
root: {
|
||||||
@ -32,10 +33,15 @@ const selectDecorator = (state: string): Colours => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const isAnvilOn = (anvil: AnvilListItem): boolean =>
|
||||||
|
!(
|
||||||
|
anvil.nodes.findIndex(({ state }: AnvilStatusNode) => state !== 'off') ===
|
||||||
|
-1
|
||||||
|
);
|
||||||
|
|
||||||
const SelectedAnvil = ({ list }: { list: AnvilListItem[] }): JSX.Element => {
|
const SelectedAnvil = ({ list }: { list: AnvilListItem[] }): JSX.Element => {
|
||||||
const { uuid } = useContext(AnvilContext);
|
const { uuid } = useContext(AnvilContext);
|
||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
const [checked, setChecked] = useState<boolean>(true);
|
|
||||||
|
|
||||||
const index = list.findIndex(
|
const index = list.findIndex(
|
||||||
(anvil: AnvilListItem) => anvil.anvil_uuid === uuid,
|
(anvil: AnvilListItem) => anvil.anvil_uuid === uuid,
|
||||||
@ -57,7 +63,15 @@ const SelectedAnvil = ({ list }: { list: AnvilListItem[] }): JSX.Element => {
|
|||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
<Box p={1}>
|
<Box p={1}>
|
||||||
<Switch checked={checked} onChange={() => setChecked(!checked)} />
|
<Switch
|
||||||
|
checked={isAnvilOn(list[index])}
|
||||||
|
onChange={() =>
|
||||||
|
putJSON('/anvils/set_power', {
|
||||||
|
anvil_uuid: list[index].anvil_uuid,
|
||||||
|
is_on: !isAnvilOn(list[index]),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
4
striker-ui/types/AnvilSet.d.ts
vendored
Normal file
4
striker-ui/types/AnvilSet.d.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
declare type AnvilSet = {
|
||||||
|
anvil_uuid: string;
|
||||||
|
is_on: boolean;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user