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.

101 lines
2.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import React, {useState} from "react";
  2. import {Button, Card, CardBody, CardFooter, CardHeader, ListGroup} from "reactstrap";
  3. import useLights from "../Hooks/light";
  4. import Light from "./Light";
  5. import Loading from "./Loading";
  6. import {deleteGroup} from "../Helpers/groups";
  7. import ColorModal from "./Modals/ColorModal";
  8. import {changeColor} from "../Helpers/lights";
  9. import GroupPropertiesModal from "./Modals/GroupPropertiesModal";
  10. import useAuth from "../Hooks/auth";
  11. function Group({id, name, permissions}) {
  12. const {user} = useAuth();
  13. const lights = useLights({groupId: id});
  14. const [colorModal, setColorModal] = useState(false);
  15. const [propModal, setPropModal] = useState(false);
  16. const ready = lights !== null;
  17. const noLights = ready && lights.length === 0;
  18. const hasLights = ready && lights.length > 0;
  19. if (id === 0 && noLights) {
  20. return <></>;
  21. }
  22. function onDelete() {
  23. if (window.confirm(`Vil du virkelig fjerne "${name}"?`)) {
  24. deleteGroup(id);
  25. }
  26. }
  27. let cValue = null;
  28. let bValue = null;
  29. let pValue = null;
  30. if (hasLights) {
  31. cValue = lights[0].color;
  32. bValue = lights[0].brightness;
  33. pValue = lights[0].on;
  34. }
  35. function iCan(name) {
  36. if (permissions === null) {
  37. return false;
  38. }
  39. const perm = permissions.find(p => p.userId === user.id);
  40. return typeof perm !== "undefined"
  41. ? perm[name]
  42. : false;
  43. }
  44. return (
  45. <Card className="mt-3">
  46. <CardHeader>{name}</CardHeader>
  47. <CardBody>
  48. {ready
  49. ? <ListGroup>{lights.map(light => <Light key={light.id} {...light} />)}</ListGroup>
  50. : <Loading/>}
  51. </CardBody>
  52. {(iCan("manage") || iCan("write") || iCan("delete")) && (
  53. <CardFooter>
  54. {iCan("manage") && (
  55. <Button color="primary" onClick={() => setPropModal(true)}>Detaljer</Button>
  56. )}
  57. {" "}
  58. {hasLights && iCan("write") && (
  59. <Button color="secondary" onClick={() => setColorModal(true)}>Skift farger</Button>
  60. )}
  61. {" "}
  62. {iCan("delete") && (id > 0) && (
  63. <Button color="danger" onClick={onDelete}>Fjern</Button>
  64. )}
  65. </CardFooter>
  66. )}
  67. {colorModal && (
  68. <ColorModal cValue={cValue}
  69. bValue={bValue}
  70. pValue={pValue}
  71. onConfirm={(color, brightness, power) => {
  72. lights.forEach(light => {
  73. changeColor(light.id, color, brightness, power);
  74. setColorModal(false);
  75. });
  76. }}
  77. onCancel={() => setColorModal(false)}
  78. />
  79. )}
  80. {propModal && (
  81. <GroupPropertiesModal id={id}
  82. nValue={name}
  83. permissions={permissions}
  84. onClose={() => setPropModal(false)}
  85. />
  86. )}
  87. </Card>
  88. );
  89. }
  90. export default Group;