From c1f9bf489a0dc26a1aa5d11637b07f486e38a478 Mon Sep 17 00:00:00 2001 From: Anselm Marie Date: Sat, 7 Sep 2024 13:08:30 -0400 Subject: [PATCH] =?UTF-8?q?clean=20up=20=E2=80=A2=20any=20cart=20counter?= =?UTF-8?q?=20to=20header=20(styling=20counter=20in=20future)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/lib/navigation/header/header.tsx | 19 +++++++++++++++- .../pokemon.card/pokemon.card.view.tsx | 18 ++++++--------- .../pokemon.card/use.pokemon.card.logic.ts | 22 +++++++++++-------- .../use.pokemon.detail.modal.logic.ts | 2 +- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/libs/features/src/lib/navigation/header/header.tsx b/libs/features/src/lib/navigation/header/header.tsx index 4948c01..77f1747 100644 --- a/libs/features/src/lib/navigation/header/header.tsx +++ b/libs/features/src/lib/navigation/header/header.tsx @@ -1,4 +1,7 @@ +import { useMemo } from 'react'; + import { pokeshopLogo } from '@pokemon-pet-shop/assets'; +import { useGetCart } from '@pokemon-pet-shop/services'; import { UiElementLayout, ElementLayoutTypeEnum, @@ -16,6 +19,17 @@ import { import { styles } from './header.module'; const Header = () => { + const { data } = useGetCart(); + + const getCartCounter = useMemo(() => { + let counter = 0; + (data || []).forEach((el) => { + counter = counter + el.quantity; + }); + + return counter; + }, [data]); + return ( @@ -39,7 +53,10 @@ const Header = () => { /> - + + {getCartCounter} + + ); diff --git a/libs/features/src/lib/pokemon/pokemon.card/pokemon.card.view.tsx b/libs/features/src/lib/pokemon/pokemon.card/pokemon.card.view.tsx index 5eb6fbd..eb17eef 100644 --- a/libs/features/src/lib/pokemon/pokemon.card/pokemon.card.view.tsx +++ b/libs/features/src/lib/pokemon/pokemon.card/pokemon.card.view.tsx @@ -22,7 +22,7 @@ import { styles } from './pokemon.card.module'; import usePokemonCard from './use.pokemon.card.logic'; const PokemonCard = ({ data = {} }: CardProps): ReactElement => { - const { getPokemonDetail, getThemeClass } = usePokemonCard(data); + const { getThemeClass, onHandleUpdateCartSubmit } = usePokemonCard(data); const openModal = useModalStore((state) => state.openModal); const handleOpenDetailModalClick = () => { @@ -30,7 +30,7 @@ const PokemonCard = ({ data = {} }: CardProps): ReactElement => { content: , options: { title: 'Here', - data: getPokemonDetail, + data: data, classNameShadow: '', classNameModal: '', isModalShown: false, @@ -43,10 +43,6 @@ const PokemonCard = ({ data = {} }: CardProps): ReactElement => { }); }; - const handleAddPetInCartClick = () => { - console.log('handleAddPetInCartClick'); - }; - return ( @@ -54,7 +50,7 @@ const PokemonCard = ({ data = {} }: CardProps): ReactElement => { $500.00 { )} typographyType={TypographyTypeEnum.SPAN} > - NO. {getPokemonDetail?.order} + NO. {data?.order} { {data?.name} - {(getPokemonDetail?.abilities || []).map( + {(data?.abilities || []).map( (abilityObj: PokemonDetailAbilityObj, i: number): ReactElement | null => { if (i > 1) { return null; @@ -107,7 +103,7 @@ const PokemonCard = ({ data = {} }: CardProps): ReactElement => { ); @@ -116,7 +112,7 @@ const PokemonCard = ({ data = {} }: CardProps): ReactElement => { - + { - /** This needs to change once the apps calls multiple different detail */ - const getPokemonDetail = useMemo(() => { - return dataDetail; - }, [dataDetail]); +const usePokemonCard = (data: PokemonListApi): UseCardReturn => { + const { getThemeClass } = usePokemonTheme(data?.types); + const updateCartMutation = useUpdateCart(); - const { getThemeClass } = usePokemonTheme(getPokemonDetail?.types); + const handleUpdateCartSubmit = () => { + updateCartMutation.mutate({ + id: data?.id, + quantity: 1, + }); + }; return { - getPokemonDetail, getThemeClass, + onHandleUpdateCartSubmit: handleUpdateCartSubmit, }; }; diff --git a/libs/features/src/lib/pokemon/pokemon.detail.modal/use.pokemon.detail.modal.logic.ts b/libs/features/src/lib/pokemon/pokemon.detail.modal/use.pokemon.detail.modal.logic.ts index 450a087..30141c8 100644 --- a/libs/features/src/lib/pokemon/pokemon.detail.modal/use.pokemon.detail.modal.logic.ts +++ b/libs/features/src/lib/pokemon/pokemon.detail.modal/use.pokemon.detail.modal.logic.ts @@ -11,8 +11,8 @@ interface PokemonDetailModalReturn { const usePokemonDetailModalLogic = (modalData: PokemonListApi): PokemonDetailModalReturn => { const res = useGetPokemonSpecies([String(modalData?.id)]); - const updateCartMutation = useUpdateCart(); const { data, isError, isLoading, isFetching } = res[0]; + const updateCartMutation = useUpdateCart(); const handleUpdateCartSubmit = () => { updateCartMutation.mutate({