Advertisement
Guest User

App.js

a guest
Jul 12th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import axios from 'axios';
  3.  
  4. import Organization from './Organization';
  5.  
  6. const axiosGitHubGraphQL = axios.create({
  7.     baseURL: 'https://api.github.com/graphql',
  8.     headers: {
  9.         Authorization: `bearer ${process.env.REACT_APP_GITHUB_PERSONAL_ACCESS_TOKEN}`,
  10.     },
  11. });
  12.  
  13. const GET_REPOSITORY_ISSUES = `
  14.     query GetRepositoryIssues(
  15.         $organization: String!,
  16.         $repository: String!,
  17.         $cursor: String
  18.     ) {
  19.         organization(login: $organization) {
  20.             name
  21.             url
  22.             repository(name: $repository) {
  23.                 id
  24.                 name
  25.                 url
  26.                 viewerHasStarred
  27.                 issues(first: 5, after: $cursor, states: [OPEN]) {
  28.                     edges {
  29.                         node {
  30.                             id
  31.                             title
  32.                             url
  33.                             reactions(last: 3) {
  34.                                 edges {
  35.                                     node {
  36.                                         id
  37.                                         content
  38.                                     }
  39.                                 }
  40.                             }
  41.                         }
  42.                     }
  43.                     totalCount
  44.                     pageInfo {
  45.                         endCursor
  46.                         hasNextPage
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.     }
  52. `;
  53.  
  54. const ADD_STAR = `
  55.     mutation AddStar($repositoryId: ID!) {
  56.         addStar(input: { starrableId: $repositoryId }) {
  57.             starrable {
  58.                 viewerHasStarred
  59.             }
  60.         }
  61.     }
  62. `;
  63.  
  64. const REMOVE_STAR = `
  65.     mutation RemoveStar($repositoryId: ID!) {
  66.         removeStar(input: { starrableId: $repositoryId }) {
  67.             starrable {
  68.                 viewerHasStarred
  69.             }
  70.         }
  71.     }
  72. `;
  73.  
  74. const App = () => {
  75.     const [path, setPath] = React.useState(
  76.         'the-road-to-graphql/the-road-to-graphql'
  77.     );
  78.     const [organization, setOrganization] = React.useState(null);
  79.     const [errors, setErrors] = React.useState(null);
  80.     const [fetching, setFetching] = React.useState(false);
  81.     const [starring, setStarring] = React.useState(false);
  82.  
  83.     const starRepository = async (viewerHasStarred, repositoryId) => {
  84.         setStarring(true);
  85.         const result = await axiosGitHubGraphQL.post('', {
  86.             query: viewerHasStarred ? REMOVE_STAR : ADD_STAR,
  87.             variables: { repositoryId },
  88.         });
  89.         setStarring(false);
  90.  
  91.         // Modify organization on star
  92.         setOrganization((initialValue) => {
  93.             const newOrganization = { ...initialValue };
  94.             newOrganization.repository.viewerHasStarred = viewerHasStarred
  95.                 ? result.data.data.removeStar.starrable.viewerHasStarred
  96.                 : result.data.data.addStar.starrable.viewerHasStarred;
  97.             return { ...newOrganization };
  98.         });
  99.     };
  100.  
  101.     const fetchData = React.useCallback(
  102.         async (cursor) => {
  103.             setFetching(true);
  104.             const [org, repository] = path.split('/');
  105.             const { data } = await axiosGitHubGraphQL.post('', {
  106.                 query: GET_REPOSITORY_ISSUES,
  107.                 variables: { organization: org, repository, cursor },
  108.             });
  109.             setFetching(false);
  110.  
  111.             if (data.errors) return setErrors(data.errors);
  112.  
  113.             // Modify organization on more issues
  114.             if (!cursor) {
  115.                 setOrganization(data.data.organization);
  116.                 return;
  117.             }
  118.  
  119.             setOrganization((initialValue) => {
  120.                 const newOrganization = { ...initialValue };
  121.  
  122.                 const oldIssues = [...newOrganization.repository.issues.edges];
  123.                 const newIssues = [...data.data.organization.repository.issues.edges];
  124.  
  125.                 newOrganization.repository.issues.edges = [...newIssues, ...oldIssues];
  126.  
  127.                 return newOrganization;
  128.             });
  129.         },
  130.         [path]
  131.     );
  132.  
  133.     const pathChange = (e) => setPath(e.target.value);
  134.     const formSubmit = (e) => {
  135.         e.preventDefault();
  136.         fetchData();
  137.     };
  138.  
  139.     const fetchMoreIssues = () => {
  140.         const { endCursor } = organization.repository.issues.pageInfo;
  141.         fetchData(endCursor);
  142.     };
  143.  
  144.     React.useEffect(() => {
  145.         fetchData();
  146.     }, [fetchData]);
  147.  
  148.     return (
  149.         <div className="App">
  150.             <form onSubmit={formSubmit}>
  151.                 <label htmlFor="url">Show open issues for https://github.com/</label>
  152.                 <input id="url" type="text" value={path} onChange={pathChange} />
  153.                 <button disabled={fetching} type="submit">
  154.                     Search
  155.                 </button>
  156.             </form>
  157.  
  158.             {organization || errors ? (
  159.                 <Organization
  160.                     organization={organization}
  161.                     fetchMoreIssues={fetchMoreIssues}
  162.                     errors={errors}
  163.                     fetching={fetching}
  164.                     starRepository={starRepository}
  165.                     starring={starring}
  166.                 />
  167.             ) : (
  168.                 <p>Fetching</p>
  169.             )}
  170.         </div>
  171.     );
  172. };
  173.  
  174. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement