Skip to content

Commit

Permalink
fix: circular-deps in vote buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Anush008 committed May 13, 2023
1 parent f86593c commit 675496c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
7 changes: 3 additions & 4 deletions src/content-scripts/components/RepoVoting/RepoUnvoteButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ export const RepoUnvoteButton = (ownerName: string, repoName: string) => {
`;
const userToken = await getAuthToken();

const unvoted = await voteOrUnvoteRepo(userToken, ownerName, repoName, false);
const unvoted = await voteOrUnvoteRepo(userToken, ownerName, repoName, "DELETE");

if (unvoted) {
const voteRepoButton = VoteRepoButton(ownerName, repoName);

repoUnvoteButton.replaceWith(voteRepoButton);
const { VoteRepoButton } = await import("./RepoVoteButton");
repoUnvoteButton.replaceWith(VoteRepoButton(ownerName, repoName));
} else {
console.log("Something went wrong");
}
Expand Down
8 changes: 3 additions & 5 deletions src/content-scripts/components/RepoVoting/RepoVoteButton.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "../../content-scripts.css";
import { createHtmlElement } from "../../../utils/createHtmlElement";
import { getAuthToken } from "../../../utils/checkAuthentication";
import { RepoUnvoteButton } from "./RepoUnvoteButton";
import { voteOrUnvoteRepo } from "../../../utils/fetchOpenSaucedApiData";

export const VoteRepoButton = (ownerName: string, repoName: string) => {
Expand All @@ -20,12 +19,11 @@ export const VoteRepoButton = (ownerName: string, repoName: string) => {
`;
const userToken = await getAuthToken();

const voted = await voteOrUnvoteRepo(userToken, ownerName, repoName, true);
const voted = await voteOrUnvoteRepo(userToken, ownerName, repoName, "PUT");

if (voted) {
const unvoteRepoButton = RepoUnvoteButton(ownerName, repoName);

voteRepoButton.replaceWith(unvoteRepoButton);
const { RepoUnvoteButton } = await import("./RepoUnvoteButton");
voteRepoButton.replaceWith(RepoUnvoteButton(ownerName, repoName));
} else {
console.log("Something went wrong");
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/fetchOpenSaucedApiData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ export const repoExistsOnOpenSauced = async (ownerName: string, repoName: string
return response.status === 200;
};

export const voteOrUnvoteRepo = async (userToken: string, ownerName: string, repoName: string, vote: boolean) => {
export const voteOrUnvoteRepo = async (userToken: string, ownerName: string, repoName: string, method: "PUT" | "DELETE") => {
const response = await fetch(
`${OPEN_SAUCED_REPOS_ENDPOINT}/${ownerName}/${repoName}/vote`,
{
method: vote ? "PUT" : "DELETE",
method,
headers: { Authorization: `Bearer ${userToken}` },
},
);
Expand Down

0 comments on commit 675496c

Please sign in to comment.