Skip to content
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
44 changes: 28 additions & 16 deletions packages/web/src/components/Home/SearchBar/SaveSearchButton.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useState } from "react";
import { useContext } from "react";
import {
Box,
HStack,
Expand All @@ -16,34 +16,46 @@ import bookmarkEmptyWhite from "../../../assets/logos/bookmark-empty-white.svg";
import { UserAuth } from "../../../context/AuthContext";
import DataContext from "../../../context/DataContext";

export default function SaveSearchButton({keyword}) {
const [isBookmarked] = useState(false);
export default function SaveSearchButton({ keyword }) {
const { colorMode } = useColorMode();
const toast = useToast();
const { user, addKeyword } = UserAuth();
const { user, addKeyword, keywords } = UserAuth();
const { onLoginModalOpen } = useContext(DataContext);
const isBookmarked = keywords.includes(keyword);

const handleBookmarkClick = async () => {
if (!user) {
onLoginModalOpen(); // prompt user to log in in order to save search
onLoginModalOpen();
return;
}

const response = await addKeyword(keyword);
const { success, description } = response;
const { success, wasAdded } = response;

if (!success) {
toast({
title: "Error!",
description:
"Sorry, an error occurred while saving your search term. Please try again.",
status: "error",
duration: 3000,
isClosable: true,
});
return;
}

toast({
title: success ? (description === "added" ? "Subscribed!" : "Already subscribed!") : "Error!",
description:
success ?
(description === "added" ?
"Your email was subscribed to notifications for this search." :
"You are already subscribed to notifications for this search."
) :
"Sorry, an error occurred while saving your search term. Please try again.",
status: success ? (description === "added" ? "success" : "info") : "error",
title: wasAdded ? "Subscribed!" : "Already subscribed!",
description: wasAdded
? "Your email was subscribed to notifications for this search."
: "You are already subscribed to notifications for this search.",
status: wasAdded ? "success" : "info",
duration: 3000,
isClosable: true,
});
}
};



return (
<Box
Expand Down
16 changes: 12 additions & 4 deletions packages/web/src/context/AuthContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ export const AuthContextProvider = ({ children }) => {
}, [user]);

const addKeyword = async (keyword) => {
if (keywords.includes(keyword)) {
return { success: true, wasAdded: false };
}

const token = await getAuthToken();
const config = {
headers: {
Authorization: `Bearer ${token}`,
},
};

try {
const response = await axios.post(
`${import.meta.env.VITE_REACT_APP_AWS_BACKEND_URL}/searches`,
Expand All @@ -85,16 +90,19 @@ export const AuthContextProvider = ({ children }) => {
},
config
);
if (response.status == 201) {

if (response.status === 200 || response.status === 201) {
setKeywords((prevKeywords) => [...prevKeywords, keyword]);
return { success: true, description: "added" };
return { success: true, wasAdded: true };
}
return { success: true, description: "existing" };

return { success: true, wasAdded: false };
} catch (err) {
console.error(err);
return { success: false, description: "error" };
return { success: false };
}
};


const removeKeyword = async (deletedKw) => {
const token = await getAuthToken();
Expand Down