From 17e4979877592a717e2d20f350b1c5d09ff1d2d2 Mon Sep 17 00:00:00 2001 From: German Zarkovich <68071939+gerzarko@users.noreply.github.com> Date: Thu, 26 Sep 2024 09:31:41 -0400 Subject: [PATCH 1/5] add all final implementation --- src/components/common/cart/AddAllToCart.tsx | 86 +++++++++++++++++++++ src/components/posts/ViewUserFavorites.tsx | 3 + 2 files changed, 89 insertions(+) create mode 100644 src/components/common/cart/AddAllToCart.tsx diff --git a/src/components/common/cart/AddAllToCart.tsx b/src/components/common/cart/AddAllToCart.tsx new file mode 100644 index 00000000..223d4a88 --- /dev/null +++ b/src/components/common/cart/AddAllToCart.tsx @@ -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) => { + 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 ( +
+ +
+ ); +}; diff --git a/src/components/posts/ViewUserFavorites.tsx b/src/components/posts/ViewUserFavorites.tsx index 2745c339..06be2789 100644 --- a/src/components/posts/ViewUserFavorites.tsx +++ b/src/components/posts/ViewUserFavorites.tsx @@ -14,6 +14,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); @@ -107,6 +108,7 @@ export const ViewUserFavorites: Component = () => { > {/* */} + { } > +

From 6d598b4835fc8f53c6b1516862a846aebf87b7ea Mon Sep 17 00:00:00 2001 From: German Zarkovich <68071939+gerzarko@users.noreply.github.com> Date: Thu, 26 Sep 2024 09:34:12 -0400 Subject: [PATCH 2/5] add language files --- src/i18n/UI/English.ts | 1 + src/i18n/UI/French.ts | 1 + src/i18n/UI/Spanish.ts | 3 +++ src/i18n/uiType.ts | 1 + 4 files changed, 6 insertions(+) diff --git a/src/i18n/UI/English.ts b/src/i18n/UI/English.ts index 6759651c..311d9087 100644 --- a/src/i18n/UI/English.ts +++ b/src/i18n/UI/English.ts @@ -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", diff --git a/src/i18n/UI/French.ts b/src/i18n/UI/French.ts index f52acb4e..51c2602e 100644 --- a/src/i18n/UI/French.ts +++ b/src/i18n/UI/French.ts @@ -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", diff --git a/src/i18n/UI/Spanish.ts b/src/i18n/UI/Spanish.ts index 797b85bc..cdad9a6c 100644 --- a/src/i18n/UI/Spanish.ts +++ b/src/i18n/UI/Spanish.ts @@ -1,3 +1,5 @@ +import { AddAllToCart } from "@components/common/cart/AddAllToCart"; + export const Spanish = { textDirection: "ltr", siteDescription: "Su comunidad para recursos de aprendizaje de calidad.", @@ -112,6 +114,7 @@ export const Spanish = { filters: "Filtros", faq: "Centro de Ayuda", 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", diff --git a/src/i18n/uiType.ts b/src/i18n/uiType.ts index f10e50d3..8609c7ff 100644 --- a/src/i18n/uiType.ts +++ b/src/i18n/uiType.ts @@ -103,6 +103,7 @@ export interface uiObject { filters: string; faq: string; addToCart: string; + addAllToCart: string; stripeSetup: string; stripeLogin: string; proceedToCheckout: string; From f947d3f268c8849cef3c68283f6fb69b7b2664b1 Mon Sep 17 00:00:00 2001 From: German Zarkovich <68071939+gerzarko@users.noreply.github.com> Date: Thu, 26 Sep 2024 09:38:29 -0400 Subject: [PATCH 3/5] fix spanish file --- src/i18n/UI/Spanish.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/i18n/UI/Spanish.ts b/src/i18n/UI/Spanish.ts index cdad9a6c..0a3656da 100644 --- a/src/i18n/UI/Spanish.ts +++ b/src/i18n/UI/Spanish.ts @@ -1,5 +1,3 @@ -import { AddAllToCart } from "@components/common/cart/AddAllToCart"; - export const Spanish = { textDirection: "ltr", siteDescription: "Su comunidad para recursos de aprendizaje de calidad.", @@ -114,7 +112,7 @@ export const Spanish = { filters: "Filtros", faq: "Centro de Ayuda", addToCart: "añadir a la cesta", - AddAllToCart: "añadir todo 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", From 71c508a0c1e24f3928b5117c2ed8f3334b5ab216 Mon Sep 17 00:00:00 2001 From: German Zarkovich <68071939+gerzarko@users.noreply.github.com> Date: Thu, 26 Sep 2024 09:39:54 -0400 Subject: [PATCH 4/5] fix punctuation in spanish --- src/i18n/UI/Spanish.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/i18n/UI/Spanish.ts b/src/i18n/UI/Spanish.ts index 0a3656da..8eeb2332 100644 --- a/src/i18n/UI/Spanish.ts +++ b/src/i18n/UI/Spanish.ts @@ -111,8 +111,8 @@ export const Spanish = { saveProfile: "Guardar Perfil", filters: "Filtros", faq: "Centro de Ayuda", - addToCart: "añadir a la cesta", - addAllToCart: "añadir todo 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", From 0cabd28e88927ddb7412ff9bbfd5fce629d65013 Mon Sep 17 00:00:00 2001 From: German Zarkovich <68071939+gerzarko@users.noreply.github.com> Date: Thu, 26 Sep 2024 09:44:30 -0400 Subject: [PATCH 5/5] fix aria label --- src/components/common/cart/AddAllToCart.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/common/cart/AddAllToCart.tsx b/src/components/common/cart/AddAllToCart.tsx index 223d4a88..b23b5c3c 100644 --- a/src/components/common/cart/AddAllToCart.tsx +++ b/src/components/common/cart/AddAllToCart.tsx @@ -73,7 +73,7 @@ export const AddAllToCart: Component = (props: Props) => {