materialblock

Untitled

Nov 5th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState } from "react";
  2. import clsx from "clsx";
  3. import axios from "axios";
  4. //Auth Kit
  5. import { useAuthUser, useSignOut } from "react-auth-kit";
  6.  
  7. //Core MaterialUi
  8. import {
  9.   makeStyles,
  10.   CssBaseline,
  11.   Switch,
  12.   Drawer,
  13.   Box,
  14.   AppBar,
  15.   Toolbar,
  16.   List,
  17.   Typography,
  18.   Divider,
  19.   IconButton,
  20.   Container,
  21.   Grid,
  22.   Paper,
  23.   Link,
  24. } from "@material-ui/core";
  25. //Icons
  26. import MenuIcon from "@material-ui/icons/Menu";
  27. import ChevronLeftIcon from "@material-ui/icons/ChevronLeft";
  28. import ExitToAppIcon from "@material-ui/icons/ExitToApp";
  29. import PersonIcon from "@material-ui/icons/Person";
  30.  
  31. //Colors
  32. import {
  33.   orange,
  34.   lightBlue,
  35.   deepPurple,
  36.   deepOrange,
  37. } from "@material-ui/core/colors";
  38. //Components
  39. import { mainListItems } from "./NavList";
  40. // For Switch Theming
  41. import { createMuiTheme, ThemeProvider } from "@material-ui/core/styles";
  42. import { useEffect } from "react";
  43.  
  44. import Card from "@material-ui/core/Card";
  45. import CardContent from "@material-ui/core/CardContent";
  46.  
  47. function Copyright() {
  48.   return (
  49.     <Typography variant="body2" color="textSecondary" align="center">
  50.       {"Copyright © "}
  51.       <Link color="inherit" href="https://github.com/falcon-group">
  52.         falcon-group
  53.       </Link>{" "}
  54.       {new Date().getFullYear()}
  55.       {"."}
  56.     </Typography>
  57.   );
  58. }
  59.  
  60. const drawerWidth = 240;
  61.  
  62. const useStyles = makeStyles(theme => ({
  63.   root: {
  64.     display: "flex",
  65.   },
  66.   toolbar: {
  67.     paddingRight: 24, // keep right padding when drawer closed
  68.   },
  69.   toolbarIcon: {
  70.     display: "flex",
  71.     alignItems: "center",
  72.     justifyContent: "flex-end",
  73.     padding: "0 8px",
  74.     ...theme.mixins.toolbar,
  75.   },
  76.   appBar: {
  77.     zIndex: theme.zIndex.drawer + 1,
  78.     transition: theme.transitions.create(["width", "margin"], {
  79.       easing: theme.transitions.easing.sharp,
  80.       duration: theme.transitions.duration.leavingScreen,
  81.     }),
  82.   },
  83.   appBarShift: {
  84.     marginLeft: drawerWidth,
  85.     width: `calc(100% - ${drawerWidth}px)`,
  86.     transition: theme.transitions.create(["width", "margin"], {
  87.       easing: theme.transitions.easing.sharp,
  88.       duration: theme.transitions.duration.enteringScreen,
  89.     }),
  90.   },
  91.   menuButton: {
  92.     marginRight: 36,
  93.   },
  94.   menuButtonHidden: {
  95.     display: "none",
  96.   },
  97.   title: {
  98.     flexGrow: 1,
  99.   },
  100.   drawerPaper: {
  101.     position: "relative",
  102.     whiteSpace: "nowrap",
  103.     width: drawerWidth,
  104.     transition: theme.transitions.create("width", {
  105.       easing: theme.transitions.easing.sharp,
  106.       duration: theme.transitions.duration.enteringScreen,
  107.     }),
  108.   },
  109.   drawerPaperClose: {
  110.     overflowX: "hidden",
  111.     transition: theme.transitions.create("width", {
  112.       easing: theme.transitions.easing.sharp,
  113.       duration: theme.transitions.duration.leavingScreen,
  114.     }),
  115.     width: theme.spacing(7),
  116.     [theme.breakpoints.up("sm")]: {
  117.       width: theme.spacing(9),
  118.     },
  119.   },
  120.   appBarSpacer: theme.mixins.toolbar,
  121.   content: {
  122.     flexGrow: 1,
  123.     height: "100vh",
  124.     overflow: "auto",
  125.   },
  126.   container: {
  127.     paddingTop: theme.spacing(4),
  128.     paddingBottom: theme.spacing(4),
  129.   },
  130.   paper: {
  131.     padding: theme.spacing(2),
  132.     display: "flex",
  133.     overflow: "auto",
  134.     flexDirection: "column",
  135.   },
  136.   // fixedHeight: {
  137.   //   height: 240
  138.   // }
  139. }));
  140.  
  141. const NotesUser = props => {
  142.   const [open, setOpen] = useState(true);
  143.   const [darkState, setDarkState] = useState(false);
  144.   const [existNotes, setNotesExist] = React.useState(false);
  145.   const [notes, setNotes] = React.useState([]);
  146.   const palletType = darkState ? "dark" : "light";
  147.   const signOut = useSignOut();
  148.   const authUser = useAuthUser();
  149.   const mainPrimaryColor = darkState ? orange[500] : lightBlue[800];
  150.   const mainSecondaryColor = darkState ? deepOrange[900] : deepPurple[500];
  151.   const darkTheme = createMuiTheme({
  152.     palette: {
  153.       type: palletType,
  154.       primary: {
  155.         main: mainPrimaryColor,
  156.       },
  157.       secondary: {
  158.         main: mainSecondaryColor,
  159.       },
  160.     },
  161.   });
  162.   const classes = useStyles();
  163.   // setTimeout(() => {
  164.   //   console.log("Hello, World!");
  165.   // }, 3000);
  166.   const id = props.match.params.id;
  167.   useEffect(() => {
  168.     // GET request using axios inside useEffect React hook
  169.     axios
  170.  
  171.       .get(
  172.         `https://elepsio.herokuapp.com/admin/users/${id}?offset=0&count=100&query=34&orderBy=asc`
  173.       )
  174.       .then(result => setNotes(result.data));
  175.   }, []);
  176.  
  177.   const handleThemeChange = () => {
  178.     setDarkState(!darkState);
  179.   };
  180.   const handleDrawerOpen = () => {
  181.     setOpen(true);
  182.   };
  183.   const handleDrawerClose = () => {
  184.     setOpen(false);
  185.   };
  186.   const fixedHeightPaper = clsx(classes.paper, classes.fixedHeight);
  187.  
  188.   return (
  189.     <ThemeProvider theme={darkTheme}>
  190.       <div className={classes.root}>
  191.         <CssBaseline />
  192.         <AppBar
  193.           position="absolute"
  194.           className={clsx(classes.appBar, open && classes.appBarShift)}
  195.         >
  196.           <Toolbar className={classes.toolbar}>
  197.             <IconButton
  198.               edge="start"
  199.               color="inherit"
  200.               aria-label="open drawer"
  201.               onClick={handleDrawerOpen}
  202.               className={clsx(
  203.                 classes.menuButton,
  204.                 open && classes.menuButtonHidden
  205.               )}
  206.             >
  207.               <MenuIcon />
  208.             </IconButton>
  209.             <Typography
  210.               component="h1"
  211.               variant="h6"
  212.               color="inherit"
  213.               noWrap
  214.               className={classes.title}
  215.             >
  216.               Заметки пользователя {id}
  217.             </Typography>
  218.  
  219.             <Switch checked={darkState} onChange={handleThemeChange} />
  220.             <IconButton color="inherit">
  221.               <PersonIcon />
  222.             </IconButton>
  223.             <Typography component="h" variant="subtitle2">
  224.               {`${authUser().name}`}
  225.             </Typography>
  226.             <IconButton color="inherit" onClick={() => signOut()}>
  227.               <ExitToAppIcon />
  228.             </IconButton>
  229.           </Toolbar>
  230.         </AppBar>
  231.         <Drawer
  232.           variant="permanent"
  233.           classes={{
  234.             paper: clsx(classes.drawerPaper, !open && classes.drawerPaperClose),
  235.           }}
  236.           open={open}
  237.         >
  238.           <div className={classes.toolbarIcon}>
  239.             <IconButton onClick={handleDrawerClose}>
  240.               <ChevronLeftIcon />
  241.             </IconButton>
  242.           </div>
  243.           <Divider />
  244.           <List>{mainListItems}</List>
  245.           <Divider />
  246.         </Drawer>
  247.  
  248.         <main className={classes.content}>
  249.           <div className={classes.appBarSpacer} />
  250.           <Container maxWidth="lg" className={classes.container}>
  251.             <Grid container spacing={3}>
  252.               <Grid item xs={12}>
  253.                 <Paper className={fixedHeightPaper}>
  254.             е
  255.  
  256.                   {/* {existNotes ? (
  257.                     <h1>У данного пользователя нету заметок</h1>
  258.                   ) : null} */}
  259.                   {notes?.map(notes => {
  260.                     return (
  261.                       <Card key={notes._id} style={{ marginBottom: "20px" }}>
  262.                         <CardContent>
  263.                           <Typography
  264.                             className={classes.title}
  265.                             color="textSecondary"
  266.                             gutterBottom
  267.                           >
  268.                             Дата создания: {notes.createDate}
  269.                           </Typography>
  270.                           <Typography
  271.                             className={classes.title}
  272.                             color="textSecondary"
  273.                             gutterBottom
  274.                           >
  275.                             Обновлено: {notes.updateDate}
  276.                           </Typography>
  277.  
  278.                           <Typography variant="h5" component="h2">
  279.                             {notes.title}
  280.                           </Typography>
  281.  
  282.                           <Typography
  283.                             variant="body2"
  284.                             color="textSecondary"
  285.                             component="p"
  286.                           >
  287.                             {notes.content}
  288.                           </Typography>
  289.                         </CardContent>
  290.                       </Card>
  291.                     );
  292.                   })}
  293.  
  294.                   {/* {notes.length === 0 && <h1>Заметок нет</h1>} */}
  295.                   {console.log(notes)}
  296.                 </Paper>
  297.               </Grid>
  298.             </Grid>
  299.  
  300.             <Box pt={4}>
  301.               <Copyright />
  302.             </Box>
  303.           </Container>
  304.         </main>
  305.       </div>
  306.     </ThemeProvider>
  307.   );
  308. };
  309. export default NotesUser;
  310.  
Advertisement
Add Comment
Please, Sign In to add comment