-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatchResolver.js
More file actions
33 lines (30 loc) · 1.08 KB
/
Copy pathdispatchResolver.js
File metadata and controls
33 lines (30 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { errors } from './utils'
const getActionType = (schemaResolver, entity, action) => {
if (typeof schemaResolver === 'function') {
return schemaResolver(entity, action)
}
else if (typeof schemaResolver === 'object') {
if (typeof schemaResolver[entity] === 'string') {
return schemaResolver[entity]
} else if (typeof schemaResolver[entity] === 'function') {
return schemaResolver[entity](action)
}
}
console.error(errors.incorrectMapType())
}
const defaultDispatchResolver = (store, action, normalizedDataArray, schemaResolver) =>
normalizedDataArray.map(normalizedData => {
const entities = Object.entries(normalizedData.entities);
if (!Array.isArray(entities)) {
console.error(errors.dispatchResolverExpectsArray())
return
}
return entities.forEach(([entity, data]) => {
const actionType = getActionType(schemaResolver, entity, action)
return actionType ? store.dispatch({
type: actionType,
payload: data
}) : console.error(errors.noActionType())
})
})
export default defaultDispatchResolver;