Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Refactor SDK checkout error handling #709

Merged
merged 11 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
625 changes: 377 additions & 248 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
"typescript": "^3.8.2",
"webapp-webpack-plugin": "^2.2.0",
"webpack": "^4.42.1",
"webpack-cli": "^3.1.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.11.0",
"webpack-merge": "^4.2.1",
"workbox-webpack-plugin": "^4.3.1"
Expand Down
41 changes: 21 additions & 20 deletions src/@sdk/api/Cart/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { LocalStorageManager } from "@sdk/data";
import { ErrorListener } from "@sdk/helpers";
import { ICheckoutModel } from "@sdk/helpers/LocalStorageHandler";
import { JobsManager } from "@sdk/jobs";
import { ErrorCartTypes } from "@sdk/jobs/Cart";
import { NetworkManager } from "@sdk/network";
import { CheckoutRepositoryManager, ICheckoutModel } from "@sdk/repository";
import { SaleorState } from "@sdk/state";
import { ISaleorStateSummeryPrices, StateItems } from "@sdk/state/types";
import { ApolloClientManager } from "@temp/@sdk/data/ApolloClientManager";

import {
IDiscount,
Expand All @@ -26,22 +27,22 @@ export class SaleorCartAPI extends ErrorListener implements ISaleorCartAPI {
private checkoutLoaded: boolean;
private summaryPricesLoaded: boolean;

private checkoutRepositoryManager: CheckoutRepositoryManager;
private localStorageManager: LocalStorageManager;
private saleorState: SaleorState;
private networkManager: NetworkManager;
private apolloClientManager: ApolloClientManager;
private jobsManager: JobsManager;

constructor(
checkoutRepositoryManager: CheckoutRepositoryManager,
networkManager: NetworkManager,
localStorageManager: LocalStorageManager,
apolloClientManager: ApolloClientManager,
saleorState: SaleorState,
loadOnStart: boolean,
jobsManager: JobsManager
) {
super();
this.saleorState = saleorState;
this.checkoutRepositoryManager = checkoutRepositoryManager;
this.networkManager = networkManager;
this.localStorageManager = localStorageManager;
this.apolloClientManager = apolloClientManager;
this.jobsManager = jobsManager;

this.loaded = false;
Expand Down Expand Up @@ -103,21 +104,21 @@ export class SaleorCartAPI extends ErrorListener implements ISaleorCartAPI {
await this.saleorState.provideCheckout(this.fireError);

// 1. save in local storage
this.checkoutRepositoryManager.addItemToCart(variantId, quantity);
this.localStorageManager.addItemToCart(variantId, quantity);

// 2. save online if possible (if checkout id available)
if (this.saleorState.checkout?.lines) {
const {
data,
error,
} = await this.networkManager.getRefreshedCheckoutLines(
} = await this.apolloClientManager.getRefreshedCheckoutLines(
this.saleorState.checkout.lines
);

if (error) {
this.fireError(error, ErrorCartTypes.SET_CART_ITEM);
} else {
this.checkoutRepositoryManager.getRepository().setCheckout({
this.localStorageManager.getHandler().setCheckout({
...this.saleorState.checkout,
lines: data,
});
Expand All @@ -138,20 +139,20 @@ export class SaleorCartAPI extends ErrorListener implements ISaleorCartAPI {
await this.saleorState.provideCheckout(this.fireError);

// 1. save in local storage
this.checkoutRepositoryManager.removeItemFromCart(variantId);
this.localStorageManager.removeItemFromCart(variantId);
// 2. save online if possible (if checkout id available)
if (this.saleorState.checkout?.lines) {
const {
data,
error,
} = await this.networkManager.getRefreshedCheckoutLines(
} = await this.apolloClientManager.getRefreshedCheckoutLines(
this.saleorState.checkout.lines
);

if (error) {
this.fireError(error, ErrorCartTypes.SET_CART_ITEM);
} else {
this.checkoutRepositoryManager.getRepository().setCheckout({
this.localStorageManager.getHandler().setCheckout({
...this.saleorState.checkout,
lines: data,
});
Expand All @@ -172,21 +173,21 @@ export class SaleorCartAPI extends ErrorListener implements ISaleorCartAPI {
await this.saleorState.provideCheckout(this.fireError);

// 1. save in local storage
this.checkoutRepositoryManager.subtractItemFromCart(variantId);
this.localStorageManager.subtractItemFromCart(variantId);

// 2. save online if possible (if checkout id available)
if (this.saleorState.checkout?.lines) {
const {
data,
error,
} = await this.networkManager.getRefreshedCheckoutLines(
} = await this.apolloClientManager.getRefreshedCheckoutLines(
this.saleorState.checkout.lines
);

if (error) {
this.fireError(error, ErrorCartTypes.SET_CART_ITEM);
} else {
this.checkoutRepositoryManager.getRepository().setCheckout({
this.localStorageManager.getHandler().setCheckout({
...this.saleorState.checkout,
lines: data,
});
Expand All @@ -207,21 +208,21 @@ export class SaleorCartAPI extends ErrorListener implements ISaleorCartAPI {
await this.saleorState.provideCheckout(this.fireError);

// 1. save in local storage
this.checkoutRepositoryManager.updateItemInCart(variantId, quantity);
this.localStorageManager.updateItemInCart(variantId, quantity);

// 2. save online if possible (if checkout id available)
if (this.saleorState.checkout?.lines) {
const {
data,
error,
} = await this.networkManager.getRefreshedCheckoutLines(
} = await this.apolloClientManager.getRefreshedCheckoutLines(
this.saleorState.checkout.lines
);

if (error) {
this.fireError(error, ErrorCartTypes.SET_CART_ITEM);
} else {
this.checkoutRepositoryManager.getRepository().setCheckout({
this.localStorageManager.getHandler().setCheckout({
...this.saleorState.checkout,
lines: data,
});
Expand Down
2 changes: 1 addition & 1 deletion src/@sdk/api/Cart/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
ICheckoutModelLine,
ICheckoutModelPrice,
ICheckoutModelPriceValue,
} from "@sdk/repository";
} from "@sdk/helpers/LocalStorageHandler";

import { PromiseQueuedResponse } from "../types";

Expand Down
5 changes: 4 additions & 1 deletion src/@sdk/api/Checkout/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ErrorListener } from "@sdk/helpers";
import {
ICheckoutModel,
IPaymentModel,
} from "@sdk/helpers/LocalStorageHandler";
import { JobsManager } from "@sdk/jobs";
import { ICheckoutModel, IPaymentModel } from "@sdk/repository";
import { SaleorState } from "@sdk/state";
import { StateItems } from "@sdk/state/types";

Expand Down
4 changes: 2 additions & 2 deletions src/@sdk/api/Checkout/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Checkout_availableShippingMethods } from "@sdk/fragments/gqlTypes/Checkout";
import { GetShopPaymentGateways_shop_availablePaymentGateways } from "@sdk/queries/gqlTypes/GetShopPaymentGateways";
import {
ICheckoutModelPrice,
ICheckoutModelPriceValue,
IPaymentCreditCard,
} from "@sdk/repository";
} from "@sdk/helpers/LocalStorageHandler";
import { GetShopPaymentGateways_shop_availablePaymentGateways } from "@sdk/queries/gqlTypes/GetShopPaymentGateways";

import { PromiseQueuedResponse, PromiseRunResponse } from "../types";

Expand Down
33 changes: 20 additions & 13 deletions src/@sdk/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import ApolloClient from "apollo-client";

import { defaultConfig } from "../config";
import { LocalStorageManager } from "../data";
import { ApolloClientManager } from "../data/ApolloClientManager";
import { LocalStorageHandler } from "../helpers/LocalStorageHandler";
import { JobsManager } from "../jobs";
import { NetworkManager } from "../network";
import { CheckoutRepositoryManager, LocalRepository } from "../repository";
import { SaleorState } from "../state";
import { Config } from "../types";
import { APIProxy } from "./APIProxy";
Expand Down Expand Up @@ -40,14 +41,20 @@ export class SaleorAPI {
};
const { loadOnStart } = finalConfig;

const repository = new LocalRepository();
const networkManager = new NetworkManager(client);
const saleorState = new SaleorState(repository, networkManager);
const checkoutRepositoryManager = new CheckoutRepositoryManager(
repository,
const localStorageHandler = new LocalStorageHandler();
const apolloClientManager = new ApolloClientManager(client);
const saleorState = new SaleorState(
localStorageHandler,
apolloClientManager
);
const localStorageManager = new LocalStorageManager(
localStorageHandler,
saleorState
);
const jobsManager = new JobsManager(repository, networkManager);
const jobsManager = new JobsManager(
localStorageHandler,
apolloClientManager
);

if (onStateUpdate) {
saleorState.subscribeToNotifiedChanges(onStateUpdate);
Expand All @@ -59,18 +66,18 @@ export class SaleorAPI {
jobsManager
);
this.cart = new SaleorCartAPI(
checkoutRepositoryManager,
networkManager,
localStorageManager,
apolloClientManager,
saleorState,
loadOnStart.cart,
jobsManager
);

this.legacyAPIProxy.attachAuthListener(authenticated => {
if (!authenticated) {
repository.setCheckout({});
repository.setPayment({});
repository.setJobs(null);
localStorageHandler.setCheckout({});
localStorageHandler.setPayment({});
localStorageHandler.setJobs(null);
krzysztofwolski marked this conversation as resolved.
Show resolved Hide resolved
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { Checkout } from "@sdk/fragments/gqlTypes/Checkout";
import { OrderDetail } from "@sdk/fragments/gqlTypes/OrderDetail";
import { Payment } from "@sdk/fragments/gqlTypes/Payment";
import { CountryCode } from "@sdk/gqlTypes/globalTypes";
import {
ICheckoutAddress,
ICheckoutModel,
ICheckoutModelLine,
IOrderModel,
IPaymentModel,
} from "@sdk/helpers/LocalStorageHandler";
import * as CheckoutMutations from "@sdk/mutations/checkout";
import {
AddCheckoutPromoCode,
Expand Down Expand Up @@ -58,18 +65,11 @@ import {
} from "@sdk/queries/gqlTypes/GetShopPaymentGateways";
import { UserCheckoutDetails } from "@sdk/queries/gqlTypes/UserCheckoutDetails";
import * as ShopQueries from "@sdk/queries/shop";
import {
ICheckoutAddress,
ICheckoutModel,
ICheckoutModelLine,
IOrderModel,
IPaymentModel,
} from "@sdk/repository";
import { filterNotEmptyArrayItems } from "@sdk/utils";

import { INetworkManager } from "./types";
import { IApolloClientManager } from "./types";

export class NetworkManager implements INetworkManager {
export class ApolloClientManager implements IApolloClientManager {
private client: ApolloClient<any>;

constructor(client: ApolloClient<any>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ICheckoutModelLine,
IOrderModel,
IPaymentModel,
} from "@sdk/repository";
} from "@temp/@sdk/helpers/LocalStorageHandler";

export enum PendingSaveItems {
UPDATE_CART = "updateCart",
Expand All @@ -21,61 +21,61 @@ export interface ApolloErrorWithUserInput extends ApolloError {
};
}

export interface INetworkManagerResponse<T> {
export interface IApolloClientManagerResponse<T> {
data?: T;
error?: ApolloErrorWithUserInput;
}

export interface INetworkManager {
export interface IApolloClientManager {
getCheckout: (
checkoutToken: string | null
) => Promise<INetworkManagerResponse<ICheckoutModel>>;
) => Promise<IApolloClientManagerResponse<ICheckoutModel>>;
getRefreshedCheckoutLines: (
checkoutlines: ICheckoutModelLine[] | null
) => Promise<INetworkManagerResponse<ICheckoutModelLine[]>>;
) => Promise<IApolloClientManagerResponse<ICheckoutModelLine[]>>;
createCheckout: (
email: string,
lines: Array<{ variantId: string; quantity: number }>,
shippingAddress: ICheckoutAddress,
billingAddress?: ICheckoutAddress
) => Promise<INetworkManagerResponse<ICheckoutModel>>;
) => Promise<IApolloClientManagerResponse<ICheckoutModel>>;
setCartItem: (
checkout: ICheckoutModel
) => Promise<INetworkManagerResponse<ICheckoutModel>>;
) => Promise<IApolloClientManagerResponse<ICheckoutModel>>;
setBillingAddress: (
billingAddress: ICheckoutAddress,
checkoutId: string
) => Promise<INetworkManagerResponse<ICheckoutModel>>;
) => Promise<IApolloClientManagerResponse<ICheckoutModel>>;
setBillingAddressWithEmail: (
billingAddress: ICheckoutAddress,
email: string,
checkoutId: string
) => Promise<INetworkManagerResponse<ICheckoutModel>>;
) => Promise<IApolloClientManagerResponse<ICheckoutModel>>;
setShippingAddress: (
shippingAddress: ICheckoutAddress,
email: string,
checkoutId: string
) => Promise<INetworkManagerResponse<ICheckoutModel>>;
) => Promise<IApolloClientManagerResponse<ICheckoutModel>>;
setShippingMethod: (
shippingMethodId: string,
checkoutId: string
) => Promise<INetworkManagerResponse<ICheckoutModel>>;
) => Promise<IApolloClientManagerResponse<ICheckoutModel>>;
addPromoCode: (
promoCode: string,
checkoutId: string
) => Promise<INetworkManagerResponse<ICheckoutModel>>;
) => Promise<IApolloClientManagerResponse<ICheckoutModel>>;
removePromoCode: (
promoCode: string,
checkoutId: string
) => Promise<INetworkManagerResponse<ICheckoutModel>>;
) => Promise<IApolloClientManagerResponse<ICheckoutModel>>;
createPayment: (
amount: number,
checkoutId: string,
paymentGateway: string,
paymentToken: string,
billingAddress: ICheckoutAddress
) => Promise<INetworkManagerResponse<IPaymentModel>>;
) => Promise<IApolloClientManagerResponse<IPaymentModel>>;
completeCheckout: (
checkoutId: string
) => Promise<INetworkManagerResponse<IOrderModel>>;
) => Promise<IApolloClientManagerResponse<IOrderModel>>;
}
Loading