Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CustomAccordition.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React, { useState } from "react";
  2. // import { useSelector, useDispatch } from "react-redux";
  3. import Accordion from "@mui/material/Accordion";
  4. import AccordionSummary from "@mui/material/AccordionSummary";
  5. import AccordionDetails from "@mui/material/AccordionDetails";
  6. import Typography from "@mui/material/Typography";
  7. import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
  8. import CustomerRequest from "../CustomerRequest/CustomerRequest";
  9. import requestService from "../../services/requestService";
  10. const CustomAccordition = ({ room, onEmptyRoom }) => {
  11. const [customers, setCustomers] = useState([]);
  12. const [loading, setLoading] = useState(true);
  13. const [expanded, setExpanded] = useState(false);
  14. const showCustomerRequests = () => {
  15. if (expanded === false) {
  16. requestService
  17. .getCusotmersForSpecificRequestRoom(room.id)
  18. .then((res) => setCustomers(res))
  19. .catch((e) => console.log(e))
  20. .finally(() => setLoading(false));
  21. }
  22. setExpanded((oldState) => !oldState);
  23. };
  24. const requestHandled = (id) => {
  25. let tmpArr = [...customers];
  26. tmpArr = tmpArr.filter((customer) => customer.id !== id);
  27. setCustomers(tmpArr);
  28. if (tmpArr.length === 0) {
  29. onEmptyRoom(room.id);
  30. }
  31. };
  32. return (
  33. <Accordion style={{ marginTop: 10 }} expanded={expanded}>
  34. <AccordionSummary
  35. expandIcon={<ExpandMoreIcon />}
  36. aria-controls="panel1a-content"
  37. id="panel1a-header"
  38. onClick={() => showCustomerRequests()}
  39. >
  40. <Typography>{room.name}</Typography>
  41. </AccordionSummary>
  42. {loading ? (
  43. <p>Loading...</p>
  44. ) : (
  45. <AccordionDetails>
  46. {customers.map((customer, index) => (
  47. <CustomerRequest
  48. key={index}
  49. customer={{ roomId: room.id, ...customer }}
  50. requestHandled={requestHandled}
  51. />
  52. ))}
  53. </AccordionDetails>
  54. )}
  55. </Accordion>
  56. );
  57. };
  58. export default CustomAccordition;