Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update like and bookmark endpoints #20

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 24 additions & 29 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,8 @@ export const createTweet = async ({token,formData}:
export const likeTweet = async (tweetId: string, token: string) => {
try {
const res = await axios.post(
`${VITE_BACKEND_URL}/api/v1/tweets/like`,
{
tweetId,
},
`${VITE_BACKEND_URL}/api/v1/tweets/${tweetId}/like`,
{},
{
headers: {
Accept: "application/json",
Expand All @@ -635,16 +633,15 @@ export const likeTweet = async (tweetId: string, token: string) => {
*/
export const unlikeTweet = async (tweetId: string, token: string) => {
try {
const res = await axios.delete(`${VITE_BACKEND_URL}/api/v1/tweets/like`, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
data: {
tweetId,
},
});
const res = await axios.delete(
`${VITE_BACKEND_URL}/api/v1/tweets/${tweetId}/like`,
{
headers: {
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
}
);
return res.data;
} catch (error) {
console.log(error);
Expand All @@ -661,10 +658,8 @@ export const unlikeTweet = async (tweetId: string, token: string) => {
export const bookmarkTweet = async (tweetId: string, token: string) => {
try {
const res = await axios.post(
`${VITE_BACKEND_URL}/api/v1/bookmarks`,
{
tweetId,
},
`${VITE_BACKEND_URL}/api/v1/tweets/${tweetId}/bookmark`,
{},
{
headers: {
Accept: "application/json",
Expand All @@ -688,19 +683,19 @@ export const bookmarkTweet = async (tweetId: string, token: string) => {
*/
export const unBookmarkTweet = async (tweetId: string, token: string) => {
try {
const res = await axios.delete(`${VITE_BACKEND_URL}/api/v1/bookmarks`, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
data: {
tweetId,
},
});
const res = await axios.delete(
`${VITE_BACKEND_URL}/api/v1/tweets/${tweetId}/bookmark`,
{
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
}
);
return res.data;
} catch (error) {
console.log(error);
throw new Error("Error unBookmarking tweet");
}
}
};
28 changes: 20 additions & 8 deletions src/mocks/TweetWorker/TweetWorker.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { HttpResponse, ResponseResolver } from "msw";

export const likeTweetWorker: ResponseResolver = async ({ request }) => {
export const likeTweetWorker: ResponseResolver = async ({
request,
params,
}) => {
const token = request.headers.get("Authorization")?.split(" ")[1];
const { tweetId } = (await request.json()) as {
const { tweetId } = params as {
tweetId: string;
};

Expand All @@ -21,9 +24,12 @@ export const likeTweetWorker: ResponseResolver = async ({ request }) => {
);
};

export const unLikeTweetWorker: ResponseResolver = async ({ request }) => {
export const unLikeTweetWorker: ResponseResolver = async ({
request,
params,
}) => {
const token = request.headers.get("Authorization")?.split(" ")[1];
const { tweetId } = (await request.json()) as {
const { tweetId } = params as {
tweetId: string;
};

Expand All @@ -42,9 +48,12 @@ export const unLikeTweetWorker: ResponseResolver = async ({ request }) => {
);
};

export const bookmarkTweetWorker: ResponseResolver = async ({ request }) => {
export const bookmarkTweetWorker: ResponseResolver = async ({
request,
params,
}) => {
const token = request.headers.get("Authorization")?.split(" ")[1];
const { tweetId } = (await request.json()) as {
const { tweetId } = params as {
tweetId: string;
};

Expand All @@ -63,9 +72,12 @@ export const bookmarkTweetWorker: ResponseResolver = async ({ request }) => {
);
};

export const unBookmarkTweetWorker: ResponseResolver = async ({ request }) => {
export const unBookmarkTweetWorker: ResponseResolver = async ({
request,
params,
}) => {
const token = request.headers.get("Authorization")?.split(" ")[1];
const { tweetId } = (await request.json()) as {
const { tweetId } = params as {
tweetId: string;
};

Expand Down
8 changes: 4 additions & 4 deletions src/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export const handlers = [
getUsersSuggestionsWorkerHandler
),
http.put(`${VITE_BACKEND_URL}/api/v1/user/profile`, EditProfileWorker),
http.post(`${VITE_BACKEND_URL}/api/v1/tweets/like`, likeTweetWorker),
http.delete(`${VITE_BACKEND_URL}/api/v1/tweets/like`, unLikeTweetWorker),
http.post(`${VITE_BACKEND_URL}/api/v1/bookmarks`, bookmarkTweetWorker),
http.delete(`${VITE_BACKEND_URL}/api/v1/bookmarks`, unBookmarkTweetWorker),
http.post(`${VITE_BACKEND_URL}/api/v1/tweets/:tweetId/like`, likeTweetWorker),
http.delete(`${VITE_BACKEND_URL}/api/v1/tweets/:tweetId/like`, unLikeTweetWorker),
http.post(`${VITE_BACKEND_URL}/api/v1/tweets/:tweetId/bookmark`, bookmarkTweetWorker),
http.delete(`${VITE_BACKEND_URL}/api/v1/tweets/:tweetId/bookmark`, unBookmarkTweetWorker),
];