Skip to content

Commit

Permalink
fix(api error): catch failed to fetch errors and let the user know
Browse files Browse the repository at this point in the history
- refactors `apiActionTypes` and `isApiAction` to `utils/actionTypes` file
- fix all imports
- moved `apiConnection` to new `Connection` store
- add `failedToFetchCount` to `Connection` store
- remove `setApiConnection` func
- add `Connection` reducer
- add `FAILED_TO_FETCH` action type to `Connection` reducer
- test for failed to fetch errors in `apiMiddleware`, send action with type `FAILED_TO_FETCH`
- catch `FAILED_TO_FETCH` actions in `Connection` reducer, increment `failedToFetchCount` each time. Set `apiConnection` to -1 after 15 tries
- catch all api success actions in the `Connection` reducer. On api success, set `apiConnection` to 1 and reset `failedToFetchCount`
  • Loading branch information
ramfox authored and chriswhong committed Sep 24, 2019
1 parent e7c3b88 commit 57011a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
9 changes: 3 additions & 6 deletions app/reducers/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import { getActionType, isApiAction } from '../utils/actionType'
export const FAILED_TO_FETCH = 'FAILED_TO_FETCH'
export const SET_API_CONNECTION = 'SET_API_CONNECTION'

export const initialState = {
const initialState = {
apiConnection: 0,
failedToFetchCount: 0
}

export const maxFailedFetches = 14

export default (state = initialState, action: AnyAction) => {
// if an api action succeeds, we are no longer in an
// unconnected state!
if (isApiAction(action) && getActionType(action) === 'success') {
if (isApiAction(action.type) && getActionType(action) === 'success') {
if (state.apiConnection !== 1 || state.failedToFetchCount > 0) {
return {
apiConnection: 1,
Expand All @@ -25,8 +23,7 @@ export default (state = initialState, action: AnyAction) => {
}
switch (action.type) {
case FAILED_TO_FETCH:
if (state.apiConnection === -1) return state
if (state.failedToFetchCount >= maxFailedFetches) {
if (state.failedToFetchCount >= 14) {
return {
failedToFetchCount: 0,
apiConnection: -1
Expand Down
18 changes: 9 additions & 9 deletions app/utils/actionType.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export const getActionType = (action = { type: '' }): string => {
if (action.type.includes('REQUEST')) return 'request'
if (action.type.includes('SUCCESS')) return 'success'
if (action.type.includes('FAILURE')) return 'failure'
return ''
}

export function apiActionTypes (endpoint: string): [string, string, string] {
const name = endpoint.toUpperCase()
return [`API_${name}_REQUEST`, `API_${name}_SUCCESS`, `API_${name}_FAILURE`]
}

export const getActionType = (action = { type: '' }): string => {
if (action.type.endsWith('REQUEST')) return 'request'
if (action.type.endsWith('SUCCESS')) return 'success'
if (action.type.endsWith('FAILURE')) return 'failure'
return ''
}

export function isApiAction (action = { type: '' }): boolean {
return !!action.type && action.type.startsWith('API')
export function isApiAction (endpoint: string): boolean {
return endpoint.includes('API')
}

0 comments on commit 57011a4

Please sign in to comment.