From 2f17418753e3be84fed68ff9185ca49ad92585e6 Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Tue, 2 Apr 2019 12:03:38 +0800 Subject: [PATCH] Simplify CRUDReducer methods --- packages/client/src/crud/CRUDReducer.ts | 70 ++++++++----------------- 1 file changed, 23 insertions(+), 47 deletions(-) diff --git a/packages/client/src/crud/CRUDReducer.ts b/packages/client/src/crud/CRUDReducer.ts index afb677c..f9feec0 100644 --- a/packages/client/src/crud/CRUDReducer.ts +++ b/packages/client/src/crud/CRUDReducer.ts @@ -67,15 +67,16 @@ export class CRUDReducer< handleRejected = ( state: ICRUDState, - action: Filter, {status: 'rejected'}>, + method: ICRUDMethod, + error: Error, ): ICRUDState => { return { ...state, status: { ...state.status, - [action.method]: { + [method]: { isLoading: false, - error: action.payload.message, + error: error.message, }, }, } @@ -83,13 +84,13 @@ export class CRUDReducer< handleLoading = ( state: ICRUDState, - action: Filter, {status: 'pending'}>, + method: ICRUDMethod, ): ICRUDState => { return { ...state, status: { ...state.status, - [action.method]: { + [method]: { isLoading: true, error: '', }, @@ -97,12 +98,7 @@ export class CRUDReducer< } } - handleFindOne = ( - state: ICRUDState, - action: Filter< - ICRUDAction, {method: 'findOne', status: 'resolved'}>, - ): ICRUDState => { - const {payload} = action + handleFindOne = (state: ICRUDState, payload: T): ICRUDState => { return { ...state, ids: [...state.ids, payload.id], @@ -111,17 +107,12 @@ export class CRUDReducer< }, status: { ...state.status, - [action.method]: this.getSuccessStatus(), + findOne: this.getSuccessStatus(), }, } } - handleSave = ( - state: ICRUDState, - action: Filter< - ICRUDAction, {method: 'save', status: 'resolved'}>, - ): ICRUDState => { - const {payload} = action + handleSave = (state: ICRUDState, payload: T): ICRUDState => { return { ...state, ids: [...state.ids, payload.id], @@ -130,17 +121,12 @@ export class CRUDReducer< }, status: { ...state.status, - [action.method]: this.getSuccessStatus(), + save: this.getSuccessStatus(), }, } } - handleUpdate = ( - state: ICRUDState, - action: Filter< - ICRUDAction, {method: 'update', status: 'resolved'}>, - ): ICRUDState => { - const {payload} = action + handleUpdate = (state: ICRUDState, payload: T): ICRUDState => { return { ...state, byId: { @@ -148,41 +134,31 @@ export class CRUDReducer< }, status: { ...state.status, - [action.method]: this.getSuccessStatus(), + update: this.getSuccessStatus(), }, } } - handleRemove = ( - state: ICRUDState, - action: Filter< - ICRUDAction, {method: 'remove', status: 'resolved'}>, - ): ICRUDState => { - const {payload} = action + handleRemove = (state: ICRUDState, payload: T): ICRUDState => { return { ...state, ids: state.ids.filter(id => id !== payload.id), byId: without(state.byId, payload.id), status: { ...state.status, - [action.method]: this.getSuccessStatus(), + remove: this.getSuccessStatus(), }, } } - handleFindMany = ( - state: ICRUDState, - action: Filter< - ICRUDAction, {method: 'findMany', status: 'resolved'}>, - ): ICRUDState => { - const {payload} = action + handleFindMany = (state: ICRUDState, payload: T[]): ICRUDState => { return { ...state, ids: payload.map(item => item.id), byId: indexBy(payload, 'id' as any), status: { ...state.status, - [action.method]: this.getSuccessStatus(), + findMany: this.getSuccessStatus(), }, } } @@ -200,21 +176,21 @@ export class CRUDReducer< switch (action.status) { case 'pending': - return this.handleLoading(state, action) + return this.handleLoading(state, action.method) case 'rejected': - return this.handleRejected(state, action) + return this.handleRejected(state, action.method, action.payload) case 'resolved': switch (action.method) { case 'save': - return this.handleSave(state, action) + return this.handleSave(state, action.payload) case 'update': - return this.handleUpdate(state, action) + return this.handleUpdate(state, action.payload) case 'remove': - return this.handleRemove(state, action) + return this.handleRemove(state, action.payload) case 'findOne': - return this.handleFindOne(state, action) + return this.handleFindOne(state, action.payload) case 'findMany': - return this.handleFindMany(state, action) + return this.handleFindMany(state, action.payload) } default: return state