-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducers.js
84 lines (78 loc) · 1.88 KB
/
reducers.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { combineReducers } from 'redux'
import * as types from './types'
// INITIAL USER STATE
const initialUserState = {
items: [],
error: null,
loading: false
}
// COUNTER REDUCER
const userReducer = (state = initialUserState, { type, payload }) => {
switch (type) {
case types.SEARCH_USERS:
return {
...state,
loading: true
}
case types.SEARCH_USERS_SUCCESS:
return {
items: payload.page>1?[...state.items, ...payload.results]: payload.results,
error: null,
loading: false
}
case types.SEARCH_USERS_FAIL:
return {
items:[],
error: payload.error,
loading: false
}
case types.USERS_EMPTY:
return {
...state,
items:[]
}
default:
return state
}
}
// INITIAL REPO STATE
const initialRepoState = {
items: [],
error: null,
loading: false
}
// REPO REDUCER
const repoReducer = (state = initialRepoState, { type, payload }) => {
switch (type) {
case types.SEARCH_REPOS:
return {
...state,
loading: true
}
case types.SEARCH_REPOS_SUCCESS:
return {
items: payload.page>1?[...state.items, ...payload.results]: payload.results,
error: null,
loading: false
}
case types.SEARCH_REPOS_FAIL:
return {
items:[],
error: payload.error,
loading: false
}
case types.REPOS_EMPTY:
return {
...state,
items:[]
}
default:
return state
}
}
// COMBINED REDUCERS
const reducers = {
users: userReducer,
repos: repoReducer,
}
export default combineReducers(reducers)