Skip to content

Commit 38bb44d

Browse files
authored
feat!: make loading and error properties readonly in the useWishlist composable (#843)
1 parent 607c457 commit 38bb44d

File tree

5 files changed

+138
-75
lines changed

5 files changed

+138
-75
lines changed

docs/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ module.exports = {
100100
['/api-reference/magento-theme.usecategory', 'useCategory()'],
101101
['/api-reference/magento-theme.usecategorysearch', 'useCategorySearch()'],
102102
['/api-reference/magento-theme.useuserorder', 'useUserOrder()'],
103+
['/api-reference/magento-theme.usewishlist', 'useWishlist()'],
103104
]
104105
},
105106
{

packages/theme/composables/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export { default as useConfig } from './useConfig';
1414
export { default as useStore } from './useStore';
1515
export { default as useCurrency } from './useCurrency';
1616
export { default as useExternalCheckout } from './useExternalCheckout';
17-
export { default as useWishlist } from './useWishlist';
17+
export * from './useWishlist';
1818
export { default as useUser } from './useUser';
1919
export { default as useGuestUser } from './useGuestUser';
2020
export { default as useForgotPassword } from './useForgotPassword';

packages/theme/composables/useUiNotification/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const timeToLive = 3000;
2424

2525
const useUiNotification = () => {
2626
const { app } = useContext();
27+
// @ts-ignore
2728
const cookieMessage = app.$vsf.$magento.config.state.getMessage<UiNotification>();
2829

2930
const send = (notification: UiNotification) => {
@@ -34,6 +35,7 @@ const useUiNotification = () => {
3435

3536
if (index !== -1) state.notifications.splice(index, 1);
3637

38+
// @ts-ignore
3739
app.$vsf.$magento.config.state.removeMessage();
3840
};
3941

@@ -57,7 +59,10 @@ const useUiNotification = () => {
5759
};
5860

5961
if (cookieMessage) {
62+
// @ts-ignore
63+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
6064
send(cookieMessage);
65+
// @ts-ignore
6166
app.$vsf.$magento.config.state.removeMessage();
6267
}
6368

packages/theme/composables/useWishlist/index.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
import { ref, useContext } from '@nuxtjs/composition-api';
1+
import { readonly, ref, useContext } from '@nuxtjs/composition-api';
2+
import { findItemOnWishlist } from '~/composables/useWishlist/helpers';
23
import { Logger } from '~/helpers/logger';
3-
import { CustomQuery } from '~/composables/types';
44
import { useCustomerStore } from '~/stores/customer';
5-
import { findItemOnWishlist } from '~/composables/useWishlist/helpers';
6-
import { UseWishlist, UseWishlistErrors, Wishlist } from '~/composables/useWishlist/useWishlist';
7-
8-
export const useWishlist = (): UseWishlist => {
5+
import type { Wishlist } from '~/modules/GraphQL/types';
6+
import type {
7+
UseWishlistAddItemParams,
8+
UseWishlistErrors,
9+
UseWishlistInterface,
10+
UseWishlistIsInWishlistParams,
11+
UseWishlistLoadParams,
12+
UseWishlistRemoveItemParams,
13+
} from '~/composables/useWishlist/useWishlist';
14+
15+
/**
16+
* The `useWishlist()` composable allows loading and manipulating wishlist of the current user.
17+
*
18+
* See the {@link UseWishlistInterface} page for more information.
19+
*/
20+
export function useWishlist(): UseWishlistInterface {
921
const customerStore = useCustomerStore();
1022
const { app } = useContext();
1123
const loading = ref(false);
12-
const wishlist = ref(customerStore.wishlist);
1324
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
1425
const calculateWishlistTotal = (wishlists) => wishlists.reduce((prev, next) => (prev?.items_count ?? 0) + (next?.items_count ?? 0), 0);
1526
const error = ref<UseWishlistErrors>({
@@ -20,14 +31,8 @@ export const useWishlist = (): UseWishlist => {
2031
loadItemsCount: null,
2132
});
2233

23-
const load = async (params: {
24-
searchParams?: Partial<{
25-
currentPage: number;
26-
pageSize: number;
27-
}>,
28-
customQuery?: CustomQuery,
29-
// eslint-disable-next-line consistent-return
30-
}) => {
34+
// eslint-disable-next-line consistent-return
35+
const load = async (params: UseWishlistLoadParams) => {
3136
Logger.debug('useWishlist/load');
3237

3338
try {
@@ -58,7 +63,7 @@ export const useWishlist = (): UseWishlist => {
5863
}
5964
};
6065

61-
const isInWishlist = ({ product }) => {
66+
const isInWishlist = ({ product }: UseWishlistIsInWishlistParams) => {
6267
Logger.debug('useWishlist/isInWishlist', product);
6368

6469
const wishlistProduct = findItemOnWishlist(customerStore.wishlist, product);
@@ -71,22 +76,22 @@ export const useWishlist = (): UseWishlist => {
7176
Logger.debug('useWishlist/setWishlist', newWishlist);
7277
};
7378

74-
const removeItem = async ({ product, params }) => {
79+
const removeItem = async ({ product, customQuery }: UseWishlistRemoveItemParams) => {
7580
Logger.debug('useWishlist/removeItem', product);
7681

7782
try {
7883
loading.value = true;
7984
Logger.debug('[Magento Storefront]: useWishlist.removeItem params->', {
8085
currentWishlist: customerStore.wishlist,
8186
product,
82-
customQuery: params?.customQuery,
87+
customQuery,
8388
});
8489

8590
const itemOnWishlist = findItemOnWishlist(customerStore.wishlist, product);
8691
const { data } = await app.context.$vsf.$magento.api.removeProductsFromWishlist({
8792
id: '0',
8893
items: [itemOnWishlist.id],
89-
}, params?.customQuery);
94+
}, customQuery);
9095

9196
Logger.debug('[Result]:', { data });
9297
error.value.removeItem = null;
@@ -130,7 +135,7 @@ export const useWishlist = (): UseWishlist => {
130135
};
131136

132137
// eslint-disable-next-line consistent-return
133-
const addItem = async ({ product, customQuery }) => {
138+
const addItem = async ({ product, customQuery }: UseWishlistAddItemParams) => {
134139
Logger.debug('useWishlist/addItem', product);
135140

136141
try {
@@ -151,7 +156,6 @@ export const useWishlist = (): UseWishlist => {
151156
if (itemOnWishlist) {
152157
return await removeItem({
153158
product,
154-
params: {},
155159
});
156160
}
157161

@@ -252,17 +256,17 @@ export const useWishlist = (): UseWishlist => {
252256
};
253257

254258
return {
255-
wishlist,
256259
loadItemsCount,
257260
isInWishlist,
258261
addItem,
259262
load,
260263
removeItem,
261264
clear,
262265
setWishlist,
263-
loading,
264-
error,
266+
loading: readonly(loading),
267+
error: readonly(error),
265268
};
266-
};
269+
}
267270

268271
export default useWishlist;
272+
export * from './useWishlist';
Lines changed: 102 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,107 @@
1-
import { Ref } from '@nuxtjs/composition-api';
2-
import { ComposableFunctionArgs } from '~/composables/types';
3-
import {
4-
Maybe, ProductInterface, Scalars, WishlistItem, WishlistItems,
5-
} from '~/modules/GraphQL/types';
6-
7-
export interface Wishlist {
8-
/** The unique ID for a `Wishlist` object */
9-
id?: Maybe<Scalars['ID']>;
10-
/** @deprecated Use field `items_v2` from type `Wishlist` instead */
11-
items?: Maybe<Array<Maybe<WishlistItem>>>;
12-
/** The number of items in the wish list */
13-
items_count?: Maybe<Scalars['Int']>;
14-
/** An array of items in the customer's wish list */
15-
items_v2?: Maybe<WishlistItems>;
16-
/** An encrypted code that Magento uses to link to the wish list */
17-
sharing_code?: Maybe<Scalars['String']>;
18-
/** The time of the last modification to the wish list */
19-
updated_at?: Maybe<Scalars['String']>;
20-
}
1+
import type { Ref, DeepReadonly } from '@nuxtjs/composition-api';
2+
import type { ComposableFunctionArgs } from '~/composables/types';
3+
import type { Wishlist, ProductInterface } from '~/modules/GraphQL/types';
214

5+
/**
6+
* Errors that occured in the `useWishlist` composable
7+
*/
228
export interface UseWishlistErrors {
23-
addItem: Error;
24-
removeItem: Error;
25-
load: Error;
26-
clear: Error;
27-
loadItemsCount: Error;
9+
addItem: Error | null;
10+
removeItem: Error | null;
11+
load: Error | null;
12+
clear: Error | null;
13+
loadItemsCount: Error | null;
2814
}
15+
16+
/**
17+
* Parameters accepted by the `loadItemsCount` method in the `useWishlist` composable
18+
*/
19+
export type UseWishlistLoadItemsCountParams = ComposableFunctionArgs<{
20+
// TODO: Add type
21+
}>;
22+
23+
/**
24+
* Parameters accepted by the `isInWishlist` method in the `useWishlist` composable
25+
*/
26+
export type UseWishlistIsInWishlistParams = { product: ProductInterface };
27+
28+
/**
29+
* Parameters accepted by the `addItem` method in the `useWishlist` composable
30+
*/
31+
export type UseWishlistAddItemParams = ComposableFunctionArgs<{
32+
product: any; // TODO: add product interface
33+
}>;
34+
35+
/**
36+
* Parameters accepted by the `load` method in the `useWishlist` composable
37+
*/
38+
export type UseWishlistLoadParams = ComposableFunctionArgs<{
39+
searchParams?: Partial<{
40+
currentPage: number;
41+
pageSize: number;
42+
}>
43+
}>;
44+
2945
/**
30-
* TODO: add types
31-
*/
32-
export type UseWishlist = {
33-
wishlist: Ref<Wishlist>,
34-
loadItemsCount(params: ComposableFunctionArgs<{}>): Promise<number | null>;
35-
isInWishlist: (params: { product: ProductInterface }) => boolean;
36-
addItem: (
37-
params: ComposableFunctionArgs<{
38-
product: any; // TODO: add product intrface
39-
}>) => Promise<void>;
40-
load: (params: ComposableFunctionArgs<{
41-
searchParams?: Partial<{
42-
currentPage: number;
43-
pageSize: number;
44-
}>,
45-
}>) => Promise<Wishlist>;
46-
removeItem: (
47-
params: ComposableFunctionArgs<{
48-
product: any; // TODO: add product intrface
49-
}>) => Promise<void>;
50-
clear: (params: { currentWishlist: any }) => Promise<any>;
51-
setWishlist: (newWishlist: Wishlist) => void;
52-
loading: Ref<boolean>
53-
error: Ref<UseWishlistErrors>;
46+
* Parameters accepted by the `removeItem` method in the `useWishlist` composable
47+
*/
48+
export type UseWishlistRemoveItemParams = ComposableFunctionArgs<{
49+
product: any; // TODO: add product interface
50+
}>;
51+
52+
/**
53+
* Parameters accepted by the `clear` method in the `useWishlist` composable
54+
*/
55+
export type UseWishlistClearParams = {
56+
currentWishlist: any; // TODO: Add type
5457
};
58+
59+
/**
60+
* Represents the data returned from and functions available in the `useWishlist()` composable.
61+
*/
62+
export interface UseWishlistInterface {
63+
/**
64+
* Returns a total number of items added to the wishlist of the current user
65+
*/
66+
loadItemsCount(params: UseWishlistLoadItemsCountParams): Promise<number | null>;
67+
68+
/**
69+
* Checks if given product is in the wishlist of the current user
70+
*/
71+
isInWishlist(params: UseWishlistIsInWishlistParams): boolean;
72+
73+
/**
74+
* Adds product to the wishlist of the current user
75+
*/
76+
addItem(params: UseWishlistAddItemParams): Promise<void>;
77+
78+
/**
79+
* Fetches wishlist of the current customer
80+
*/
81+
load(params: UseWishlistLoadParams): Promise<Wishlist | void>; // TODO: Why this method returns a Wishlist but others dont?
82+
83+
/**
84+
* Removes product from the wishlist of the current user
85+
*/
86+
removeItem(params: UseWishlistRemoveItemParams): Promise<void>;
87+
88+
/**
89+
* Removes all products from the wishlist of the current user
90+
*/
91+
clear(params: UseWishlistClearParams): Promise<any>;
92+
93+
/**
94+
* Overrides the wishlist of the current user
95+
*/
96+
setWishlist(newWishlist: Wishlist): void;
97+
98+
/**
99+
* Indicates whether any of the methods is in progress
100+
*/
101+
loading: DeepReadonly<Ref<boolean>>;
102+
103+
/**
104+
* Contains errors from any of the composable methods
105+
*/
106+
error: DeepReadonly<Ref<UseWishlistErrors>>;
107+
}

0 commit comments

Comments
 (0)