forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.js
179 lines (159 loc) · 4.89 KB
/
actions.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
import { createAction } from 'redux-act'
import { remoteFunction } from 'src/util/webextensionRPC'
import * as selectors from './selectors'
import { selectors as filters } from 'src/search-filters'
import analytics from 'src/analytics'
export const fetchAllLists = createAction('custom-lists/listData')
export const createList = createAction('custom-lists/addList')
export const deleteList = createAction(
'custom-lists/deleteList',
(id, index) => ({
id,
index,
}),
)
export const updateListName = createAction(
'custom-lists/updateListName',
(value, index) => ({
value,
index,
}),
)
export const addPagetoList = createAction(
'custom-lists/addPagetoList',
(url, index) => ({
url,
index,
}),
)
export const hidePageFromList = createAction('custom-lists/hidePageFromList')
export const showListDeleteModal = createAction(
'custom-lists/showListDeleteModal',
(id, index) => ({
id,
index,
}),
)
export const resetListDeleteModal = createAction(
'custom-lists/resetListDeleteModal',
)
export const resetActiveListIndex = createAction(
'custom-lists/resetActiveListIndex',
)
export const setActiveListIndex = createAction(
'custom-lists/setActiveListIndex',
)
export const setShowCrowdFundingModal = createAction(
'custom-lists/setShowCrowdFundingModal',
)
export const toggleListFilterIndex = createAction(
'custom-lists/toggleListFilterIndex',
)
export const setUrlDragged = createAction(
'custom-lists/setUrlDragged',
url => url,
)
export const resetUrlDragged = createAction('custom-lists/resetUrlDragged')
export const closeCreateListForm = createAction(
'custom-lists/closeCreateListForm',
)
export const openCreateListForm = createAction(
'custom-lists/openCreateListForm',
)
export const toggleCreateListForm = createAction(
'custom-lists/toggleCreateListForm',
)
export const showCommonNameWarning = createAction(
'custom-lists/showCommonNameWarning',
)
export const removeCommonNameWarning = createAction(
'custom-lists/removeCommonNameWarning',
)
export const showEditBox = index => (dispatch, getState) => {
const activeListIndex = selectors.activeListIndex(getState())
if (activeListIndex === index) {
dispatch(resetActiveListIndex())
} else {
dispatch(setActiveListIndex(index))
}
}
export const delPageFromList = url => async (dispatch, getState) => {
try {
// const lists = await remoteFunction('fetchAllLists')()
const index = selectors.listFilterIndex(getState())
const listId = filters.listFilter(getState())
await remoteFunction('removePageFromList')({
id: Number(listId),
url,
})
dispatch(hidePageFromList(url, index))
} catch (err) {
console.error(err)
}
}
export const getListFromDB = () => async (dispatch, getState) => {
try {
const lists = await remoteFunction('fetchAllLists')({ limit: 1000 })
dispatch(fetchAllLists(lists || []))
} catch (err) {
console.error(err)
}
}
export const createPageList = (name, cb) => async (dispatch, getState) => {
// gets id from DB after it is added
try {
analytics.trackEvent({ category: 'Collections', action: 'create' })
// Create List
const listExist = Boolean(
await remoteFunction('fetchListIgnoreCase')({ name }),
)
if (!listExist) {
// Gets the id of the created list for future reference
const id = await remoteFunction('createCustomList')({ name })
const list = {
id,
name,
isDeletable: true,
pages: [],
}
dispatch(createList(list))
dispatch(closeCreateListForm())
dispatch(removeCommonNameWarning())
cb()
} else {
dispatch(showCommonNameWarning())
}
} catch (err) {
console.error(err)
}
}
export const updateList = (index, name, id) => async (dispatch, getState) => {
dispatch(resetActiveListIndex())
try {
await remoteFunction('updateListName')({ id, name })
dispatch(updateListName(name, index))
} catch (err) {
console.error(err)
}
}
export const deletePageList = () => async (dispatch, getState) => {
const { id, deleting } = selectors.deleteConfirmProps(getState())
try {
// DB call to remove List by ID.
await remoteFunction('removeList')({ id })
} catch (err) {
console.error(err)
} finally {
dispatch(deleteList(id, deleting))
dispatch(resetListDeleteModal())
}
}
export const addUrltoList = (url, index, id) => async (dispatch, getState) => {
try {
await remoteFunction('insertPageToList')({ id, url })
} catch (err) {
console.error(err)
} finally {
dispatch(addPagetoList(url, index))
}
}