refactor: add sending a request to the backend when switches change state for power and memebership

main
Josue 4 years ago committed by Tsu-ba-me
parent 2fe146d58c
commit 73af9f6e57
  1. 23
      striker-ui/components/Nodes/AnvilNode.tsx
  2. 11
      striker-ui/lib/fetchers/putJSON.ts

@ -6,6 +6,8 @@ import { BodyText } from '../Text';
import nodeState from '../../lib/consts/NODES';
import Decorator, { Colours } from '../Decorator';
import putJSON from '../../lib/fetchers/putJSON';
const useStyles = makeStyles((theme) => ({
root: {
overflow: 'auto',
@ -84,13 +86,30 @@ const AnvilNode = ({
<BodyText text="Power: " />
</Box>
<Box flexGrow={1}>
<Switch checked={node.state === 'ready'} />
<Switch
checked={node.state === 'ready'}
onChange={() =>
putJSON('/anvils/set_power', {
host_uuid: node.node_uuid,
is_on: !(node.state === 'ready'),
})
}
/>
</Box>
<Box className={classes.label}>
<BodyText text="Member: " />
</Box>
<Box>
<Switch checked disabled={!node.removable} />
<Switch
checked={node.state === 'ready'}
disabled={!node.removable}
onChange={() =>
putJSON('/anvils/set_membership', {
host_uuid: node.node_uuid,
is_member: !(node.state === 'ready'),
})
}
/>
</Box>
</Box>
{node.state !== 'ready' && (

@ -0,0 +1,11 @@
const putJSON = <T>(uri: string, data: T): void => {
fetch(`${process.env.NEXT_PUBLIC_API_URL}${uri}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
};
export default putJSON;
Loading…
Cancel
Save