Skip to content

Commit

Permalink
feat: options for shop list
Browse files Browse the repository at this point in the history
  • Loading branch information
YulikK committed Jun 8, 2024
1 parent 7dac105 commit 7c65f65
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
39 changes: 35 additions & 4 deletions src/shared/API/shopping-list/ShoppingListApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import type {
ShoppingListSetAnonymousIdAction,
} from '@commercetools/platform-sdk';

import { DEFAULT_PAGE, PRODUCT_LIMIT } from '@/shared/constants/product.ts';

import type { OptionsRequest } from '../types/type.ts';

import makeSortRequest from '../product/utils/sort.ts';
import getApiClient, { type ApiClient } from '../sdk/client.ts';

export class ShoppingListApi {
Expand Down Expand Up @@ -92,13 +97,39 @@ export class ShoppingListApi {
return data;
}

public async get(): Promise<ClientResponse<ShoppingListPagedQueryResponse>> {
const data = await this.client.apiRoot().me().shoppingLists().get().execute();
public async get(options?: OptionsRequest): Promise<ClientResponse<ShoppingListPagedQueryResponse>> {
const { limit = PRODUCT_LIMIT, page = DEFAULT_PAGE, sort } = options || {};
const data = await this.client
.apiRoot()
.me()
.shoppingLists()
.get({
queryArgs: {
limit,
offset: (page - 1) * PRODUCT_LIMIT,
...(sort && { sort: makeSortRequest(sort) }),
withTotal: true,
},
})
.execute();
return data;
}

public async getAnonymList(ID: string): Promise<ClientResponse<ShoppingListResponse>> {
const data = await this.client.apiRoot().shoppingLists().withId({ ID }).get().execute();
public async getAnonymList(ID: string, options?: OptionsRequest): Promise<ClientResponse<ShoppingListResponse>> {
const { limit = PRODUCT_LIMIT, page = DEFAULT_PAGE, sort } = options || {};
const data = await this.client
.apiRoot()
.shoppingLists()
.withId({ ID })
.get({
queryArgs: {
limit,
offset: (page - 1) * PRODUCT_LIMIT,
...(sort && { sort: makeSortRequest(sort) }),
withTotal: true,
},
})
.execute();
return data;
}

Expand Down
19 changes: 12 additions & 7 deletions src/shared/API/shopping-list/model/ShoppingListModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import getStore from '@/shared/Store/Store.ts';
import { setAnonymousId, setAnonymousShopListId } from '@/shared/Store/actions.ts';
import { showErrorMessage } from '@/shared/utils/userMessage.ts';

import type { OptionsRequest } from '../../types/type.ts';

import {
isClientResponse,
isErrorResponse,
Expand Down Expand Up @@ -66,8 +68,11 @@ export class ShoppingListModel {
};
}

private async getAnonymousShoppingList(anonymousShopListId: string): Promise<ShoppingList | null> {
const dataAnonymList = await this.root.getAnonymList(anonymousShopListId);
private async getAnonymousShoppingList(
anonymousShopListId: string,
options?: OptionsRequest,
): Promise<ShoppingList | null> {
const dataAnonymList = await this.root.getAnonymList(anonymousShopListId, options);
const anonymShoppingList = this.getShopListFromData(dataAnonymList);
if (anonymShoppingList.id && !dataAnonymList.body.customer?.id) {
this.shoppingList = anonymShoppingList;
Expand All @@ -94,8 +99,8 @@ export class ShoppingListModel {
return cart;
}

private async getUserShoppingLists(): Promise<ShoppingList> {
const data = await this.root.get();
private async getUserShoppingLists(options?: OptionsRequest): Promise<ShoppingList> {
const data = await this.root.get(options);
if (data.body.count === 0) {
const newShopList = await this.root.create();
this.shoppingList = this.getShopListFromData(newShopList);
Expand Down Expand Up @@ -183,14 +188,14 @@ export class ShoppingListModel {
return this.shoppingList;
}

public async getShoppingList(): Promise<ShoppingList> {
public async getShoppingList(options?: OptionsRequest): Promise<ShoppingList> {
if (!this.shoppingList) {
const { anonymousId, anonymousShopListId } = getStore().getState();
if (anonymousShopListId && anonymousId) {
this.shoppingList = await this.getAnonymousShoppingList(anonymousShopListId);
this.shoppingList = await this.getAnonymousShoppingList(anonymousShopListId, options);
}
if (!this.shoppingList) {
this.shoppingList = await this.getUserShoppingLists();
this.shoppingList = await this.getUserShoppingLists(options);
}
}
return this.shoppingList;
Expand Down

0 comments on commit 7c65f65

Please sign in to comment.