Skip to content

Commit

Permalink
Rename "useUserOrders" to "useUserOrder"
Browse files Browse the repository at this point in the history
  • Loading branch information
filipsobol committed Feb 25, 2021
1 parent 218d784 commit 1fbc5d7
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion packages/boilerplate/composables/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export { default as useProduct } from './useProduct';
export { default as useReview } from './useReview';
export { default as useUser } from './useUser';
export { default as useUserBilling } from './useUserBilling';
export { default as useUserOrders } from './useUserOrders';
export { default as useUserOrder } from './useUserOrder';
export { default as useUserShipping } from './useUserShipping';
export { default as useWishlist } from './useWishlist';
export * from './getters';
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import {
Context,
useUserOrdersFactory,
UseUserOrdersFactoryParams
useUserOrderFactory,
UseUserOrderFactoryParams
} from '@vue-storefront/core';
import { OrdersResponse, OrderSearchParams } from '../types';

const params: UseUserOrdersFactoryParams<OrdersResponse, OrderSearchParams> = {
const params: UseUserOrderFactoryParams<OrdersResponse, OrderSearchParams> = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
searchOrders: async (context: Context, params: OrderSearchParams): Promise<OrdersResponse> => {
console.log('Mocked: searchOrders');
Expand All @@ -19,4 +19,4 @@ const params: UseUserOrdersFactoryParams<OrdersResponse, OrderSearchParams> = {
}
};

export default useUserOrdersFactory<OrdersResponse, OrderSearchParams>(params);
export default useUserOrderFactory<OrdersResponse, OrderSearchParams>(params);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import useUserOrders from '../../src/useUserOrders';
import useUserOrder from '../../src/useUserOrder';

jest.mock('@vue-storefront/commercetools-api', () => ({
getOrders: jest.fn(async () => ({
Expand All @@ -11,7 +11,7 @@ jest.mock('@vue-storefront/commercetools-api', () => ({
}));

jest.mock('@vue-storefront/core', () => ({
useUserOrdersFactory: ({ searchOrders }) => () => ({ search: searchOrders })
useUserOrderFactory: ({ searchOrders }) => () => ({ search: searchOrders })
}));

const context = {
Expand All @@ -28,13 +28,13 @@ const context = {
}
};

describe('[commercetools-composables] useUserOrders', () => {
describe('[commercetools-composables] useUserOrder', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('loads user orders with criteria', async () => {
const { search } = useUserOrders() as any;
const { search } = useUserOrder() as any;

const response = await search(context, { param: 'param1' });

Expand All @@ -43,7 +43,7 @@ describe('[commercetools-composables] useUserOrders', () => {
});

it('loads user all orders', async () => {
const { search } = useUserOrders() as any;
const { search } = useUserOrder() as any;

const response = await search(context);

Expand All @@ -54,7 +54,7 @@ describe('[commercetools-composables] useUserOrders', () => {
it('loads user orders with empty response', async () => {
(context.$ct.api.getOrders as jest.Mock).mockReturnValue({ data: null });

const { search } = useUserOrders() as any;
const { search } = useUserOrder() as any;

const response = await search(context, { param: 'param1' });

Expand Down
2 changes: 1 addition & 1 deletion packages/commercetools/composables/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export { default as useReview } from './useReview';
export { default as useShipping } from './useShipping';
export { default as useUser } from './useUser';
export { default as useUserBilling } from './useUserBilling';
export { default as useUserOrders } from './useUserOrders';
export { default as useUserOrder } from './useUserOrder';
export { default as useUserShipping } from './useUserShipping';
export { default as useWishlist } from './useWishlist';
export * from './getters';
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useUserOrdersFactory, UseUserOrdersFactoryParams, Context } from '@vue-storefront/core';
import { useUserOrderFactory, UseUserOrderFactoryParams, Context } from '@vue-storefront/core';
import { Order } from '../types/GraphQL';
import { OrderSearchParams } from '../types';

const params: UseUserOrdersFactoryParams<Order[], OrderSearchParams> = {
const params: UseUserOrderFactoryParams<Order[], OrderSearchParams> = {
searchOrders: async (context: Context, { customQuery, ...searchParams } = {}): Promise<Order[]> => {
const result = await context.$ct.api.getOrders(searchParams, customQuery);
const { results: data } = result.data?.me.orders || { results: [], total: 0 };
return data;
}
};

export default useUserOrdersFactory<Order[], OrderSearchParams>(params);
export default useUserOrderFactory<Order[], OrderSearchParams>(params);
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { UseUserOrders } from '../../src/types';
import { UseUserOrdersFactoryParams, useUserOrdersFactory } from '../../src/factories';
import { UseUserOrder } from '../../src/types';
import { UseUserOrderFactoryParams, useUserOrderFactory } from '../../src/factories';
import { Ref } from '@vue/composition-api';

let useUserOrders: () => UseUserOrders<Readonly<Ref<Readonly<any>>>, any>;
let params: UseUserOrdersFactoryParams<any, any>;
let useUserOrder: () => UseUserOrder<Readonly<Ref<Readonly<any>>>, any>;
let params: UseUserOrderFactoryParams<any, any>;

function createComposable(): void {
params = {
searchOrders: jest.fn().mockResolvedValueOnce(['first', 'second'])
};
useUserOrders = useUserOrdersFactory<any, any>(params);
useUserOrder = useUserOrderFactory<any, any>(params);
}

describe('[CORE - factories] useUserOrderFactory', () => {
Expand All @@ -20,7 +20,7 @@ describe('[CORE - factories] useUserOrderFactory', () => {

describe('initial setup', () => {
it('should have proper initial props', () => {
const { loading, orders } = useUserOrders();
const { loading, orders } = useUserOrder();
expect(loading.value).toEqual(false);
expect(orders.value).toEqual([]);
});
Expand All @@ -29,7 +29,7 @@ describe('[CORE - factories] useUserOrderFactory', () => {
describe('methods', () => {
describe('search', () => {
it('should set search results', async () => {
const { search, orders } = useUserOrders();
const { search, orders } = useUserOrder();
await search({});
expect(orders.value).toEqual(['first', 'second']);
});
Expand All @@ -39,7 +39,7 @@ describe('[CORE - factories] useUserOrderFactory', () => {
params.searchOrders = jest.fn().mockImplementationOnce(() => {
throw err;
});
const { search, loading, orders, error } = useUserOrders();
const { search, loading, orders, error } = useUserOrder();
await search({});
expect(error.value.search).toBe(err);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/src/factories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export * from './useReviewFactory';
export * from './useShippingFactory';
export * from './useUserBillingFactory';
export * from './useUserFactory';
export * from './useUserOrdersFactory';
export * from './useUserOrderFactory';
export * from './useUserShippingFactory';
export * from './useWishlistFactory';
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { Ref, computed } from '@vue/composition-api';
import { CustomQuery, UseUserOrders, Context, FactoryParams, UseUserOrdersErrors } from '../types';
import { CustomQuery, UseUserOrder, Context, FactoryParams, UseUserOrderErrors } from '../types';
import { sharedRef, Logger, generateContext } from '../utils';

export interface UseUserOrdersFactoryParams<ORDERS, ORDER_SEARCH_PARAMS> extends FactoryParams {
export interface UseUserOrderFactoryParams<ORDERS, ORDER_SEARCH_PARAMS> extends FactoryParams {
searchOrders: (context: Context, params: ORDER_SEARCH_PARAMS & { customQuery?: CustomQuery }) => Promise<ORDERS>;
}

export function useUserOrdersFactory<ORDERS, ORDER_SEARCH_PARAMS>(factoryParams: UseUserOrdersFactoryParams<ORDERS, ORDER_SEARCH_PARAMS>) {
return function useUserOrders(): UseUserOrders<ORDERS, ORDER_SEARCH_PARAMS> {
const orders: Ref<ORDERS> = sharedRef([], 'useUserOrders-orders');
const loading: Ref<boolean> = sharedRef(false, 'useUserOrders-loading');
export function useUserOrderFactory<ORDERS, ORDER_SEARCH_PARAMS>(factoryParams: UseUserOrderFactoryParams<ORDERS, ORDER_SEARCH_PARAMS>) {
return function useUserOrder(): UseUserOrder<ORDERS, ORDER_SEARCH_PARAMS> {
const orders: Ref<ORDERS> = sharedRef([], 'useUserOrder-orders');
const loading: Ref<boolean> = sharedRef(false, 'useUserOrder-loading');
const context = generateContext(factoryParams);
const error: Ref<UseUserOrdersErrors> = sharedRef({}, 'useUserOrders-error');
const error: Ref<UseUserOrderErrors> = sharedRef({}, 'useUserOrder-error');

const search = async (searchParams): Promise<void> => {
Logger.debug('useUserOrders.search', searchParams);
Logger.debug('useUserOrder.search', searchParams);

try {
loading.value = true;
error.value.search = null;
orders.value = await factoryParams.searchOrders(context, searchParams);
} catch (err) {
error.value.search = err;
Logger.error('useUserOrders/search', err);
Logger.error('useUserOrder/search', err);
} finally {
loading.value = false;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,20 @@ export interface UseUser
error: ComputedProperty<UseUserErrors>;
}

export interface UseUserOrdersSearchParams {
export interface UseUserOrderSearchParams {
id?: any;
page?: number;
perPage?: number;
[x: string]: any;
}
export interface UseUserOrdersErrors {
export interface UseUserOrderErrors {
search?: Error;
}
export interface UseUserOrders<ORDERS, ORDER_SEARCH_PARAMS> {
export interface UseUserOrder<ORDERS, ORDER_SEARCH_PARAMS> {
orders: ComputedProperty<ORDERS>;
search(params: ComposableFunctionArgs<ORDER_SEARCH_PARAMS>): Promise<void>;
loading: ComputedProperty<boolean>;
error: ComputedProperty<UseUserOrdersErrors>;
error: ComputedProperty<UseUserOrderErrors>;
}

export interface UseUserAddress<ADDRESS> {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/docs/commercetools/composables.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ const customQuery = (query, variables) => {
search({ id: '12345', customQuery })
```

Use it for: `useProduct`, `useCategory`, `useUser`, `useUserOrders` methods.
Use it for: `useProduct`, `useCategory`, `useUser`, `useUserOrder` methods.
:::
2 changes: 1 addition & 1 deletion packages/core/docs/composables/use-user.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Use `useUser` to:
If you want to fetch/save other user data you should use the following composables:
- [`useUserBilling`](./use-user-billing.md)
- [`useUserShipping`](./use-user-shipping.md)
- [`useUserOrders`](./use-user-orders.md)
- [`useUserOrder`](./use-user-order.md)

## How to use it in your project?

Expand Down
4 changes: 2 additions & 2 deletions packages/core/docs/contributing/api-design-philosophy.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ cacheManager.setTags((tags) => {
"Separating concerns by files is as effective as separating school friendships by desks. Concerns are “separated” when there is no coupling: changing A wouldn’t break B. Increasing the distance without addressing the coupling only makes it easier to add bugs.
~ Dan Abramov
```
8. Composables should be independent and rely on each other only if they are from the same group (`useUser` `useUserOrders`). The only exception is `useUser` that has to be used in many other composables.
8. Composables should be independent and rely on each other only if they are from the same group (`useUser` `useUserOrder`). The only exception is `useUser` that has to be used in many other composables.
9. If you introduce a new feature shared across all/many composables (like Logging/cache) users should be able to configure this feature from core/core nuxt module.
```ts
// every function in composables is using logger
Expand Down Expand Up @@ -64,7 +64,7 @@ const removeFromCart = async (product: CART_ITEM, customQuery?: CustomQuery) =>

We try to cover each subdomain of the eCommerce domain with a dedicated composable. For example we have a composable for Users Management domain, inventory domain, product catalog domain etc. If you have to add a new feature always think about business domain it correlates to and based on that decide if it should be a new composable or an existing one.

If composables share the same category/prefix it means that they most likely also share the same context eg. `useUserOrders` `useUserShipping` `useUserBilling` are all subcomposables of `useUser` and their content depends on this composable.
If composables share the same category/prefix it means that they most likely also share the same context eg. `useUserOrder` `useUserShipping` `useUserBilling` are all subcomposables of `useUser` and their content depends on this composable.

Each composable has usually 3 pieces:
- main data object (eg `products`)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/docs/core/composables.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ While API Client is a data layer for the application composables are the busines
- `useCategory` to fetch categories
- `useCart` to manage basket
- `useUser` to manage authorization and user profile
- `useUserOrders` (subcomposable to `useUser`) to manage user orders that has already been placed
- `useUserOrder` (subcomposable to `useUser`) to manage user orders that has already been placed
- `useUserAddresses` (subcomposable to `useUser`) to manage user shipping addresses
- `useCheckout` to manage order processing

Expand Down
2 changes: 1 addition & 1 deletion packages/core/docs/general/key-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Vue Storefront integrations are exposing following composables:
- [useUser](/composables/use-user) for managing user sessions, credentials and registration
- [useUserShipping](/composables/use-user-shipping) for managing shipping addresses
- [useUserBilling](/composables/use-user-billing) for managing billing addresses
- **useUserOrders** for managing past and active user orders
- **useUserOrder** for managing past and active user orders

#### Shopping Cart
- [useCart](/composables/use-cart) for loading the cart, adding/removing products and discounts
Expand Down
2 changes: 1 addition & 1 deletion packages/core/docs/migrate/integrators-2.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ We have changes a bit the naming and signatures of core factory functions. Below
| useUserBillingFactory | setDefault | setDefaultAddress | context: Context, params: { address: Readonly<USER_BILLING_ITEM>; shipping: Readonly<USER_BILLING>; }) | No changes |
| useUserShippingFactory | setDefault | setDefaultAddress | context: Context, params: { address: Readonly<USER_SHIPPING_ITEM>; shipping: Readonly<USER_SHIPPING>; }) | No changes |
| useUserFactory | loadUser | load | context: Context, | context: Context, params?: {} |
| useUserOrdersFactory | searchOrders | No changes | context: Context, params: ORDER_SEARCH_PARAMS, customQuery?: CustomQuery | context: Context, params: ORDER_SEARCH_PARAMS & { customQuery?: CustomQuery } |
| useUserOrderFactory | searchOrders | No changes | context: Context, params: ORDER_SEARCH_PARAMS, customQuery?: CustomQuery | context: Context, params: ORDER_SEARCH_PARAMS & { customQuery?: CustomQuery } |
| useWishlistFactory | addToWishlist | addItem | context, { currentWishlist: WISHLIST, product: PRODUCT }, customQuery | context, { currentWishlist: WISHLIST, product: PRODUCT, customQuery } |
| useWishlistFactory | loadWishlist | load | context: Context, customQuery?: CustomQuery | No changes |
| useWishlistFactory | removeFromWishlist | removeItem | context: Context, params: { currentWishlist: WISHLIST, product: WISHLIST_ITEM }, customQuery?: CustomQuery | context: Context, params: { currentWishlist: WISHLIST, product: WISHLIST_ITEM, customQuery?: CustomQuery } |
Expand Down
2 changes: 1 addition & 1 deletion packages/core/docs/migrate/projects-2.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ We have changes a bit the naming and signatures of composable functions. Below i
| useUser | logout | () | () |
| useUser | changePassword | (currentPassword: string, newPassword: string) | ({ currentPassword: string, newPassword: string }) |
| useUser | load | () | () |
| useUserOrders | searchOrders | (searchParams: ORDER_SEARCH_PARAMS, customQuery?: CustomQuery) | ({ ...searchParams: ORDER_SEARCH_PARAMS, customQuery?: CustomQuery } = {}) |
| useUserOrder | searchOrders | (searchParams: ORDER_SEARCH_PARAMS, customQuery?: CustomQuery) | ({ ...searchParams: ORDER_SEARCH_PARAMS, customQuery?: CustomQuery } = {}) |
| useWishlist | addToWishlist | (product: PRODUCT, customQuery?: CustomQuery) | ({ product: PRODUCT, customQuery?: CustomQuery }) |
| useWishlist | removeFromWishlist | (product: WISHLIST_ITEM, customQuery?: CustomQuery) | ({ product: WISHLIST_ITEM, customQuery?: CustomQuery }) |
| useWishlist | load | (customQuery?: CustomQuery) | ({ customQuery?: CustomQuery } = {}) |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# useUserOrders composable
# useUserOrder composable

`useUserOrders` composition API function is responsible, as it's name suggests for interactions with user's order history from your eCommerce. This function returns following values:
`useUserOrder` composition API function is responsible, as it's name suggests for interactions with user's order history from your eCommerce. This function returns following values:

- `searchOrders` - a main querying function that is used to query user's order history from eCommerce platform and populate the `orders` object with the result. Every time you invoke this function API request is made. This method accepts a single `params` object.
- `orders` - a main data object that contains an array of orders fetched by `searchOrders` method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ import {
SfProperty
} from '@storefront-ui/vue';
import { computed, ref } from '@vue/composition-api';
import { useUserOrders, orderGetters } from '<%= options.generate.replace.composables %>';
import { useUserOrder, orderGetters } from '<%= options.generate.replace.composables %>';
import { AgnosticOrderStatus } from '@vue-storefront/core';
import { onSSR } from '@vue-storefront/core';
Expand All @@ -118,7 +118,7 @@ export default {
SfProperty
},
setup() {
const { orders, search } = useUserOrders();
const { orders, search } = useUserOrder();
const currentOrder = ref(null);
onSSR(async () => {
Expand Down

0 comments on commit 1fbc5d7

Please sign in to comment.