forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducer.js
194 lines (168 loc) · 4.67 KB
/
reducer.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { createReducer } from 'redux-act'
import * as actions from './actions'
/**
*
* @typedef {Object} Lists
* @property {number} id
* @property {name} string - Indicates name of the list.
* @property {boolean} isDeletable - Indicates if the list can be deleted.
* @property {boolean} isNestable - Indicates if list can be nested.
* @property {string[]} pages- Indicates pages contained in the list.
*/
// TODO: complete this.
/**
*
* @typedef {Object} ListState
* @property {number} activeListIndex
* @property {number} listFilterIndex
* @property {Lists[]} lists
* @property {string} urlDragged
* @property {boolean} showCommonNameWarning
* @property {boolean} showCreateListForm
*/
const defaultState = {
activeListIndex: -1,
listFilterIndex: null,
lists: [],
deleteConfirmProps: {
isShown: false,
id: undefined,
// Used to keep track of any particular result (use index)
deleting: undefined,
},
urlDragged: '',
showCreateListForm: false,
showCommonNameWarning: false,
showCrowdFundingModal: false,
}
const fetchAllLists = (state, lists) => ({
...state,
lists,
tempLists: lists,
})
const createList = (state, list) => ({
...state,
lists: [...state.lists, list],
})
const updateList = (state, { value, index }) => ({
...state,
lists: [
...state.lists.slice(0, index),
{
...state.lists[index],
name: value,
},
...state.lists.slice(index + 1),
],
})
const deleteList = (state, { id, index }) => ({
...state,
lists: [...state.lists.slice(0, index), ...state.lists.slice(index + 1)],
})
const addPageToList = (state, { url, index }) => {
const { lists } = state
const list = lists[index]
if (list.pages.includes(url)) {
return state
}
const newList = {
...list,
pages: [...list.pages, url],
}
return {
...state,
lists: [...lists.slice(0, index), newList, ...lists.slice(index + 1)],
}
}
const showDeleteConfirm = (state, { id, index }) => {
return {
...state,
deleteConfirmProps: {
...state.deleteConfirmProps,
isShown: true,
id,
deleting: index,
},
}
}
const payloadReducer = key => (state, payload) => ({ ...state, [key]: payload })
const toggleListFilterIndex = (state, index) => {
const { listFilterIndex } = state
if (listFilterIndex === null || listFilterIndex !== index) {
return {
...state,
listFilterIndex: index,
}
}
return {
...state,
listFilterIndex: null,
}
}
const setUrlDragged = (state, url) => {
return {
...state,
urlDragged: url,
}
}
const setShowCrowdFundingModal = (state, value) => {
return {
...state,
showCrowdFundingModal: value,
}
}
const resetUrlDragged = state => {
return {
...state,
urlDragged: '',
}
}
const openCreateListForm = state => ({
...state,
showCreateListForm: true,
})
const closeCreateListForm = state => ({
...state,
showCreateListForm: false,
})
const toggleCreateListForm = state => ({
...state,
showCreateListForm: !state.showCreateListForm,
})
const showCommonNameWarning = state => ({
...state,
showCommonNameWarning: true,
})
const removeCommonNameWarning = state => ({
...state,
showCommonNameWarning: false,
})
export default createReducer(
{
[actions.fetchAllLists]: fetchAllLists,
[actions.resetActiveListIndex]: state => ({
...state,
activeListIndex: defaultState.activeListIndex,
}),
[actions.setActiveListIndex]: payloadReducer('activeListIndex'),
[actions.createList]: createList,
[actions.updateListName]: updateList,
[actions.deleteList]: deleteList,
[actions.addPagetoList]: addPageToList,
[actions.showListDeleteModal]: showDeleteConfirm,
[actions.toggleListFilterIndex]: toggleListFilterIndex,
[actions.resetListDeleteModal]: state => ({
...state,
deleteConfirmProps: { ...defaultState.deleteConfirmProps },
}),
[actions.setUrlDragged]: setUrlDragged,
[actions.resetUrlDragged]: resetUrlDragged,
[actions.openCreateListForm]: openCreateListForm,
[actions.closeCreateListForm]: closeCreateListForm,
[actions.toggleCreateListForm]: toggleCreateListForm,
[actions.showCommonNameWarning]: showCommonNameWarning,
[actions.removeCommonNameWarning]: removeCommonNameWarning,
[actions.setShowCrowdFundingModal]: setShowCrowdFundingModal,
},
defaultState,
)