-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathReduxIt.js
94 lines (90 loc) · 2.59 KB
/
ReduxIt.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
85
86
87
88
89
90
91
92
93
94
import { applyMiddleware, createStore } from 'redux'
import logger from 'redux-logger'
import thunk from 'redux-thunk'
import { Shuffle } from './functions/core'
const initialState = {
backgrounds: [],
after: '',
background: undefined,
index: -1,
error: false,
fetching: undefined,
store: [],
categories: ['EarthPorn', 'SpacePorn', 'Wallpapers', 'ExposurePorn',
'SkyPorn', 'FractalPorn', 'ImaginaryTechnology',
'BridgePorn'],
refetch: false
}
export const reducer = (state = initialState, action) => {
switch (action.type) {
case 'FETCHING':
return {
...state,
fetching: true
}
case 'FETCH_ERROR':
return {
...state,
error: true
}
case 'FETCH_SUCCESS':
return {
...state,
fetching: false,
error: false,
backgrounds: action.payload,
after: action.payload.after
}
case 'NEXT_WALL':
let nextIndex = state.index + 1
let previousStore = state.store
let toCheck = state.backgrounds[state.index]
if (toCheck) previousStore.push(toCheck.data.url)
let toAdd = action.payload ? {
background: state.backgrounds[nextIndex].data.url,
store: previousStore
} : {}
return {
...state,
index: nextIndex,
fetching: true,
...toAdd
}
case 'PREVIOUS_WALL':
let store = state.store
let previousImage = state.backgrounds.length > 1 ? store.pop() : store[0]
return {
...state,
background: previousImage,
store: store
}
case 'STOP_FETCHING':
return {
...state,
fetching: false
}
case 'END_STUCK':
return {
...state,
fetching: true,
index: -1,
backgrounds: []
}
case 'UPDATE_CATEGORIES':
return {
...state,
categories: Shuffle(action.payload),
refetch: true
}
case 'END_REFETCH':
return {
...state,
refetch: false
}
default:
return state
}
}
const middleWear = applyMiddleware(thunk, logger)
const store = createStore(reducer, middleWear)
export default store