Created
February 21, 2020 16:53
-
-
Save r3dm1ke/3bed1f6b393fc6afd1972e9fd26d155d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, {useState, useEffect} from 'react'; | |
| import {useQuery} from "@apollo/react-hooks"; | |
| import {Typography, makeStyles, CircularProgress} from "@material-ui/core"; | |
| import {useDebounce} from "use-debounce"; | |
| import {SEARCH_FOR_REPOS} from "./queries"; | |
| import Repository from "./Repository"; | |
| const useStyles = makeStyles({ | |
| note: { | |
| marginTop: '1rem', | |
| textAlign: 'center', | |
| }, | |
| spinnerContainer: { | |
| display: 'flex', | |
| justifyContent: 'space-around', | |
| marginTop: '1rem' | |
| } | |
| }); | |
| const RepositoryList = ({searchTerm}) => { | |
| const classes = useStyles(); | |
| const [expandedRepo, setExpandedRepo] = useState(null); | |
| const [debouncedSearchTerm] = useDebounce(searchTerm, 1000); | |
| const {data, loading, error} = useQuery(SEARCH_FOR_REPOS, | |
| {variables: {search_term: debouncedSearchTerm}} | |
| ); | |
| useEffect(() => { | |
| setExpandedRepo(null); | |
| }, [data]); | |
| if (loading) { | |
| return ( | |
| <div className={classes.spinnerContainer}> | |
| <CircularProgress /> | |
| </div> | |
| ); | |
| } | |
| if (error) { | |
| return ( | |
| <Typography | |
| variant={'overline'} | |
| className={classes.note} | |
| component={'div'} | |
| color={'error'} | |
| > | |
| {error} | |
| </Typography> | |
| ) | |
| } | |
| if (!data.search.repositoryCount) { | |
| return ( | |
| <Typography | |
| variant={'overline'} | |
| className={classes.note} | |
| component={'div'} | |
| > | |
| There are no such repositories! | |
| </Typography> | |
| ) | |
| } | |
| return ( | |
| <div> | |
| {data.search.edges.map((repo, i) => ( | |
| <Repository | |
| repo={repo} | |
| expanded={expandedRepo === i} | |
| onToggled={() => setExpandedRepo(i)} | |
| key={i} | |
| /> | |
| ))} | |
| </div> | |
| ); | |
| }; | |
| export default RepositoryList; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment