-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcollectionActions.js
More file actions
159 lines (146 loc) · 5.23 KB
/
Copy pathcollectionActions.js
File metadata and controls
159 lines (146 loc) · 5.23 KB
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
import * as types from "../constants/ActionTypes";
export const collectRef = "/apiv1/collections";
/**
* Fetch the list of the user collection asynchronously
* Use when user login or added a new collection
*
* @param {*} uid A JWT token to authenticate with the backend
*/
export function asyncCollections(uid) {
// fetch user's collections
return (dispatch) => {
if (uid) {
let userCollections = [];
fetch(collectRef, {headers: {"x-access-token": uid}}).then((data) => {
if(data.status === 204){
return;
}
data.json().then((data) => {
data.forEach((doc) => {
userCollections.push(doc);
});
});
dispatch(syncCollections(userCollections));
});
}
};
}
/**
* Sends a signal to the reducer to sync the user collections
*
* @param {object} payload List of user collections
*
* @returns {object} reducer action obj with type SYNC_COLLECTIONS with payload
*/
export function syncCollections(payload) {
return { type: types.SYNC_COLLECTIONS, payload: payload };
}
/**
* Fetch the specific collection specify by user
*
* @param {string} collectionID Collection id
* @param {*} uid A JWT token to authenticate with the backend
*/
export function asyncCollection(collectionID, uid) {
// fetch projects in collection
return (dispatch) => {
if (collectionID) {
let collectionProjects = [];
let projectOptions = [];
fetch(`${collectRef}/collectionID/${collectionID}`, {headers: {"x-access-token": uid}})
.then((resp) => {
switch(resp.status){
case 200:
document.title = collectionID + " Collection | MYR";
resp.json().then((data) => {
data.forEach((doc) => {
collectionProjects.push(doc);
});
collectionProjects.map((proj) => {
return projectOptions.push({
value: proj._id,
label: proj.name
});
});
dispatch(syncCollection(projectOptions));
});
break;
case 401:
window.alert("Error: You are not logged in as the owner of this collection");
break;
case 404:
window.location.assign("/error-404");
break;
default:
window.alert(`Error fetching collection scenes: ${resp.statusText}`);
}
});
}
};
}
/**
* Sends a signal to the reducer to load the retrieved collection
*
* @param {object} payload Data of retrieved collection
*
* @returns {object} reducer action obj with type: SYNC_COLLECTION and payload
*/
export function syncCollection(payload) {
return { type: types.SYNC_COLLECTION, payload: payload };
}
/**
* Sends a signal to the reducer to delete the specific collection of user
*
* @param {string} collectionID Collection ID
* @param {string} name Name of the collection if exists
* @param {*} uid A JWT token to authenticate with the backend
*/
export function deleteCollection(collectionID, name = null, uid) {
return (dispatch) => {
name = (name ? name : collectionID);
if (window.confirm(`Are you sure you want to delete collection "${name}"?`)) {
// Delete Document
fetch(`${collectRef}/collectionID/${name}`, {method: "DELETE", headers: { "x-access-token": uid}}).then((resp) => {
if(resp.status !== 204) {
console.error(`Error deleting collection ${name}: ${resp.statusText}`);
return;
}
dispatch({ type: types.DELETE_COLLECTION, id: collectionID });
});
}
};
}
/**
* Creates a new collection
*
* @param {string} name The name of the collection to be created
* @param {*} uid A JWT token to authenticate with the backend
*/
export async function createCollection(name, uid) {
name = name.toLowerCase().trim();
let resp = await fetch(`${collectRef}/`, {
method: "POST",
body: JSON.stringify({collectID: name}),
headers:{"Content-Type": "application/json", "x-access-token": uid}
});
if(resp.status === 409){
window.alert("Error: A collection already exists with that collection name.");
return false;
}else if (resp.status !== 201) {
window.alert(`Error creating collection: ${resp.statusText}`);
return false;
}else{
asyncCollections(uid);
window.alert("Collection added!");
return true;
}
}
export default {
asyncCollection,
asyncCollections,
deleteCollection,
syncCollection,
syncCollections,
createCollection,
collectRef
};