This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
IpfsNodeRedux.js
138 lines (110 loc) · 4.23 KB
/
IpfsNodeRedux.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {AppState} from 'react-native'
import { createReducer, createActions } from 'reduxsauce'
import Immutable from 'seamless-immutable'
import Config from 'react-native-config'
/* ------------- Types and Action Creators ------------- */
const { Types, Creators } = createActions({
lock: ['value'],
appStateChange: ['previousState', 'newState'],
createNodeRequest: ['path'],
createNodeSuccess: null,
createNodeFailure: ['error'],
startNodeRequest: null,
startNodeSuccess: null,
startNodeFailure: ['error'],
stopNodeRequest: null,
stopNodeSuccess: null,
stopNodeFailure: ['error'],
nodeOnline: null,
getPhotoHashesRequest: ['thread'],
getPhotoHashesSuccess: ['thread', 'items'],
getPhotoHashesFailure: ['thread', 'error']
})
export const IpfsNodeTypes = Types
export default Creators
/* ------------- Initial State ------------- */
export const INITIAL_STATE = Immutable({
locked: false,
appState: 'default',
nodeState: {
state: 'undefined', // | creating | stopped | starting | started | stopping
online: false,
error: null
},
threads: {
default: {
querying: false,
items: [],
error: null
},
[Config.ALL_THREAD_NAME]: {
querying: false,
items: [],
error: null
}
}
})
/* ------------- Selectors ------------- */
export const IpfsNodeSelectors = {
locked: (state) => state.ipfs.locked,
appState: (state) => state.ipfs.appState
}
/* ------------- Reducers ------------- */
export const lock = (state, {value}) =>
state.merge({...state, locked: value})
export const newAppState = (state, {newState}) =>
state.merge({...state, appState: newState})
export const creatingNode = state =>
state.merge({...state, nodeState: {state: 'creating', error: null}})
export const nodeCreated = state =>
state.merge({...state, nodeState: {state: 'stopped', error: null}})
export const nodeStarting = state =>
state.merge({...state, nodeState: {state: 'starting', error: null}})
export const nodeStarted = state =>
state.merge({...state, nodeState: {state: 'started', error: null}})
export const nodeStopping = state =>
state.merge({...state, nodeState: {state: 'stopping', error: null}})
export const nodeStopped = state =>
state.merge({...state, nodeState: {state: 'stopped', error: null}})
export const nodeError = (state, {error}) =>
state.merge({...state, nodeState: {...state.nodeState, error: error}})
export const nodeOnline = state =>
state.merge({...state, nodeState: {...state.nodeState, online: true}})
export const photoHashesRequest = (state, {thread}) => {
const currentThreadState = state.threads[thread]
const newThreadState = currentThreadState.merge({querying: true})
const newThreads = state.threads.set(thread, newThreadState)
return state.merge({...state, threads: newThreads})
}
export const photoHashesSuccess = (state, {thread, items}) => {
const currentThreadState = state.threads[thread]
const newThreadState = currentThreadState.merge({querying: false, items})
const newThreads = state.threads.set(thread, newThreadState)
return state.merge({...state, threads: newThreads})
}
export const photoHashesFailure = (state, {thread, error}) => {
const currentThreadState = state.threads[thread]
const newThreadState = currentThreadState.merge({querying: false, error})
const newThreads = state.threads.set(thread, newThreadState)
return state.merge({...state, threads: newThreads})
}
// Helper so sagas can figure out current items loaded
// const getItems = state => state.items
/* ------------- Hookup Reducers To Types ------------- */
export const reducer = createReducer(INITIAL_STATE, {
[Types.LOCK]: lock,
[Types.APP_STATE_CHANGE]: newAppState,
[Types.CREATE_NODE_REQUEST]: creatingNode,
[Types.CREATE_NODE_SUCCESS]: nodeCreated,
[Types.CREATE_NODE_FAILURE]: nodeError,
[Types.START_NODE_REQUEST]: nodeStarting,
[Types.START_NODE_SUCCESS]: nodeStarted,
[Types.START_NODE_FAILURE]: nodeError,
[Types.STOP_NODE_REQUEST]: nodeStopping,
[Types.STOP_NODE_SUCCESS]: nodeStopped,
[Types.STOP_NODE_FAILURE]: nodeError,
[Types.NODE_ONLINE]: nodeOnline,
[Types.GET_PHOTO_HASHES_REQUEST]: photoHashesRequest,
[Types.GET_PHOTO_HASHES_SUCCESS]: photoHashesSuccess,
[Types.GET_PHOTO_HASHES_FAILURE]: photoHashesFailure
})