refactor: use node status to determine Anvil status, and add set_power for an anvil

main
Josue 4 years ago committed by Tsu-ba-me
parent 982a8e4f00
commit ff9460cc49
  1. 20
      striker-ui/components/Anvils/SelectedAnvil.tsx
  2. 4
      striker-ui/types/AnvilSet.d.ts

@ -1,4 +1,4 @@
import { useState, useContext } from 'react';
import { useContext } from 'react';
import { Switch, Box } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import { HeaderText } from '../Text';
@ -6,6 +6,7 @@ import { SELECTED_ANVIL } from '../../lib/consts/DEFAULT_THEME';
import anvilState from '../../lib/consts/ANVILS';
import { AnvilContext } from '../AnvilContext';
import Decorator, { Colours } from '../Decorator';
import putJSON from '../../lib/fetchers/putJSON';
const useStyles = makeStyles(() => ({
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 { uuid } = useContext(AnvilContext);
const classes = useStyles();
const [checked, setChecked] = useState<boolean>(true);
const index = list.findIndex(
(anvil: AnvilListItem) => anvil.anvil_uuid === uuid,
@ -57,7 +63,15 @@ const SelectedAnvil = ({ list }: { list: AnvilListItem[] }): JSX.Element => {
/>
</Box>
<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>
</>
)}

@ -0,0 +1,4 @@
declare type AnvilSet = {
anvil_uuid: string;
is_on: boolean;
};
Loading…
Cancel
Save