Category.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { List, ListItem, ListItemButton, ListItemText, Button } from "@mui/material"
  2. import { Typography } from "@mui/material"
  3. import { Box, Container } from "@mui/system"
  4. import { useState } from "react"
  5. import { useEffect } from "react"
  6. import { connect, useDispatch, useSelector } from "react-redux"
  7. import { useParams } from "react-router-dom"
  8. import { MyLink } from "."
  9. import { isCurrentUserAdmin, useGetCategoryByIdQuery } from "../reducers"
  10. import { actionSetCurrentEntity, frontEndNames, getCurrentEntity } from "../reducers/frontEndReducer"
  11. import { CategoryBreadcrumbs } from "./CategoryBreadcrumbs"
  12. import { CGoodsList } from "./GoodsList"
  13. import { LoadingState } from "./LoadingState"
  14. import { CatsList } from "./RootCats"
  15. const CSubCategories = connect(state => ({ cats: getCurrentEntity(frontEndNames.category, state)?.subCategories }),
  16. {})(CatsList);
  17. const Category = () => {
  18. const { _id } = useParams();
  19. const { isLoading, data } = useGetCategoryByIdQuery(_id);
  20. let cat = isLoading ? { name: 'loading', goods: [] } : data?.CategoryFindOne;
  21. let csubCats = false;
  22. const dispatch = useDispatch();
  23. let state = useSelector(state => state);
  24. useEffect(() => {
  25. if (getCurrentEntity(frontEndNames.category, state)?._id != _id)
  26. dispatch(actionSetCurrentEntity(frontEndNames.category, { _id }));
  27. if (!isLoading)
  28. dispatch(actionSetCurrentEntity(frontEndNames.category, data.CategoryFindOne));
  29. }, [_id, isLoading, data]);
  30. let isAdmin = isCurrentUserAdmin(state);
  31. return isLoading ? <LoadingState /> : (
  32. <>
  33. <Container>
  34. <Box>
  35. <CategoryBreadcrumbs category={cat} />
  36. {
  37. isAdmin && (
  38. <MyLink to="/editgood">
  39. <Button size='small' variant="contained" >
  40. Add Good
  41. </Button>
  42. </MyLink>
  43. )
  44. }
  45. <Typography paragraph gutterBottom component={'h3'} variant={'h3'} sx={{marginTop: "1vh"} } >
  46. {cat.name}
  47. </Typography>
  48. {csubCats && <CSubCategories />}
  49. {!csubCats && cat.subCategories?.length > 0 && (
  50. <List>
  51. {cat.subCategories.map(scat => (
  52. <ListItem key={scat._id} disablePadding>
  53. <ListItemButton>
  54. <MyLink to={`/category/${scat._id}`}>
  55. <ListItemText primary={scat.name} />
  56. </MyLink>
  57. </ListItemButton>
  58. </ListItem>
  59. ))}
  60. </List>
  61. )
  62. }
  63. <CGoodsList goods={cat.goods} />
  64. </Box>
  65. </Container>
  66. </>
  67. )
  68. }
  69. const CCategory = connect(state => ({}),
  70. {})(Category);
  71. export { CCategory };