diff --git a/src/components/posts/AddFavorite.tsx b/src/components/posts/AddFavorite.tsx index 93c3a4e6..c2ed3360 100644 --- a/src/components/posts/AddFavorite.tsx +++ b/src/components/posts/AddFavorite.tsx @@ -23,6 +23,7 @@ export const FavoriteButton: Component = (props) => { [] ); const [isFavorited, setIsFavorited] = createSignal(false); + const [notUser, setNotUser] = createSignal(false); onMount(async () => { if (UserError) { @@ -36,20 +37,22 @@ export const FavoriteButton: Component = (props) => { } } - const { data, error } = await supabase - .from("favorites") - .select("list_number") - .eq("default_list", true); - if (error) { - console.log("supabase errror: " + error.message); - } + if (session() !== null) { + const { data, error } = await supabase + .from("favorites") + .select("list_number") + .eq("default_list", true); + if (error) { + console.log("supabase errror: " + error.message); + } - if (data) { - if (data.length > 0) { - setListNumber(data[0].list_number); + if (data) { + if (data.length > 0) { + setListNumber(data[0].list_number); + } } + getFavorites(); } - getFavorites(); }); // createEffect(() => { @@ -99,18 +102,25 @@ export const FavoriteButton: Component = (props) => { e.preventDefault(); e.stopPropagation(); - const { data, error } = await supabase - .from("favorites_products") - .insert({ - list_number: listNumber(), - product_id: props.id, - }); - if (error) { - console.log("supabase errror: " + error.message); + if (session() === null) { + setNotUser(true); + setTimeout(() => setNotUser(false), 3000); + } + + if (notUser() === false) { + const { data, error } = await supabase + .from("favorites_products") + .insert({ + list_number: listNumber(), + product_id: props.id, + }); + if (error) { + console.log("supabase errror: " + error.message); + } + setAdded(true); + getFavorites(); + setTimeout(() => setAdded(false), 3000); } - setAdded(true); - getFavorites(); - setTimeout(() => setAdded(false), 3000); } async function removeFromFavorites(e: Event) { @@ -224,6 +234,13 @@ export const FavoriteButton: Component = (props) => {

+ +
+

+ {t("messages.signIntoAddToFavorites")} +

+
+
); }; diff --git a/src/i18n/UI/English.ts b/src/i18n/UI/English.ts index 5920188b..014ec99b 100644 --- a/src/i18n/UI/English.ts +++ b/src/i18n/UI/English.ts @@ -198,6 +198,7 @@ export const English = { pleaseDescribe: "Please describe the issue", addedToFavorites: "Added to Favorites!", noFavoriteItems: "No favorites yet - go get shopping!", + signIntoAddToFavorites: "Sign in to add to favorites", }, diff --git a/src/i18n/UI/French.ts b/src/i18n/UI/French.ts index 1504d3b6..20a06bee 100644 --- a/src/i18n/UI/French.ts +++ b/src/i18n/UI/French.ts @@ -200,6 +200,7 @@ export const French = { pleaseDescribe: "S'il vous plaît décrivez le problème", addedToFavorites: "Ajouté aux Favoris!", noFavoriteItems: "Vous n'avez aucun favori!", + signIntoAddToFavorites: "Connectez-vous pour ajouter aux favoris", }, formLabels: { diff --git a/src/i18n/UI/Spanish.ts b/src/i18n/UI/Spanish.ts index 3109a0d9..3920108c 100644 --- a/src/i18n/UI/Spanish.ts +++ b/src/i18n/UI/Spanish.ts @@ -203,6 +203,7 @@ export const Spanish = { pleaseDescribe: "Por favor describe el problema", addedToFavorites: "¡Añadida a favoritos!", noFavoriteItems: "No hay elementos favoritos", + signIntoAddToFavorites:"Inicie sesión para agregar a favoritos", }, formLabels: { diff --git a/src/i18n/uiType.ts b/src/i18n/uiType.ts index 656705a3..1b179a6f 100644 --- a/src/i18n/uiType.ts +++ b/src/i18n/uiType.ts @@ -170,6 +170,7 @@ export interface uiObject { pleaseDescribe: string; addedToFavorites: string; noFavoriteItems: string; + signIntoAddToFavorites: string; }; formLabels: {