Skip to content

Commit

Permalink
Merge pull request #128 from datagrove/bugFix
Browse files Browse the repository at this point in the history
fix favorites for not signed in users
  • Loading branch information
r-southworth authored Aug 14, 2024
2 parents 767df44 + 83fad60 commit 6b29196
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
61 changes: 39 additions & 22 deletions src/components/posts/AddFavorite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const FavoriteButton: Component<Props> = (props) => {
[]
);
const [isFavorited, setIsFavorited] = createSignal<boolean>(false);
const [notUser, setNotUser] = createSignal<boolean>(false);

onMount(async () => {
if (UserError) {
Expand All @@ -36,20 +37,22 @@ export const FavoriteButton: Component<Props> = (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(() => {
Expand Down Expand Up @@ -99,18 +102,25 @@ export const FavoriteButton: Component<Props> = (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) {
Expand Down Expand Up @@ -224,6 +234,13 @@ export const FavoriteButton: Component<Props> = (props) => {
</p>
</div>
</Show>
<Show when={notUser() === true}>
<div class="absolute -right-1 top-24 z-0 w-[190px] rounded-lg bg-background1 py-0.5 text-black shadow-md dark:bg-background1-DM md:w-[194px] lg:w-[240px]">
<p class="pr-1 text-center italic text-ptext1 dark:text-ptext1-DM">
{t("messages.signIntoAddToFavorites")}
</p>
</div>
</Show>
</div>
);
};
1 change: 1 addition & 0 deletions src/i18n/UI/English.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",

},

Expand Down
1 change: 1 addition & 0 deletions src/i18n/UI/French.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
1 change: 1 addition & 0 deletions src/i18n/UI/Spanish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
1 change: 1 addition & 0 deletions src/i18n/uiType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export interface uiObject {
pleaseDescribe: string;
addedToFavorites: string;
noFavoriteItems: string;
signIntoAddToFavorites: string;
};

formLabels: {
Expand Down

0 comments on commit 6b29196

Please sign in to comment.