Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useEffect, useContext } from "react";
  2. import AuthContext from "../../../contexts/auth.context";
  3. import UserContext from "../../../contexts/user.context";
  4. import { catchUtil } from "../../../api/apiUtils";
  5. import ProjectAPI from "../../../api/project.api";
  6. import AdminAPI from "../../../api/admin.api";
  7.  
  8. import Projects from "./Projects";
  9.  
  10. function ProjectsContainer({ closeNavbarForMobile }: any) {
  11.   let [error, setError] = useState(""),
  12.     [as, setAs] = useState(false),
  13.     [projects, setProjects] = useState<Project[]>([]),
  14.     [pagination, setPagination] = useState({
  15.       totalPages: 1,
  16.       currentPage: 0
  17.     }),
  18.     [projectCount, setProjectCount] = useState(0),
  19.     [searchInput, setSearchInput] = useState(""),
  20.     [displayZeroProjectsCard, setDisplayZeroProjectsCard] = useState(false);
  21.  
  22.   const authContext = useContext(AuthContext);
  23.   const userContext = useContext(UserContext);
  24.  
  25.   const triggerSearch = async () => {
  26.     console.log("searching for: ", searchInput);
  27.   };
  28.  
  29.   const fetchPage = async (page: { selected: number }) => {
  30.     try {
  31.       let {
  32.         data: { data, meta }
  33.       } = await (userContext.user.admin.as ? AdminAPI : ProjectAPI)(
  34.         authContext
  35.       ).list(page.selected + 1);
  36.  
  37.       setPagination({
  38.         totalPages: meta.total_pages,
  39.         currentPage: meta.currentPage
  40.       });
  41.  
  42.       setProjectCount(meta.total);
  43.       if (meta.total === 0) setDisplayZeroProjectsCard(true);
  44.  
  45.       setProjects(data);
  46.     } catch (err) {
  47.       catchUtil(err, setError);
  48.     }
  49.   };
  50.  
  51.   useEffect(() => {
  52.     closeNavbarForMobile();
  53.     setAs(userContext.user.admin.as);
  54.     // eslint-disable-next-line
  55.   }, []);
  56.  
  57.   useEffect(() => {
  58.     if (userContext.user.admin.as === as) return;
  59.     setAs(oldValue => !oldValue);
  60.  
  61.     setPagination({
  62.       totalPages: 1,
  63.       currentPage: 0
  64.     });
  65.     // eslint-disable-next-line
  66.   }, [userContext]);
  67.  
  68.   useEffect(() => {
  69.     fetchPage({ selected: pagination.currentPage });
  70.     // eslint-disable-next-line
  71.   }, [as]);
  72.  
  73.   return (
  74.     <Projects
  75.       error={error}
  76.       projects={projects}
  77.       pagination={pagination}
  78.       fetchPage={fetchPage}
  79.       projectCount={projectCount}
  80.       searchInput={searchInput}
  81.       setSearchInput={setSearchInput}
  82.       triggerSearch={triggerSearch}
  83.       displayZeroProjectsCard={displayZeroProjectsCard}
  84.     />
  85.   );
  86. }
  87.  
  88. export default ProjectsContainer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement