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

Add all favorites to Cart Button #157

Merged
merged 6 commits into from
Oct 1, 2024
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
86 changes: 86 additions & 0 deletions src/components/common/cart/AddAllToCart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { onMount } from "solid-js";
import type { Component } from "solid-js";
import type { Post } from "@lib/types";
import { getLangFromUrl, useTranslations } from "@i18n/utils";
import cart from "@assets/shopping-cart.svg";
import { items, setItems } from "@components/common/cart/AddToCartButton";

const lang = getLangFromUrl(new URL(window.location.href));
const t = useTranslations(lang);

interface Props {
favorites: Post[];
}

export const AddAllToCart: Component<Props> = (props: Props) => {
const storedItems = localStorage.getItem("cartItems");

props.favorites.map((fav) => {
fav.quantity = 1;
});

onMount(() => {
if (storedItems) {
setItems(JSON.parse(storedItems));
}
});

function clickHandler(e: Event) {
e.preventDefault();
e.stopPropagation();

const elem = document.getElementById("addAllToCart");
elem?.classList.add("animate-click");

let itemInCart = false;

const updatedItems = items.map((item: Post) => {
const favoriteMatch = props.favorites.find(
(favoritePost) => favoritePost.product_id === item.product_id
);
if (favoriteMatch) {
itemInCart = true;
console.log(item.quantity, favoriteMatch.quantity);
return {
...item,
quantity: item.quantity + favoriteMatch.quantity,
};
}
console.log(item);
return item;
});

const newItems = props.favorites.filter(
(fav) => !items.some((item) => item.product_id === fav.product_id)
);
if (newItems.length > 0) {
const newCartItems = [...updatedItems, ...newItems];
setItems(newCartItems);
console.log(newCartItems);
elem!.innerText = t("buttons.addedToCart");
} else {
// update the store quantity
setItems(updatedItems);
console.log(updatedItems);
elem!.innerText = t("buttons.addedToCart");
}

localStorage.setItem("cartItems", JSON.stringify(updatedItems));
}

return (
<div class="relative z-10 w-full">
<button
onclick={(e) => clickHandler(e)}
class="btn-cart"
aria-label={t("buttons.addAllToCart")}
id={"addAllToCart"}
onAnimationEnd={(elem) => {
elem.target.classList.remove("animate-click");
}}
>
{t("buttons.addAllToCart")}
</button>
</div>
);
};
3 changes: 3 additions & 0 deletions src/components/posts/ViewUserFavorites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { MobileViewCard } from "@components/services/MobileViewCard.tsx";
import { useStore } from "@nanostores/solid";
import { windowSize } from "@components/common/WindowSizeStore";
import { downloadPostImage, downloadUserImage } from "@lib/imageHelper.tsx";
import { AddAllToCart } from "@components/common/cart/AddAllToCart.tsx";

const lang = getLangFromUrl(new URL(window.location.href));
const t = useTranslations(lang);
Expand Down Expand Up @@ -105,6 +106,7 @@ export const ViewUserFavorites: Component = () => {
}
>
<ViewCard posts={favoritedItems()} />
<AddAllToCart favorites={favoritedItems()} />
</Show>

<Show
Expand All @@ -113,6 +115,7 @@ export const ViewUserFavorites: Component = () => {
}
>
<MobileViewCard lang={lang} posts={favoritedItems()} />
<AddAllToCart favorites={favoritedItems()} />
</Show>
<Show when={favoritedItems().length === 0}>
<p class="mb-6 italic">
Expand Down
1 change: 1 addition & 0 deletions src/i18n/UI/English.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export const English = {
filters: "Filters",
faq: "Help Center",
addToCart: "Add to Cart",
addAllToCart: "Add All to Cart",
stripeSetup: "Stripe Setup",
stripeLogin: "Stripe Login",
proceedToCheckout: "Proceed to Checkout",
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 @@ -108,6 +108,7 @@ export const French = {
filters: "Filtres",
faq: "Centre d'Aide",
addToCart: "Ajouter au panier",
addAllToCart: "Ajouter tout panier",
stripeSetup: "Configuration de Stripe",
stripeLogin: "Connexion à Stripe",
proceedToCheckout: "Passer à la caisse",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/UI/Spanish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ export const Spanish = {
saveProfile: "Guardar Perfil",
filters: "Filtros",
faq: "Centro de Ayuda",
addToCart: "añadir a la cesta",
addToCart: "Añadir a la cesta",
addAllToCart: "Añadir todo a la cesta",
stripeSetup: "Configuración de Stripe",
stripeLogin: "Iniciar sesión Stripe",
proceedToCheckout: "Pasar por la caja",
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/uiType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export interface uiObject {
filters: string;
faq: string;
addToCart: string;
addAllToCart: string;
stripeSetup: string;
stripeLogin: string;
proceedToCheckout: string;
Expand Down Expand Up @@ -145,6 +146,7 @@ export interface uiObject {
};



messages: {
noAccount: string;
emailValid: string;
Expand Down
Loading