Simplify CRUDReducer methods
This commit is contained in:
parent
580fb368e6
commit
2f17418753
@ -67,15 +67,16 @@ export class CRUDReducer<
|
|||||||
|
|
||||||
handleRejected = (
|
handleRejected = (
|
||||||
state: ICRUDState<T>,
|
state: ICRUDState<T>,
|
||||||
action: Filter<ICRUDAction<T, ActionType>, {status: 'rejected'}>,
|
method: ICRUDMethod,
|
||||||
|
error: Error,
|
||||||
): ICRUDState<T> => {
|
): ICRUDState<T> => {
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
status: {
|
status: {
|
||||||
...state.status,
|
...state.status,
|
||||||
[action.method]: {
|
[method]: {
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
error: action.payload.message,
|
error: error.message,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -83,13 +84,13 @@ export class CRUDReducer<
|
|||||||
|
|
||||||
handleLoading = (
|
handleLoading = (
|
||||||
state: ICRUDState<T>,
|
state: ICRUDState<T>,
|
||||||
action: Filter<ICRUDAction<T, ActionType>, {status: 'pending'}>,
|
method: ICRUDMethod,
|
||||||
): ICRUDState<T> => {
|
): ICRUDState<T> => {
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
status: {
|
status: {
|
||||||
...state.status,
|
...state.status,
|
||||||
[action.method]: {
|
[method]: {
|
||||||
isLoading: true,
|
isLoading: true,
|
||||||
error: '',
|
error: '',
|
||||||
},
|
},
|
||||||
@ -97,12 +98,7 @@ export class CRUDReducer<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleFindOne = (
|
handleFindOne = (state: ICRUDState<T>, payload: T): ICRUDState<T> => {
|
||||||
state: ICRUDState<T>,
|
|
||||||
action: Filter<
|
|
||||||
ICRUDAction<T, ActionType>, {method: 'findOne', status: 'resolved'}>,
|
|
||||||
): ICRUDState<T> => {
|
|
||||||
const {payload} = action
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
ids: [...state.ids, payload.id],
|
ids: [...state.ids, payload.id],
|
||||||
@ -111,17 +107,12 @@ export class CRUDReducer<
|
|||||||
},
|
},
|
||||||
status: {
|
status: {
|
||||||
...state.status,
|
...state.status,
|
||||||
[action.method]: this.getSuccessStatus(),
|
findOne: this.getSuccessStatus(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSave = (
|
handleSave = (state: ICRUDState<T>, payload: T): ICRUDState<T> => {
|
||||||
state: ICRUDState<T>,
|
|
||||||
action: Filter<
|
|
||||||
ICRUDAction<T, ActionType>, {method: 'save', status: 'resolved'}>,
|
|
||||||
): ICRUDState<T> => {
|
|
||||||
const {payload} = action
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
ids: [...state.ids, payload.id],
|
ids: [...state.ids, payload.id],
|
||||||
@ -130,17 +121,12 @@ export class CRUDReducer<
|
|||||||
},
|
},
|
||||||
status: {
|
status: {
|
||||||
...state.status,
|
...state.status,
|
||||||
[action.method]: this.getSuccessStatus(),
|
save: this.getSuccessStatus(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleUpdate = (
|
handleUpdate = (state: ICRUDState<T>, payload: T): ICRUDState<T> => {
|
||||||
state: ICRUDState<T>,
|
|
||||||
action: Filter<
|
|
||||||
ICRUDAction<T, ActionType>, {method: 'update', status: 'resolved'}>,
|
|
||||||
): ICRUDState<T> => {
|
|
||||||
const {payload} = action
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
byId: {
|
byId: {
|
||||||
@ -148,41 +134,31 @@ export class CRUDReducer<
|
|||||||
},
|
},
|
||||||
status: {
|
status: {
|
||||||
...state.status,
|
...state.status,
|
||||||
[action.method]: this.getSuccessStatus(),
|
update: this.getSuccessStatus(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleRemove = (
|
handleRemove = (state: ICRUDState<T>, payload: T): ICRUDState<T> => {
|
||||||
state: ICRUDState<T>,
|
|
||||||
action: Filter<
|
|
||||||
ICRUDAction<T, ActionType>, {method: 'remove', status: 'resolved'}>,
|
|
||||||
): ICRUDState<T> => {
|
|
||||||
const {payload} = action
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
ids: state.ids.filter(id => id !== payload.id),
|
ids: state.ids.filter(id => id !== payload.id),
|
||||||
byId: without(state.byId, payload.id),
|
byId: without(state.byId, payload.id),
|
||||||
status: {
|
status: {
|
||||||
...state.status,
|
...state.status,
|
||||||
[action.method]: this.getSuccessStatus(),
|
remove: this.getSuccessStatus(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleFindMany = (
|
handleFindMany = (state: ICRUDState<T>, payload: T[]): ICRUDState<T> => {
|
||||||
state: ICRUDState<T>,
|
|
||||||
action: Filter<
|
|
||||||
ICRUDAction<T, ActionType>, {method: 'findMany', status: 'resolved'}>,
|
|
||||||
): ICRUDState<T> => {
|
|
||||||
const {payload} = action
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
ids: payload.map(item => item.id),
|
ids: payload.map(item => item.id),
|
||||||
byId: indexBy(payload, 'id' as any),
|
byId: indexBy(payload, 'id' as any),
|
||||||
status: {
|
status: {
|
||||||
...state.status,
|
...state.status,
|
||||||
[action.method]: this.getSuccessStatus(),
|
findMany: this.getSuccessStatus(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -200,21 +176,21 @@ export class CRUDReducer<
|
|||||||
|
|
||||||
switch (action.status) {
|
switch (action.status) {
|
||||||
case 'pending':
|
case 'pending':
|
||||||
return this.handleLoading(state, action)
|
return this.handleLoading(state, action.method)
|
||||||
case 'rejected':
|
case 'rejected':
|
||||||
return this.handleRejected(state, action)
|
return this.handleRejected(state, action.method, action.payload)
|
||||||
case 'resolved':
|
case 'resolved':
|
||||||
switch (action.method) {
|
switch (action.method) {
|
||||||
case 'save':
|
case 'save':
|
||||||
return this.handleSave(state, action)
|
return this.handleSave(state, action.payload)
|
||||||
case 'update':
|
case 'update':
|
||||||
return this.handleUpdate(state, action)
|
return this.handleUpdate(state, action.payload)
|
||||||
case 'remove':
|
case 'remove':
|
||||||
return this.handleRemove(state, action)
|
return this.handleRemove(state, action.payload)
|
||||||
case 'findOne':
|
case 'findOne':
|
||||||
return this.handleFindOne(state, action)
|
return this.handleFindOne(state, action.payload)
|
||||||
case 'findMany':
|
case 'findMany':
|
||||||
return this.handleFindMany(state, action)
|
return this.handleFindMany(state, action.payload)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return state
|
return state
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user