Skip to content

Commit 7284235

Browse files
committed
fix(projects): simplified useApolloClient hook
Previously, in order to use the Github API client, you had to: 1. Get the GitHub token from the appState 2. Call the `useApolloClient` hook and get the `initApolloClient` function 3. Call the `initApolloClient` function with the GitHub token as an argument and get the client But since `useApolloClient` is a hook, this commit simplifies it so that the appState is accessed directly from within the hook, and the actual API client is returned directly, so that the above steps get simplified to: 1. Call the `useApolloClient` hook and get the client
1 parent cbbd9ed commit 7284235

File tree

5 files changed

+34
-45
lines changed

5 files changed

+34
-45
lines changed

apps/projects/app/App.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,13 @@ const App = () => {
5050
const [ githubLoading, setGithubLoading ] = useState(false)
5151
const [ panel, setPanel ] = useState(null)
5252
const [ panelProps, setPanelProps ] = useState(null)
53-
const initApolloClient = useApolloClient()
53+
const client = useApolloClient()
5454

5555
const {
5656
github = { status : STATUS.INITIAL },
5757
isSyncing = true,
5858
} = appState
5959

60-
const client = initApolloClient(github.token)
61-
6260
const handlePopupMessage = useCallback(message => {
6361
if (!popupRef) return
6462
if (message.data.from !== 'popup') return

apps/projects/app/components/Content/IssueDetail/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo } from 'react'
1+
import React from 'react'
22
import PropTypes from 'prop-types'
33
import { useAragonApi } from '../../../api-react'
44
import {
@@ -44,9 +44,7 @@ Wrap.propTypes = {
4444
}
4545

4646
const IssueDetail = ({ issueId }) => {
47-
const { appState: { github } } = useAragonApi()
48-
const initApolloClient = useApolloClient()
49-
const client = useMemo(() => initApolloClient(github.token), [])
47+
const client = useApolloClient()
5048
const { layoutName } = useLayout()
5149
const shapeIssue = useShapedIssue()
5250
const { loading, error, data } = useQuery(GET_ISSUE, {

apps/projects/app/components/Content/Issues.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { compareAsc, compareDesc } from 'date-fns'
1010
import { useApolloClient } from '../../utils/apollo-client'
1111
import useShapedIssue from '../../hooks/useShapedIssue'
1212
import usePathSegments from '../../hooks/usePathSegments'
13-
import { STATUS } from '../../utils/github'
1413
import { getIssuesGQL } from '../../utils/gql-queries.js'
1514
import { Issue } from '../Card'
1615
import { EmptyWrapper, FilterBar, LoadingAnimation, Tabs } from '../Shared'
@@ -35,7 +34,7 @@ class Issues extends React.PureComponent {
3534
filters: PropTypes.object.isRequired,
3635
graphqlQuery: PropTypes.shape({
3736
data: PropTypes.object,
38-
error: PropTypes.string,
37+
error: PropTypes.object,
3938
loading: PropTypes.bool.isRequired,
4039
refetch: PropTypes.func,
4140
}).isRequired,
@@ -360,17 +359,15 @@ IssuesQuery.propTypes = {
360359
}
361360

362361
const IssuesWrap = props => {
363-
const initApolloClient = useApolloClient()
362+
const client = useApolloClient()
364363
const { appState } = useAragonApi()
365364
const {
366365
repos,
367366
issues = [],
368-
github = { status : STATUS.INITIAL },
369367
} = appState
370368
const shapeIssue = useShapedIssue()
371369
const { query: { repoId }, selectIssue } = usePathSegments()
372370
const { setupNewIssue } = usePanelManagement()
373-
const [ client, setClient ] = useState(null)
374371
const [ downloadedRepos, setDownloadedRepos ] = useState({})
375372
const [ query, setQuery ] = useState(null)
376373
const [ filters, setFilters ] = useState({
@@ -417,11 +414,6 @@ const IssuesWrap = props => {
417414
setQuery(getIssuesGQL(reposQueryParams))
418415
}, [ downloadedRepos, filters, repos ])
419416

420-
useEffect(() => {
421-
const apolloClient = initApolloClient(github.token)
422-
setClient(apolloClient)
423-
}, [github.token])
424-
425417
return (
426418
<>
427419
<Header

apps/projects/app/hooks/useGithubAuth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { CURRENT_USER } from '../utils/gql-queries'
55

66
const useGithubAuth = () => {
77
const { appState } = useAragonApi()
8-
const initApolloClient = useApolloClient()
8+
const client = useApolloClient()
99
const token = appState.github && appState.github.token
1010

1111
const [ githubCurrentUser, setGithubCurrentUser ] = useState({})
1212

1313
useEffect(() => {
1414
if (!token) return
1515

16-
initApolloClient(token)
16+
client
1717
.query({ query: CURRENT_USER })
1818
.then(res => setGithubCurrentUser(res.data.viewer))
1919
}, [token])

apps/projects/app/utils/apollo-client.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@ import { GITHUB_TOKEN_REVOKED } from '../store/eventTypes'
44
import { STATUS } from '../utils/github'
55

66
export const useApolloClient = () => {
7-
const { api } = useAragonApi()
8-
const initApolloClient = token => {
9-
if (token === null) return null
10-
return new ApolloClient({
11-
uri: 'https://api.github.com/graphql',
12-
request: operation => {
13-
if (token) {
14-
operation.setContext({
15-
headers: {
16-
accept: 'application/vnd.github.starfire-preview+json', // needed to create issues
17-
authorization: `bearer ${token}`,
18-
},
19-
})
20-
}
21-
},
22-
onError: error => {
23-
if (error.networkError && error.networkError.statusCode === 401) {
24-
api.emitTrigger(GITHUB_TOKEN_REVOKED, {
25-
status: STATUS.REVOKED,
26-
scope: null,
27-
token: null,
28-
})
29-
}
7+
const {
8+
api,
9+
appState: {
10+
github: { token } = { token: null }
11+
}
12+
} = useAragonApi()
13+
if (token === null) return null
14+
return new ApolloClient({
15+
uri: 'https://api.github.com/graphql',
16+
request: operation => {
17+
operation.setContext({
18+
headers: {
19+
accept: 'application/vnd.github.starfire-preview+json', // needed to create issues
20+
authorization: `bearer ${token}`,
21+
},
22+
})
23+
},
24+
onError: error => {
25+
if (error.networkError && error.networkError.statusCode === 401) {
26+
api.emitTrigger(GITHUB_TOKEN_REVOKED, {
27+
status: STATUS.REVOKED,
28+
scope: null,
29+
token: null,
30+
})
3031
}
31-
})
32-
}
33-
return initApolloClient
32+
else console.error(error)
33+
}
34+
})
3435
}

0 commit comments

Comments
 (0)