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 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 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
25cb55d
Merge branch 'main' of github.com:plentymarkets/storefront-nuxt3-boil…
abocsan-plenty Jan 16, 2025
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
8 changes: 7 additions & 1 deletion apps/web/composables/useCustomer/useCustomer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ export const useCustomer: UseCustomerReturn = () => {
useHandleError(error.value);
state.value.data = data?.value?.data ?? state.value.data;
checkUserState();
useWishlist().setWishlistItemIds(state.value.data?.basket?.itemWishListIds || []);
const wishlistIdsFromSession = state.value.data?.basket?.itemWishListIds || [];
const wishlistIdsJSON = JSON.parse(JSON.stringify(wishlistIdsFromSession));
const wishlistIds = [];
for (const wishlistObject of wishlistIdsJSON) {
wishlistIds.push(wishlistObject.variationId);
}
useWishlist().setWishlistItemIds(wishlistIds || []);
abocsan-plenty marked this conversation as resolved.
Show resolved Hide resolved

state.value.loading = false;
return state.value.data;
Expand Down
13 changes: 11 additions & 2 deletions apps/web/composables/useInitialSetup/useInitialSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const setInitialDataSSR: SetInitialData = async () => {
const { setUser } = useCustomer();
const { setCategoryTree } = useCategoryTree();
const { setCart, loading: cartLoading } = useCart();
const { setWishlistItemIds } = useWishlist();
const { setWishlistItemIds, setWishlistVariationIds } = useWishlist();

cartLoading.value = true;

Expand All @@ -52,7 +52,16 @@ 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 || []);
const wishlistIdsFromSession = data.value.data.session?.basket?.itemWishListIds || [];
const wishlistIdsJSON = JSON.parse(JSON.stringify(wishlistIdsFromSession));
const wishlistIds = [];
const wishlistVariationIds = [];
for (const wishlistObject of wishlistIdsJSON) {
wishlistIds.push(wishlistObject.variationId);
wishlistVariationIds.push(wishlistObject);
}
setWishlistItemIds(wishlistIds || []);
setWishlistVariationIds(wishlistVariationIds || []);
abocsan-plenty marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (error) {
useHandleError(error as ErrorParams);
Expand Down
4 changes: 4 additions & 0 deletions apps/web/composables/useWishlist/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
WishlistItem,
WishlistVariation,
AddWishlistItemParams,
AddWishlistItemResponse,
DeleteWishlistItemParams,
Expand All @@ -9,6 +10,7 @@ export interface UseWishlistState {
data: WishlistItem[];
loading: boolean;
wishlistItemIds: string[];
wishlistVariationIds: WishlistVariation[];
}

export type FetchWishlist = () => Promise<WishlistItem[]>;
Expand All @@ -17,6 +19,7 @@ export type DeleteWishlistItem = (params: DeleteWishlistItemParams) => Promise<b
export type IsWishlistItem = (variationId: number) => boolean;
export type InteractWithWishlist = (variationId: number, quantity: number) => Promise<void>;
export type SetWishlistItemIds = (wishlistItemIds: string[]) => void;
export type SetWishlistVariationIds = (wishlistItems: any[]) => void;
abocsan-plenty marked this conversation as resolved.
Show resolved Hide resolved

export interface UseWishlist {
data: Readonly<Ref<UseWishlistState['data']>>;
Expand All @@ -28,6 +31,7 @@ export interface UseWishlist {
isWishlistItem: IsWishlistItem;
setWishlistItemIds: SetWishlistItemIds;
interactWithWishlist: InteractWithWishlist;
setWishlistVariationIds: SetWishlistVariationIds;
}

export type UseWishlistReturn = () => UseWishlist;
43 changes: 31 additions & 12 deletions apps/web/composables/useWishlist/useWishlist.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AddWishlistItemResponse, WishlistItem } from '@plentymarkets/shop-api';
import type { AddWishlistItemResponse, WishlistItem, WishlistVariation } from '@plentymarkets/shop-api';
import type { AddWishlistItemParams, DeleteWishlistItemParams } from '@plentymarkets/shop-api';
import type {
FetchWishlist,
Expand All @@ -9,6 +9,7 @@ import type {
IsWishlistItem,
InteractWithWishlist,
SetWishlistItemIds,
SetWishlistVariationIds,
} from '~/composables/useWishlist/types';

/**
Expand All @@ -26,8 +27,28 @@ export const useWishlist: UseWishlistReturn = () => {
data: [] as WishlistItem[],
loading: false,
wishlistItemIds: [] as string[],
wishlistVariationIds: [] as WishlistVariation[],
}));

/**
* @description Function for setting the wishlist item ids.
* @return SetWishlistItemIds
* @example
* ``` ts
* setWishlistItemIds(['1', '2']);
* ```
* @param wishlistItemIds
*/
const setWishlistItemIds: SetWishlistItemIds = (wishlistItemIds = []) => {
state.value.wishlistItemIds = wishlistItemIds.map((number) => number.toString());

if (state.value.data.length > 0) {
state.value.data = state.value.data.filter((wishListItem) =>
state.value.wishlistItemIds.includes(wishListItem.variation.id.toString()),
);
}
};

/**
* @description Function for fetching products.
* @return FetchWishlist
Expand All @@ -44,27 +65,24 @@ export const useWishlist: UseWishlistReturn = () => {
.then(({ data }) => {
state.value.data = data ?? state.value.data;
state.value.loading = false;
const itemIds = data ?? state.value.wishlistItemIds;
setWishlistItemIds(itemIds.map((wishlistItem) => wishlistItem.variation.id.toString()));

return state.value.data;
});
};

/**
* @description Function for setting the wishlist item ids.
* @return SetWishlistItemIds
* @description Function for setting the wishlist variation ids.
* @return SetWishlistVariationIds
* @example
* ``` ts
* setWishlistItemIds(['1', '2']);
* setWishlistVariationIds([{variationId: 1, canDirectlyAddToCart: 0}, {variationId: 2, canDirectlyAddToCart: 1}]);
* ```
* @param wishlistItemIds
*/
const setWishlistItemIds: SetWishlistItemIds = (wishlistItemIds = []) => {
state.value.wishlistItemIds = wishlistItemIds.map((number) => number.toString());

if (state.value.data.length > 0) {
state.value.data = state.value.data.filter((wishListItem) =>
state.value.wishlistItemIds.includes(wishListItem.variation.id.toString()),
);
}
const setWishlistVariationIds: SetWishlistVariationIds = (wishlistItems = []) => {
state.value.wishlistVariationIds = wishlistItems.map((item) => item);
};

/**
Expand Down Expand Up @@ -162,6 +180,7 @@ export const useWishlist: UseWishlistReturn = () => {
deleteWishlistItem,
isWishlistItem,
interactWithWishlist,
setWishlistVariationIds,
...toRefs(state.value),
};
};
Loading