-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwitchControl.tsx
47 lines (41 loc) · 1.21 KB
/
SwitchControl.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { styled } from '@mui/system';
import { Box } from '@mui/material';
import { useSetValueMutation } from '../../hooks/useApi';
import { ChannelHeader } from '../components/ChannelHeader';
import { SwitchVirtualReceiverChannel } from 'src/types/types';
interface ControlProps {
channel: SwitchVirtualReceiverChannel;
refetch: () => void;
}
const StyledBox = styled(Box)({
display: 'flex',
flexDirection: 'column',
});
const ChannelHeaderWithPointer = styled(ChannelHeader)({
cursor: 'pointer',
});
export const SwitchControl = ({ channel, refetch }: ControlProps) => {
const setValueMutation = useSetValueMutation();
const { datapoints, name, address, interfaceName } = channel;
const checked = datapoints.STATE === 'true';
const onHandleChange = async () => {
await setValueMutation.mutateAsync({
interface: interfaceName,
address,
valueKey: 'STATE',
type: 'boolean',
value: !checked,
});
refetch();
};
return (
<StyledBox>
<ChannelHeaderWithPointer
name={name}
icon={checked ? 'mdi:light-switch' : 'mdi:light-switch-off'}
color={checked ? 'orange' : 'unset'}
onClick={onHandleChange}
/>
</StyledBox>
);
};