Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/actions/__tests__/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('actions', () => {
const { dispatch, reducer } = store()
actions.saveCount({ value: 14 })(dispatch)
return eventually(() => {
expect(reducer.mock.calls[1][1].error.message)
expect(reducer.mock.calls[1][1].error)
.toEqual('something terrible happened')
})
})
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('actions', () => {
const { dispatch, reducer } = store()
actions.loadCount()(dispatch)
return eventually(() => {
expect(reducer.mock.calls[1][1].error.message)
expect(reducer.mock.calls[1][1].error)
.toEqual('something terrible happened')
})
})
Expand Down
31 changes: 15 additions & 16 deletions src/actions/action.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
type Q<T> = { request: T }
type S<T> = { response: T }
type E = { error: Error }
type E = { error: string }

type QEmpty = Q<{}>
type QValue = Q<{ value: number }>
type Value = { value: number }

type LoadCount =
({ type: 'LOAD_COUNT_REQUEST' } & QEmpty)
| ({ type: 'LOAD_COUNT_SUCCESS' } & QEmpty & S<{ value: number }>)
| ({ type: 'LOAD_COUNT_ERROR' } & QEmpty & E)
type LoadCountAction =
({ type: 'LOAD_COUNT_REQUEST' } & Q<{}>)
| ({ type: 'LOAD_COUNT_SUCCESS' } & Q<{}> & S<Value>)
| ({ type: 'LOAD_COUNT_ERROR' } & Q<{}> & E)

type SaveCount =
({ type: 'SAVE_COUNT_REQUEST' } & QValue)
| ({ type: 'SAVE_COUNT_SUCCESS' } & QValue & S<{}>)
| ({ type: 'SAVE_COUNT_ERROR' } & QValue & E)
type SaveCountAction =
({ type: 'SAVE_COUNT_REQUEST' } & Q<Value>)
| ({ type: 'SAVE_COUNT_SUCCESS' } & Q<Value> & S<{}>)
| ({ type: 'SAVE_COUNT_ERROR' } & Q<Value> & E)

export type Action =
LoadCount
| SaveCount
LoadCountAction
| SaveCountAction
| { type: 'INCREMENT_COUNTER', delta: number }
| { type: 'RESET_COUNTER' }

type _T = Action['type']

type APIActionGroup<TQ extends _T, TS extends _T, TE extends _T, _Q, _S> =
type ThunkActionGroup<TQ extends _T, TS extends _T, TE extends _T, _Q, _S> =
({ type: TQ } & Q<_Q>)
| ({ type: TS } & Q<_Q> & S<_S>)
| ({ type: TE } & Q<_Q> & E)
Expand All @@ -34,9 +33,9 @@ type Thunk<Q, S> = (request: Q) => Promise<S>
export const createThunkAction = <Q, S, TQ extends _T, TS extends _T, TE extends _T>
(fn: Thunk<Q, S>, tq: TQ, ts: TS, te: TE) =>
(request: Q) =>
(dispatch: Dispatch<APIActionGroup<TQ, TS, TE, Q, S>>) => {
(dispatch: Dispatch<ThunkActionGroup<TQ, TS, TE, Q, S>>) => {
dispatch({ type: tq, request })
fn(request)
.then(response => dispatch({ type: ts, request, response }))
.catch(error => dispatch({ type: te, request, error }))
.catch(err => dispatch({ type: te, request, error: err.message }))
}
2 changes: 1 addition & 1 deletion src/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function error (state: string = '', action: Action): string {
return ''
case 'LOAD_COUNT_ERROR':
case 'SAVE_COUNT_ERROR':
return action.error.toString()
return action.error
default:
return state
}
Expand Down