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.

53 lines
1.4 KiB

5 years ago
  1. import React, {useState} from "react";
  2. import {Button, Card, CardBody, CardFooter, CardHeader} from "reactstrap";
  3. import useBridges from "../Hooks/bridge";
  4. import BridgeModal from "./Modals/BridgeModal";
  5. import useLights from "../Hooks/lights";
  6. import Loading from "./Loading";
  7. import BridgeLight from "./BridgeLight";
  8. function Bridge({addr, driver, id, name}) {
  9. const {forgetBridge, discoverLights} = useBridges();
  10. const {lightsByBridge} = useLights();
  11. const [modal, setModal] = useState(false);
  12. function edit() {
  13. setModal(true);
  14. }
  15. function unEdit() {
  16. setModal(false);
  17. }
  18. function forget() {
  19. if (window.confirm(`Vil du virkelig glemme bruen "${name}"?`)) {
  20. forgetBridge(id);
  21. }
  22. }
  23. function discover() {
  24. discoverLights(id);
  25. }
  26. const lights = lightsByBridge(id);
  27. const hasLights = lights !== null;
  28. return (
  29. <Card className="mt-3">
  30. <CardHeader>{name} ({driver}, {addr})</CardHeader>
  31. <CardBody>
  32. {hasLights ? lights.map(l => <BridgeLight key={l.id} {...l} />) : <Loading/>}
  33. </CardBody>
  34. <CardFooter>
  35. <Button color="info" onClick={discover}>Oppdag lys</Button>
  36. {" "}
  37. <Button color="secondary" onClick={edit}>Endre</Button>
  38. {" "}
  39. <Button color="danger" onClick={forget}>Glem</Button>
  40. </CardFooter>
  41. {modal && <BridgeModal bridge={{addr, driver, id, name}} onCancel={unEdit} />}
  42. </Card>
  43. );
  44. }
  45. export default Bridge;