Skip to content

Commit

Permalink
Merge pull request #52 from anaswaratrajan/markChoices
Browse files Browse the repository at this point in the history
Refactor serverAPI interface
  • Loading branch information
anastr0 authored Jan 30, 2021
2 parents 6e46252 + 7cb96eb commit 0a0ee74
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
70 changes: 46 additions & 24 deletions src/api/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,19 @@ class ServerAPI {

this.URL = process.env.NEXT_PUBLIC_SERVER_URL;
this.domain = process.env.NEXT_PUBLIC_ORIGIN_DOMAIN;
this.headers = {
"Content-Type": "application/json"
}
}

httpMethod = async (
endpoint: string,
reqMethod: string,
token = "",
payload = ""
requestOptions: RequestInit,
): Promise<HttpResponse> => {
this.headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
};
const requestOptions: RequestInit = {
mode: "cors",
credentials: "include",
method: reqMethod,
headers: this.headers,
};
if (reqMethod !== "GET") {
requestOptions.body = payload;
}
const res = await fetch(endpoint, requestOptions);
const { status } = res;
const responseData = await res.json();
console.log(res)
return {
data: responseData,
statusCode: status,
Expand All @@ -47,7 +36,11 @@ class ServerAPI {
pollid: string | string[] | null | undefined
): Promise<HttpResponse> => {
const endpoint = `${this.URL}/poll/${pollid}`;
return this.httpMethod(endpoint, "GET");
const requestOptions: RequestInit = {
method: "GET",
headers: this.headers,
};
return this.httpMethod(endpoint, requestOptions);
};

getPolls = (pollArgs: {
Expand All @@ -56,27 +49,47 @@ class ServerAPI {
}): Promise<HttpResponse> => {
const { userID, token } = pollArgs;
const endpoint = `${this.URL}/user/${userID}`;
return this.httpMethod(endpoint, "GET", token);
this.headers = {
Authorization: `Bearer ${token}`,
};
const requestOptions: RequestInit = {
credentials: "include",
method: "GET",
headers: this.headers,
};
return this.httpMethod(endpoint, requestOptions);
};

createPoll = (pollArgs: {
poll: RocketMeetPoll;
token: string;
}): Promise<HttpResponse> => {
const { poll, token } = pollArgs;
const payload = JSON.stringify(poll);
const endpoint = `${this.URL}/user/poll`;
return this.httpMethod(endpoint, "POST", token, payload);
this.headers = {
Authorization: `Bearer ${token}`,
};
const requestOptions: RequestInit = {
credentials: "include",
method: "POST",
headers: this.headers,
body: JSON.stringify(poll),
};
return this.httpMethod(endpoint, requestOptions);
};

markChoices = (voteArgs: {
newVote: Vote;
pollid: string;
}): Promise<HttpResponse> => {
const { newVote, pollid } = voteArgs;
const payload = JSON.stringify(newVote);
const endpoint = `${this.URL}/poll/${pollid}`;
return this.httpMethod(endpoint, "PUT", payload);
const requestOptions: RequestInit = {
method: "PUT",
headers: this.headers,
body: JSON.stringify(newVote)
};
return this.httpMethod(endpoint, requestOptions);
};

markFinalChoice = (voteArgs: {
Expand All @@ -85,9 +98,18 @@ class ServerAPI {
token: string;
}): Promise<HttpResponse> => {
const { finalChoice, pollid, token } = voteArgs;
const payload = JSON.stringify(finalChoice);
const endpoint = `${this.URL}/user/poll/${pollid}`;
return this.httpMethod(endpoint, "PUT", token, payload);
this.headers = {
Authorization: `Bearer ${token}`,
};
const requestOptions: RequestInit = {
credentials: "include",
method: "PUT",
headers: this.headers,
body: JSON.stringify(finalChoice)
};
console.log(requestOptions)
return this.httpMethod(endpoint, requestOptions);
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/privateAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const privateAuthWrapper = (Component: NextPage) => {
if (user) {
user.getIdToken(true)
.then((token) => {
// console.log(token)
console.log(token)
dispatch(
login({ displayName: user.displayName, username: user.email, token })
);
Expand Down

1 comment on commit 0a0ee74

@vercel
Copy link

@vercel vercel bot commented on 0a0ee74 Jan 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.