Advertisement
Guest User

Untitled

a guest
Feb 7th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState } from "react";
  2. import { createUseStyles } from "react-jss";
  3. import { useCookies } from "react-cookie";
  4. import { Navbar } from "../components/navbar";
  5. import { MDDivider, MDCard, MDCardSection } from "react-md-components";
  6. import { isEmpty } from "lodash";
  7.  
  8. const useStyles = createUseStyles({
  9.   nav: {
  10.     marginTop: ".5rem",
  11.     marginBottom: ".5rem",
  12.     padding: "8px",
  13.     width: "calc(100vw - 16px)",
  14.     display: "grid",
  15.     gridTemplateColumns: "repeat(auto-fill, minmax(150px, 1fr))"
  16.   },
  17.   page: {
  18.     width: "90%",
  19.     marginLeft: "5%",
  20.     marginRight: "5%",
  21.     display: "flex",
  22.     flexDirection: "column"
  23.   },
  24.   list: {
  25.     maxWidth: "650px",
  26.     marginLeft: "auto",
  27.     marginRight: "auto",
  28.     display: "grid",
  29.     gridRowGap: ".5rem",
  30.     gridTemplateColumns: "1fr",
  31.     marginBottom: "1em"
  32.   },
  33.   article: {
  34.     cursor: "pointer"
  35.   }
  36. });
  37. let i = 0;
  38. const Index = props => {
  39.   const styles = useStyles();
  40.   const [cookies] = useCookies(["token", "uid"]);
  41.   const [list, setList] = useState([]);
  42.   const [authors, setAuthors] = useState([]);
  43.   const [fetched, setFetched] = useState(false);
  44.   const [offset, setOffset] = useState(0);
  45.   if (!fetched)
  46.     fetch(`/api/article/fetchlatest?offset=${offset}`)
  47.       .then(res => res.json())
  48.       .then(res => {
  49.         console.log([...res]);
  50.         setList([...res]);
  51.         console.log(list);
  52.         setFetched(true);
  53.         setOffset(offset + 10);
  54.       });
  55.   else if (fetched && i < list.length) {
  56.     const elem = list.splice(i, i + 1)[0];
  57.     if (!isEmpty(elem)) {
  58.       fetch(`/api/user/public/${elem.author}`)
  59.         .then(res => res.json())
  60.         .then(res =>
  61.           authors.includes(res) ? null : setAuthors([...authors, res])
  62.         );
  63.     }
  64.     i++;
  65.   } else {
  66.     console.log(`${i} to ${list.length}`);
  67.   }
  68.   let keylist = 0;
  69.   return (
  70.     <>
  71.       <Navbar />
  72.       <MDDivider />
  73.       <div className={styles.page}>
  74.         <div className={styles.list}>
  75.           {list.map(elem => {
  76.             let media = "";
  77.             let authorname = "";
  78.             try {
  79.               media = JSON.parse(elem.body).blocks.filter(
  80.                 elem => elem.type == "image"
  81.               )[0].data.file.url;
  82.             } catch (error) {}
  83.             try {
  84.               authorname =
  85.                 authors.filter(author => author._id == elem.author)[0].name +
  86.                 " " +
  87.                 authors.filter(author => author._id == elem.author)[0].surname;
  88.             } catch (error) {}
  89.             return (
  90.               <MDCard
  91.                 key={keylist++}
  92.                 subtitle={authorname != "" ? `Di ${authorname}` : null}
  93.                 title={elem.title}
  94.                 media={media}
  95.                 className={styles.article}
  96.                 onClick={() => (window.location.href = `/view/${elem._id}`)}
  97.               >
  98.                 <MDCardSection>
  99.                   {JSON.parse(elem.body)
  100.                     .blocks.filter(elem => elem.type == "paragraph")[0]
  101.                     .data.text.substring(0, 120)
  102.                     .replace(/&nbsp;/g, " ")
  103.                     .replace(/<br>/g, "\n")}
  104.                 </MDCardSection>
  105.               </MDCard>
  106.             );
  107.           })}
  108.         </div>
  109.       </div>
  110.     </>
  111.   );
  112. };
  113. export default Index;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement