-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathvotings.ts
More file actions
50 lines (43 loc) · 1.58 KB
/
Copy pathvotings.ts
File metadata and controls
50 lines (43 loc) · 1.58 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
import { CreateVotingRequest, VotingStatus } from "store/features/votings/types";
import { SERVER_HTTP_URL } from "../config";
export const VotingAPI = {
/**
* Adds a vote configuration to a board.
*
* @param voting the current vote configuration
*
* @returns `true` if the operation succeeded or throws an error otherwise
*/
createVoting: async (board: string, voting: CreateVotingRequest) => {
try {
const response = await fetch(`${SERVER_HTTP_URL}/boards/${board}/votings`, {
method: "POST",
credentials: "include",
body: JSON.stringify(voting),
});
if (response.status === 201) {
return await response.json();
}
throw new Error(`create voting request resulted in response with status ${response.status}`);
} catch (error) {
throw new Error(`unable to create voting`, { cause: error });
}
},
changeVotingStatus: async (board: string, voting: string, status?: VotingStatus) => {
try {
const options: RequestInit = {
method: "PUT",
credentials: "include",
body: JSON.stringify({ status: status }),
headers: { "Content-Type": "application/json" },
};
const response = await fetch(`${SERVER_HTTP_URL}/boards/${board}/votings/${voting}`, options);
if (response.status === 200) {
return await response.json();
}
throw new Error(`change voting status request resulted in response with status ${response.status}`);
} catch (error) {
throw new Error(`unable to change voting status`, { cause: error });
}
},
};