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

feat: improve variant handling for wishlist #694

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1eafcbb
feat: improve variant handling for wishlist
ivladu-plenty Sep 26, 2024
e125604
Merge branch 'main' into feat/improve_variant_handling_for_wishlist
ivladu-plenty Sep 26, 2024
be96125
add condition to show the button
ivladu-plenty Sep 27, 2024
b6a93cf
Merge branch 'main' into feat/improve_variant_handling_for_wishlist
ivladu-plenty Sep 27, 2024
dfd57a1
Merge branch 'main' into feat/improve_variant_handling_for_wishlist
ivladu-plenty Sep 30, 2024
8919ca7
add param to url for the getWishlist test
ivladu-plenty Sep 30, 2024
ab6005c
added implementation for wishlist
ivladu-plenty Oct 4, 2024
a7e2bd7
revert package json file
ivladu-plenty Oct 7, 2024
5a56117
revert yarn lock
ivladu-plenty Oct 7, 2024
2a8ff74
Merge branch 'main' into feat/improve_variant_handling_for_wishlist
ivladu-plenty Oct 7, 2024
8e417b3
remove unused method
ivladu-plenty Oct 7, 2024
3a3ee06
fix lint errors
ivladu-plenty Oct 7, 2024
2b91dc0
fix lint errors
ivladu-plenty Oct 7, 2024
dbbe0fc
remove space
ivladu-plenty Oct 7, 2024
ad3c3b0
remove unused logic
ivladu-plenty Oct 9, 2024
1bd373d
Merge branch 'main' into feat/improve_variant_handling_for_wishlist
ivladu-plenty Oct 9, 2024
48133fa
fix lint errors
ivladu-plenty Oct 9, 2024
251e3d9
commit changes
ivladu-plenty Oct 9, 2024
98c10f5
chore: refactored wishlist composable + redone wishlist item check logic
abocsan-plenty Oct 9, 2024
f2fd7f9
chore: delete wishlist item without refetching
abocsan-plenty Oct 10, 2024
c0baa95
chore: redo wishlist logic to account for all scenarios
abocsan-plenty Oct 10, 2024
1ae24ac
Merge branch 'main' of github.com:plentymarkets/storefront-nuxt3-boil…
abocsan-plenty Oct 10, 2024
7aee818
chore: case where simple item was added to wishlist from product page
abocsan-plenty Oct 10, 2024
35bdd5b
set empty object in useInitialSetup
ivladu-plenty Oct 14, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const cart: Cart = {
"subAmount": 1,
"isExportDelivery": true,
"shopCountryId": 1,
"itemWishListIds": [],
"itemWishListIds": {},
"items": [
{
"id": 1,
Expand Down
7 changes: 4 additions & 3 deletions apps/web/components/WishlistButton/WishlistButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
<SfLoaderCircular v-if="actionLoading" class="flex justify-center items-center" size="sm" />
<template v-else>
<SfIconClose v-if="isCloseButton" size="sm" />
<SfIconFavoriteFilled v-else-if="isWishlistItem(variationId)" size="sm" />
<SfIconFavoriteFilled v-else-if="isWishlistItem(variationId) && isTrulyInWishlist" size="sm" />
<SfIconFavorite v-else size="sm" />

<slot />
</template>
</UiButton>
Expand All @@ -28,7 +29,7 @@ import type { WishlistButtonProps } from '~/components/WishlistButton/types';
import { SfIconFavorite, SfIconFavoriteFilled, SfLoaderCircular, SfIconClose } from '@storefront-ui/vue';
import { productGetters } from '@plentymarkets/shop-api';

const { product, quantity = 1, discard = false } = defineProps<WishlistButtonProps>();
const { product, quantity = 1, discard = false, isTrulyInWishlist = false } = defineProps<WishlistButtonProps>();
const { t } = useI18n();
const { isWishlistItem, interactWithWishlist, loading: wishlistLoading } = useWishlist();
const actionLoading = ref(false);
Expand All @@ -38,7 +39,7 @@ const variationId = computed(() => productGetters.getVariationId(product));
const isCloseButton = computed(() => isWishlistItem(variationId.value) && discard);
const onWishlistClick = async () => {
actionLoading.value = true;
await interactWithWishlist(variationId.value, quantity);
await interactWithWishlist(variationId.value, quantity, isTrulyInWishlist);
actionLoading.value = false;
};
</script>
1 change: 1 addition & 0 deletions apps/web/components/WishlistButton/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export type WishlistButtonProps = {
product: Product;
quantity?: number;
discard?: boolean;
isTrulyInWishlist?: boolean;
};
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@
:show-base-price="productGetters.showPricePerUnit(product)"
>
<template #wishlistButton>
<WishlistButton discard square class="absolute top-0 right-0 mr-2 mb-2 bg-white" :product="product" />
<WishlistButton
discard
square
class="absolute top-0 right-0 mr-2 mb-2 bg-white"
:product="product"
is-truly-in-wishlist
/>
</template>
</UiProductCard>
</NuxtLazyHydrate>
Expand Down
4 changes: 2 additions & 2 deletions apps/web/components/ui/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
class="group relative text-white hover:text-white active:text-white hover:bg-primary-800 active:bg-primary-700 mr-1 -ml-0.5 rounded-md"
:tag="NuxtLink"
:to="localePath(paths.wishlist)"
:aria-label="t('numberInWishlist', { count: wishlistItemIds.length })"
:aria-label="t('numberInWishlist', { count: Object.keys(wishlistItemIds).length })"
variant="tertiary"
square
data-testid="wishlist-page-navigation"
>
<template #prefix>
<SfIconFavorite />
<SfBadge
:content="wishlistItemIds.length"
:content="Object.keys(wishlistItemIds).length"
class="outline outline-primary-500 bg-white !text-neutral-900 group-hover:outline-primary-800 group-active:outline-primary-700 flex justify-center items-center text-[13px] leading-[12px] min-w-[16px] min-h-[16px]"
data-testid="wishlist-badge"
placement="top-right"
Expand Down
2 changes: 1 addition & 1 deletion apps/web/components/ui/NavbarBottom.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/>
<SfBadge
v-if="label === t('wishlist')"
:content="wishlistItemIds.length"
:content="Object.keys(wishlistItemIds).length"
:max="99"
class="translate-x-[5px] translate-y-[-3px] outline outline-primary-500 bg-white !text-neutral-900 group-hover:outline-primary-800 group-active:outline-primary-700 flex justify-center items-center text-[13px] leading-[12px] min-w-[16px] min-h-[16px]"
data-testid="wishlist-badge"
Expand Down
13 changes: 11 additions & 2 deletions apps/web/components/ui/ProductCard/ProductCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
square
class="absolute bottom-0 right-0 mr-2 mb-2 bg-white ring-1 ring-inset ring-neutral-200 !rounded-full"
:product="product"
:is-truly-in-wishlist="
productGetters.canBeAddedToCartFromCategoryPage(product) ===
canBeDirectlyAddedToCart(productGetters.getVariationId(product))
"
/>
</slot>
</div>
Expand Down Expand Up @@ -70,7 +74,11 @@
</span>
</div>
<UiButton
v-if="productGetters.canBeAddedToCartFromCategoryPage(product)"
v-if="
isFromWishlist
? wishlistGetters.canDirectlyAddToCart(product)
: productGetters.canBeAddedToCartFromCategoryPage(product)
"
size="sm"
class="min-w-[80px] w-fit"
data-testid="add-to-basket-short"
Expand All @@ -93,7 +101,7 @@
</template>

<script setup lang="ts">
import { CategoryTreeItem, productGetters } from '@plentymarkets/shop-api';
import { CategoryTreeItem, productGetters, wishlistGetters } from '@plentymarkets/shop-api';
import { SfLink, SfIconShoppingCart, SfLoaderCircular, SfRating, SfCounter } from '@storefront-ui/vue';
import type { ProductCardProps } from '~/components/ui/ProductCard/types';

Expand Down Expand Up @@ -124,6 +132,7 @@ const { openQuickCheckout } = useQuickCheckout();
const { addToCart } = useCart();
const { send } = useNotification();
const { price, crossedPrice } = useProductPrice(product);
const { canBeDirectlyAddedToCart } = useWishlist();

const loading = ref(false);
const runtimeConfig = useRuntimeConfig();
Expand Down
4 changes: 2 additions & 2 deletions apps/web/components/ui/ProductCard/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Product } from '@plentymarkets/shop-api';
import type { Product, WishlistItem } from '@plentymarkets/shop-api';

export type ProductCardProps = {
product: Product;
product: Product | WishlistItem;
name: string;
imageUrl: string;
imageAlt?: string;
Expand Down
9 changes: 7 additions & 2 deletions apps/web/components/ui/PurchaseCard/PurchaseCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
'bottom-0 right-0 mr-2 mb-2 bg-white ring-1 ring-inset ring-neutral-200 !rounded-full':
viewport.isLessThan('lg'),
}"
:is-truly-in-wishlist="!productCanBeAddedToWishlist(product)"
>
<template v-if="viewport.isGreaterOrEquals('lg')">
{{ !isWishlistItem(productGetters.getVariationId(product)) ? t('addToWishlist') : t('removeFromWishlist') }}
{{
!isWishlistItem(productGetters.getVariationId(product)) || productCanBeAddedToWishlist(product)
? t('addToWishlist')
: t('removeFromWishlist')
}}
</template>
</WishlistButton>
</div>
Expand Down Expand Up @@ -143,7 +148,7 @@ const { send } = useNotification();
const { addToCart, loading } = useCart();
const { t } = useI18n();
const quantitySelectorValue = ref(productGetters.getMinimumOrderQuantity(product));
const { isWishlistItem } = useWishlist();
const { isWishlistItem, productCanBeAddedToWishlist } = useWishlist();
const { openQuickCheckout } = useQuickCheckout();
const { crossedPrice } = useProductPrice(product);
const { reviewArea } = useProductReviews(Number(productGetters.getId(product)));
Expand Down
11 changes: 8 additions & 3 deletions apps/web/composables/useCustomer/useCustomer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { RegisterParams, SessionResult, UserChangePasswordParams } from '@plentymarkets/shop-api';
import type {
RegisterParams,
SessionResult,
UserChangePasswordParams,
WishlistVariation,
} from '@plentymarkets/shop-api';
import type {
UseCustomerReturn,
UseCustomerState,
Expand Down Expand Up @@ -63,7 +68,7 @@ export const useCustomer: UseCustomerReturn = () => {
useHandleError(error.value);
state.value.data = data?.value?.data ?? state.value.data;
checkUserState();
useWishlist().setWishlistItemIds(state.value.data?.basket?.itemWishListIds || []);
useWishlist().setWishlistItemIds(state.value.data?.basket?.itemWishListIds || ({} as WishlistVariation));

state.value.loading = false;
return state.value.data;
Expand Down Expand Up @@ -140,7 +145,7 @@ export const useCustomer: UseCustomerReturn = () => {

state.value.data.user = null;
checkUserState();
useWishlist().setWishlistItemIds([]);
useWishlist().setWishlistItemIds({} as WishlistVariation);
};

/** Function for registering a user.
Expand Down
10 changes: 7 additions & 3 deletions apps/web/composables/useInitialSetup/useInitialSetup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Cart, SessionResult } from '@plentymarkets/shop-api';

import type { Cart, SessionResult, WishlistVariation } from '@plentymarkets/shop-api';
import type { SetInitialData, UseInitialSetupReturn } from './types';
import type { ErrorParams } from '../useHandleError';

Expand Down Expand Up @@ -52,7 +51,12 @@ const setInitialDataSSR: SetInitialData = async () => {
setUser(data.value.data.session as SessionResult);
setCart(data.value.data.session?.basket as Cart);
setCategoryTree(data.value.data.categories);
setWishlistItemIds(data.value.data.session?.basket?.itemWishListIds || []);

let dataWishlistIds = data.value.data.session?.basket?.itemWishListIds;
if (dataWishlistIds && dataWishlistIds.length === 0) {
dataWishlistIds = {} as WishlistVariation;
}
setWishlistItemIds(dataWishlistIds || ({} as WishlistVariation));
}
} catch (error) {
useHandleError(error as ErrorParams);
Expand Down
12 changes: 9 additions & 3 deletions apps/web/composables/useWishlist/types.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import type {
WishlistItem,
WishlistVariation,
AddWishlistItemParams,
AddWishlistItemResponse,
DeleteWishlistItemParams,
} from '@plentymarkets/shop-api';
import { Product } from '@plentymarkets/shop-api';

export interface UseWishlistState {
data: WishlistItem[];
loading: boolean;
wishlistItemIds: string[];
wishlistItemIds: WishlistVariation;
}

export type FetchWishlist = () => Promise<WishlistItem[]>;
export type AddWishlistItem = (params: AddWishlistItemParams) => Promise<AddWishlistItemResponse>;
export type DeleteWishlistItem = (params: DeleteWishlistItemParams) => Promise<boolean>;
export type IsWishlistItem = (variationId: number) => boolean;
export type InteractWithWishlist = (variationId: number, quantity: number) => Promise<void>;
export type SetWishlistItemIds = (wishlistItemIds: string[]) => void;
export type CanBeDirectlyAddedToCart = (variationId: number) => boolean;
export type ProductCanBeAddedToWishlist = (product: Product) => boolean;
export type InteractWithWishlist = (variationId: number, quantity: number, isTrulyInWishlist: boolean) => Promise<void>;
export type SetWishlistItemIds = (wishlistItemIds: WishlistVariation) => void;

export interface UseWishlist {
data: Readonly<Ref<UseWishlistState['data']>>;
Expand All @@ -26,6 +30,8 @@ export interface UseWishlist {
addWishlistItem: AddWishlistItem;
deleteWishlistItem: DeleteWishlistItem;
isWishlistItem: IsWishlistItem;
canBeDirectlyAddedToCart: CanBeDirectlyAddedToCart;
productCanBeAddedToWishlist: ProductCanBeAddedToWishlist;
setWishlistItemIds: SetWishlistItemIds;
interactWithWishlist: InteractWithWishlist;
}
Expand Down
Loading