fix: naming and redundancy issues

This commit is contained in:
Sérgio Salgado 2021-04-06 00:39:52 +01:00 committed by Josh Harvey
parent fff9523988
commit 40974dd501
15 changed files with 194 additions and 143 deletions

View file

@ -4,6 +4,7 @@ import { ApolloClient } from 'apollo-client'
import { ApolloLink } from 'apollo-link'
import { onError } from 'apollo-link-error'
import { HttpLink } from 'apollo-link-http'
import * as R from 'ramda'
import React, { useContext } from 'react'
import { useHistory, useLocation } from 'react-router-dom'
@ -18,10 +19,7 @@ const getClient = (history, location, setUserData) =>
onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.forEach(({ message, locations, path, extensions }) => {
if (extensions?.code === 'UNAUTHENTICATED') {
setUserData(null)
if (location.pathname !== '/login') history.push('/login')
}
handle(extensions?.code, history, location, setUserData)
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
)
@ -49,6 +47,23 @@ const getClient = (history, location, setUserData) =>
}
})
const handle = (type, ...args) => {
const handler = {
UNAUTHENTICATED: ({ history, location, setUserData }) => {
setUserData(null)
if (location.pathname !== '/login') history.push('/login')
},
INVALID_CREDENTIALS: () => {},
INVALID_TWO_FACTOR_CODE: () => {},
INVALID_URL_TOKEN: () => {},
USER_ALREADY_EXISTS: () => {}
}
if (!R.has(type, handler)) throw new Error('Unknown error code.')
return handler[type](...args)
}
const Provider = ({ children }) => {
const history = useHistory()
const location = useLocation()