-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapiCallHandler.js
More file actions
109 lines (93 loc) · 2.78 KB
/
apiCallHandler.js
File metadata and controls
109 lines (93 loc) · 2.78 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
import axios from "axios";
import omit from "lodash/omit";
const host = process.env.REACT_APP_API_ENDPOINT;
const get = async (url, headers) => {
return await axios.get(url, headers);
};
const post = async (url, body, headers) => {
return await axios.post(url, body, headers);
};
const put = async (url, body, headers) => {
return await axios.put(url, body, headers);
};
const headers = (accessToken) => {
let headersHash;
if (accessToken) {
headersHash = { Accept: "application/json", Authorization: accessToken };
} else {
headersHash = { Accept: "application/json" };
}
return { headers: headersHash };
};
export const createOrUpdateProject = async (projectWithUserId, accessToken) => {
const project = omit(projectWithUserId, ["user_id"]);
if (!project.identifier) {
return await post(
`${host}/api/projects`,
{ project },
headers(accessToken),
);
} else {
return await put(
`${host}/api/projects/${project.identifier}`,
{ project },
headers(accessToken),
);
}
};
export const deleteProject = async (identifier, accessToken) => {
return await axios.delete(
`${host}/api/projects/${identifier}`,
headers(accessToken),
);
};
export const getImage = async (url) => {
return await get(url, headers());
};
export const createRemix = async (project, accessToken) => {
return await post(
`${host}/api/projects/${project.identifier}/remix`,
{ project },
headers(accessToken),
);
};
export const readProject = async (projectIdentifier, locale, accessToken) => {
const queryString = locale ? `?locale=${locale}` : "";
return await get(
`${host}/api/projects/${projectIdentifier}${queryString}`,
headers(accessToken),
);
};
export const loadAssets = async (assetsIdentifier, locale, accessToken) => {
const queryString = locale ? `?locale=${locale}` : "";
return await get(
`${host}/api/projects/${assetsIdentifier}/images${queryString}`,
headers(accessToken),
);
};
export const readProjectList = async (page, accessToken) => {
return await get(`${host}/api/projects`, {
params: { page },
...headers(accessToken),
});
};
export const uploadImages = async (projectIdentifier, accessToken, images) => {
var formData = new FormData();
images.forEach((image) => {
formData.append("images[]", image, image.name);
});
return await post(
`${host}/api/projects/${projectIdentifier}/images`,
formData,
{ ...headers(accessToken), "Content-Type": "multipart/form-data" },
);
};
export const createError = async (projectIdentifier, userId, error) => {
const { errorMessage, errorType } = error;
return await post(`${host}/api/project_errors`, {
error: errorMessage,
error_type: errorType,
project_id: projectIdentifier,
user_id: userId,
});
};