Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add reducer for errors #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kea-loaders",
"version": "3.0.0",
"version": "3.0.1",
"description": "Action Listener side-effects for Kea",
"author": "Marius Andra",
"license": "MIT",
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test('loaders work', async () => {
const unmount = logic.mount()

expect(logic.values.users).toBe(null)
expect(Object.keys(logic.values)).toEqual(['users', 'usersLoading'])
expect(Object.keys(logic.values)).toEqual(['users', 'usersLoading', 'usersError'])
expect(Object.keys(logic.actions).sort()).toEqual([
'loadUsersAsync',
'loadUsersAsyncFailure',
Expand Down Expand Up @@ -215,6 +215,7 @@ test('throwing calls failure', async () => {

logic.actions.loadUsersSync()
expect(logic.values.users).toEqual(null)
expect(logic.values.usersError).toEqual(Error('sync nope'))
expect(syncListenerRan).toBe('sync nope')

expect(errorList).toEqual(['sync nope'])
Expand All @@ -223,6 +224,7 @@ test('throwing calls failure', async () => {
await delay(10)

expect(logic.values.users).toEqual(null)
expect(logic.values.usersError).toEqual(Error('async nope'))
expect(asyncListenerRan).toBe('async nope')
expect(errorList).toEqual(['sync nope', 'async nope'])

Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,15 @@ export function loaders<L extends Logic = Logic>(
const newReducers: Record<string, [any, any] | any> = {}
const reducerObject: Record<string, (state: any, payload: any) => any> = {}
const reducerLoadingObject: Record<string, () => any> = {}
const reducerErrorObject: Record<string, (state: any, payload: any) => any> = {}
Object.keys(loaderActions).forEach((actionKey) => {
reducerObject[`${actionKey}Success`] = (_, { [reducerKey]: value }) => value
reducerLoadingObject[`${actionKey}`] = () => true
reducerLoadingObject[`${actionKey}Success`] = () => false
reducerLoadingObject[`${actionKey}Failure`] = () => false
reducerErrorObject[`${actionKey}`] = () => null
reducerErrorObject[`${actionKey}Success`] = () => null
reducerErrorObject[`${actionKey}Failure`] = (_, { errorObject }) => errorObject
})
if (typeof logic.reducers[reducerKey] === 'undefined') {
newReducers[reducerKey] = [defaultValue, reducerObject]
Expand All @@ -134,6 +138,9 @@ export function loaders<L extends Logic = Logic>(
if (typeof logic.reducers[`${reducerKey}Loading`] === 'undefined') {
newReducers[`${reducerKey}Loading`] = [false, reducerLoadingObject]
}
if (typeof logic.reducers[`${reducerKey}Error`] === 'undefined') {
newReducers[`${reducerKey}Error`] = [false, reducerErrorObject]
}

const newListeners: Record<string, ListenerFunction> = {}
Object.entries(loaderActions).forEach(([actionKey, listener]) => {
Expand Down