-
Notifications
You must be signed in to change notification settings - Fork 79
/
GalleryActions.js
250 lines (229 loc) · 5.66 KB
/
GalleryActions.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import GALLERY from './GalleryActionTypes';
/**
* Load the folder view for a given folder
* Used by AssetAdmin.
* @todo Refactor so that AssetAdmin has its own actions
*/
export function setFolder(folderId) {
return (dispatch) => {
dispatch({
type: GALLERY.SET_FOLDER,
payload: { ID: folderId },
});
// Always unset selected file, since it might not be in the current folder
dispatch({
type: GALLERY.SET_FILE,
payload: { fileId: null },
});
dispatch({
type: GALLERY.HIGHLIGHT_FILES,
payload: { ids: null },
});
};
}
/**
* Load the file view for a given folder
*/
export function setFile(fileId) {
return (dispatch) => {
dispatch({
type: GALLERY.HIGHLIGHT_FILES,
payload: { ids: [fileId] },
});
// Start message
dispatch({
type: GALLERY.SET_FILE,
payload: { fileId },
});
};
}
/**
* Adds files to state.
*
* @param array files - Array of file objects.
* @param number [count] - The number of files in the current view.
*/
export function addFiles(files, count) {
// TODO: Refactor this away - will be part of "load folder" and "search" actions
return (dispatch) =>
dispatch({
type: GALLERY.ADD_FILES,
payload: { files, count },
});
}
/**
* Deletes a number of items
*/
export function deleteItems(deleteApi, ids) {
return (dispatch) => {
dispatch({
type: GALLERY.DELETE_ITEM_REQUEST,
payload: { ids },
});
return deleteApi({ ids })
.then((json) => {
dispatch({
type: GALLERY.DELETE_ITEM_SUCCESS,
payload: { ids },
});
dispatch({
type: GALLERY.SET_FILE,
payload: { fileId: null },
});
return json;
})
.catch((error) => {
// Failure finish message
dispatch({
type: GALLERY.DELETE_ITEM_FAILURE,
payload: { error },
});
});
};
}
/**
* Load the contents of a folder from the API
*/
export function loadFolderContents(listApi, folderId, limit, page) {
return (dispatch) => {
dispatch({
type: GALLERY.LOAD_FOLDER_REQUEST,
payload: { viewingFolder: (folderId > 0), folderId: parseInt(folderId, 10) },
});
return listApi({ id: folderId, limit, page })
.then((data) => {
dispatch({
type: GALLERY.LOAD_FOLDER_SUCCESS,
payload: {
folderId: parseInt(folderId, 10),
parentfolderId: data.parent,
files: data.files,
canEdit: data.canEdit,
canDelete: data.canDelete,
},
});
});
};
}
/**
* Updates a file with new data.
*
* @param {function} updateApi
* @param number id - The id of the file to update.
* @param object updates - The new values.
*/
export function updateFile(updateApi, id, updates) {
return (dispatch) => {
dispatch({
type: GALLERY.UPDATE_FILE_REQUEST,
payload: { id, updates },
});
return updateApi(Object.assign({}, { id }, updates))
.then(() => {
dispatch({
type: GALLERY.UPDATE_FILE_SUCCESS,
payload: { id, updates },
});
});
};
}
/**
* Selects files. If no param is passed all files are selected.
*
* @param Array ids - Array of file ids to select.
*/
export function selectFiles(ids = null) {
return (dispatch) =>
dispatch({
type: GALLERY.SELECT_FILES,
payload: { ids },
});
}
/**
* Deselects files. If no param is passed all files are deselected.
*
* @param Array ids - Array of file ids to deselect.
*/
export function deselectFiles(ids = null) {
return (dispatch) =>
dispatch({
type: GALLERY.DESELECT_FILES,
payload: { ids },
});
}
/**
* Highlight files. If no param is passed all files are highlighted.
*
* @param Array ids - Array of file ids to select.
*/
export function highlightFiles(ids = null) {
return (dispatch) =>
dispatch({
type: GALLERY.HIGHLIGHT_FILES,
payload: { ids },
});
}
/**
* Sorts files in some order.
*
* @param func comparator - Used to determine the sort order.
*/
export function sortFiles(comparator) {
return (dispatch) =>
dispatch({
type: GALLERY.SORT_FILES,
payload: { comparator },
});
}
/**
* Create a new folder as a sub-folder of the current and open it for viewing.
* Triggers an asyncrhonous back-end requests and changes view after the request
* has completed.
*
* @param {Number} parentId
* @param {String} folderName
*/
export function createFolder(createFolderApi, parentId, name) {
return (dispatch) => {
// Start message
dispatch({
type: GALLERY.CREATE_FOLDER_REQUEST,
payload: { name },
});
return createFolderApi({ ParentID: isNaN(parentId) ? 0 : parentId, Name: name })
.then(json => {
dispatch({
type: GALLERY.CREATE_FOLDER_SUCCESS,
payload: { name },
});
dispatch({
type: GALLERY.SET_FOLDER,
payload: { ID: json.ID },
});
// TODO: Fix this so that the subsequent action is passed without a coupling to router
// here.
// - Successful action should triggers 'show files' view rather than triggering an action
// showFilesFromFolder(json.folderId);
})
.catch((err) => {
// Failure finish message
dispatch({
type: GALLERY.CREATE_FOLDER_FAILURE,
payload: { error: `Couldn\'t create ${name}: ${err}` },
});
});
};
}
/**
* Sets the permissions for the folder
*
* @param object folderPermissions Contains the canEdit, canDelete permissions as self-named keys
*/
export function setFolderPermissions(folderPermissions) {
return (dispatch) => {
dispatch({
type: GALLERY.SET_FOLDER_PERMISSIONS,
payload: folderPermissions,
});
};
}