The main server, and probably only repository in this org.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.1 KiB

5 years ago
  1. import React, {useEffect, useState} from "react";
  2. import useLights from "../Hooks/light";
  3. import {Card, CardBody, CardHeader, Col, CustomInput, FormGroup} from "reactstrap";
  4. import ColorPicker from "./Misc/ColorPicker";
  5. import {changeColor} from "../Helpers/lights";
  6. import BrightnessSlider from "./Misc/BrightnessSlider";
  7. function LGroup({id, name}) {
  8. const lights = useLights({groupId: id});
  9. const light = lights !== null && lights.length > 0 ? lights[0] : null;
  10. const [color, setColor] = useState(null);
  11. const [brightness, setBrightness] = useState(null);
  12. const [power, setPower] = useState(null);
  13. useEffect(() => {
  14. if (light !== null) {
  15. setColor(light.color);
  16. setBrightness(light.brightness);
  17. setPower(light.on);
  18. }
  19. }, [light]);
  20. if (light === null || color === null || brightness === null || power === null) {
  21. return <></>;
  22. }
  23. function recolor(newColor) {
  24. setColor(newColor);
  25. lights.forEach(l => changeColor(l.id, newColor, null, null));
  26. }
  27. function rebrightness(newBrightness) {
  28. setBrightness(newBrightness);
  29. lights.forEach(l => changeColor(l.id, null, newBrightness, null));
  30. }
  31. function repower(newPower) {
  32. setPower(newPower);
  33. lights.forEach(l => changeColor(l.id, null, null, newPower));
  34. }
  35. return (
  36. <Col lg={4} md={6} sm={12}>
  37. <Card className="mt-3">
  38. <CardHeader>
  39. {name}
  40. </CardHeader>
  41. <CardBody>
  42. <FormGroup>
  43. {power && <ColorPicker color={color} onChange={recolor}/>}
  44. </FormGroup>
  45. <FormGroup>
  46. {power && <BrightnessSlider brightness={brightness} onChange={rebrightness}/>}
  47. </FormGroup>
  48. <FormGroup className="on-switch">
  49. <CustomInput type="switch"
  50. id={`switch-power-${id}`}
  51. name="power"
  52. label={power ? "På" : "Av"}
  53. checked={power}
  54. onChange={e => repower(e.target.checked)}/>
  55. </FormGroup>
  56. </CardBody>
  57. </Card>
  58. </Col>
  59. );
  60. }
  61. export default LGroup;