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

Commit

Permalink
Update config usage in sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
orzechdev committed May 21, 2020
1 parent f269df3 commit 83a7cc1
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 101 deletions.
5 changes: 1 addition & 4 deletions src/@next/pages/CartPage/CartPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const generateCart = (
export const CartPage: React.FC<IProps> = ({}: IProps) => {
const history = useHistory();
const { user } = useAuth();
const chObj = useCheckout();
const { checkout } = useCheckout();
const {
loaded,
removeItem,
Expand All @@ -135,9 +135,6 @@ export const CartPage: React.FC<IProps> = ({}: IProps) => {
discount,
} = useCart();

console.log("chObj", chObj);
const checkout = chObj.checkout;

const shippingTaxedPrice =
checkout?.shippingMethod?.id && shippingPrice
? {
Expand Down
20 changes: 1 addition & 19 deletions src/@sdk/api/Auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ export class AuthAPI extends ErrorListener {
private saleorState: SaleorState;
private jobsManager: JobsManager;

constructor(
saleorState: SaleorState,
loadOnStart: boolean,
jobsManager: JobsManager
) {
constructor(saleorState: SaleorState, jobsManager: JobsManager) {
super();
this.saleorState = saleorState;
this.jobsManager = jobsManager;
Expand All @@ -58,25 +54,11 @@ export class AuthAPI extends ErrorListener {
}
);

// if (loadOnStart) {
// this.load();

if (!this.saleorState.signInToken && window.PasswordCredential) {
this.autoSignIn();
}
// }
}

// /**
// * Initialize data, retrieve initial cache or make initial fetches. If not changed in SDK config, called on start by default.
// */
// load = () => {
// this.saleorState.provideSignInToken();
// return {
// pending: false,
// };
// };

/**
* Tries to authenticate user with given email and password.
* @param email Email used for authentication.
Expand Down
20 changes: 0 additions & 20 deletions src/@sdk/api/Cart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class SaleorCartAPI extends ErrorListener {
localStorageManager: LocalStorageManager,
apolloClientManager: ApolloClientManager,
saleorState: SaleorState,
loadOnStart: boolean,
jobsManager: JobsManager
) {
super();
Expand Down Expand Up @@ -80,22 +79,9 @@ export class SaleorCartAPI extends ErrorListener {
this.loaded = loaded.checkout && loaded.summaryPrices;
}
);

// if (loadOnStart) {
// this.load();
// }
}

// load = async () => {
// await this.saleorState.provideCheckout(this.fireError, true);
// return {
// pending: false,
// };
// };

addItem = async (variantId: string, quantity: number) => {
// await this.saleorState.provideCheckout(this.fireError);

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

Expand Down Expand Up @@ -129,8 +115,6 @@ export class SaleorCartAPI extends ErrorListener {
};

removeItem = async (variantId: string) => {
// await this.saleorState.provideCheckout(this.fireError);

// 1. save in local storage
this.localStorageManager.removeItemFromCart(variantId);
// 2. save online if possible (if checkout id available)
Expand Down Expand Up @@ -163,8 +147,6 @@ export class SaleorCartAPI extends ErrorListener {
};

subtractItem = async (variantId: string) => {
// await this.saleorState.provideCheckout(this.fireError);

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

Expand Down Expand Up @@ -198,8 +180,6 @@ export class SaleorCartAPI extends ErrorListener {
};

updateItem = async (variantId: string, quantity: number) => {
// await this.saleorState.provideCheckout(this.fireError);

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

Expand Down
32 changes: 1 addition & 31 deletions src/@sdk/api/Checkout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ export class SaleorCheckoutAPI extends ErrorListener {
private saleorState: SaleorState;
private jobsManager: JobsManager;

constructor(
saleorState: SaleorState,
loadOnStart: boolean,
jobsManager: JobsManager
) {
constructor(saleorState: SaleorState, jobsManager: JobsManager) {
super();
this.saleorState = saleorState;
this.jobsManager = jobsManager;
Expand All @@ -61,7 +57,6 @@ export class SaleorCheckoutAPI extends ErrorListener {
shippingMethod,
promoCodeDiscount,
} = checkout || {};
console.log("StateItems.CHECKOUT", id);
this.checkout = {
billingAddress,
email,
Expand All @@ -84,7 +79,6 @@ export class SaleorCheckoutAPI extends ErrorListener {
StateItems.PAYMENT,
(payment: IPaymentModel) => {
const { id, token, gateway, creditCard } = payment || {};
console.log("StateItems.PAYMENT", id);
this.payment = {
creditCard,
gateway,
Expand All @@ -96,38 +90,22 @@ export class SaleorCheckoutAPI extends ErrorListener {
this.saleorState.subscribeToChange(
StateItems.PAYMENT_GATEWAYS,
(paymentGateways: PaymentGateway[]) => {
console.log("StateItems.PAYMENT_GATEWAYS", paymentGateways);
this.availablePaymentGateways = paymentGateways;
}
);
this.saleorState.subscribeToChange(
StateItems.LOADED,
(loaded: SaleorStateLoaded) => {
console.log("StateItems.LOADED", loaded);
this.loaded =
loaded.checkout && loaded.payment && loaded.paymentGateways;
}
);

// if (loadOnStart) {
// this.load();
// }
}

// load = async () => {
// await this.saleorState.provideCheckout(this.fireError, true);
// await this.saleorState.providePayment(true);
// await this.saleorState.providePaymentGateways(this.fireError);
// return {
// pending: false,
// };
// };

setShippingAddress = async (
shippingAddress: IAddress,
email: string
): PromiseRunResponse<DataErrorCheckoutTypes, FunctionErrorCheckoutTypes> => {
// await this.saleorState.provideCheckout(this.fireError);
const checkoutId = this.saleorState.checkout?.id;
const alteredLines = this.saleorState.checkout?.lines?.map(item => ({
quantity: item!.quantity,
Expand Down Expand Up @@ -185,7 +163,6 @@ export class SaleorCheckoutAPI extends ErrorListener {
billingAddress: IAddress,
email?: string
): PromiseRunResponse<DataErrorCheckoutTypes, FunctionErrorCheckoutTypes> => {
// await this.saleorState.provideCheckout(this.fireError);
const checkoutId = this.saleorState.checkout?.id;
const isShippingRequiredForProducts = this.saleorState.checkout?.lines
?.filter(line => line.quantity > 0)
Expand Down Expand Up @@ -292,7 +269,6 @@ export class SaleorCheckoutAPI extends ErrorListener {
DataErrorCheckoutTypes,
FunctionErrorCheckoutTypes
> => {
// await this.saleorState.provideCheckout(this.fireError);
const checkoutId = this.saleorState.checkout?.id;

if (checkoutId && this.checkout?.shippingAddress) {
Expand Down Expand Up @@ -328,7 +304,6 @@ export class SaleorCheckoutAPI extends ErrorListener {
setShippingMethod = async (
shippingMethodId: string
): PromiseRunResponse<DataErrorCheckoutTypes, FunctionErrorCheckoutTypes> => {
// await this.saleorState.provideCheckout(this.fireError);
const checkoutId = this.saleorState.checkout?.id;

if (checkoutId) {
Expand Down Expand Up @@ -361,7 +336,6 @@ export class SaleorCheckoutAPI extends ErrorListener {
addPromoCode = async (
promoCode: string
): PromiseRunResponse<DataErrorCheckoutTypes, FunctionErrorCheckoutTypes> => {
// await this.saleorState.provideCheckout(this.fireError);
const checkoutId = this.saleorState.checkout?.id;

if (checkoutId) {
Expand Down Expand Up @@ -395,7 +369,6 @@ export class SaleorCheckoutAPI extends ErrorListener {
removePromoCode = async (
promoCode: string
): PromiseRunResponse<DataErrorCheckoutTypes, FunctionErrorCheckoutTypes> => {
// await this.saleorState.provideCheckout(this.fireError);
const checkoutId = this.saleorState.checkout?.id;

if (checkoutId) {
Expand Down Expand Up @@ -428,8 +401,6 @@ export class SaleorCheckoutAPI extends ErrorListener {
token: string,
creditCard?: ICreditCard
): PromiseRunResponse<DataErrorCheckoutTypes, FunctionErrorCheckoutTypes> => {
// await this.saleorState.provideCheckout(this.fireError);
// await this.saleorState.providePayment();
const checkoutId = this.saleorState.checkout?.id;
const billingAddress = this.saleorState.checkout?.billingAddress;
const amount = this.saleorState.summaryPrices?.totalPrice?.gross.amount;
Expand Down Expand Up @@ -474,7 +445,6 @@ export class SaleorCheckoutAPI extends ErrorListener {
DataErrorCheckoutTypes,
FunctionErrorCheckoutTypes
> => {
// await this.saleorState.provideCheckout(this.fireError);
const checkoutId = this.saleorState.checkout?.id;

if (checkoutId) {
Expand Down
1 change: 0 additions & 1 deletion src/@sdk/api/Checkout/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ICheckoutModelPriceValue,
IPaymentCreditCard,
} from "@sdk/helpers/LocalStorageHandler";
import { GetShopPaymentGateways_shop_availablePaymentGateways } from "@sdk/queries/gqlTypes/GetShopPaymentGateways";

export type IPrice = ICheckoutModelPrice | null | undefined;
export type IPriceValue = ICheckoutModelPriceValue | null | undefined;
Expand Down
11 changes: 3 additions & 8 deletions src/@sdk/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export class SaleorAPI {
...config?.loadOnStart,
},
};
const { loadOnStart } = finalConfig;

const localStorageHandler = new LocalStorageHandler();
const apolloClientManager = new ApolloClientManager(client);
Expand All @@ -50,6 +49,7 @@ export class SaleorAPI {
apolloClientManager
);
const saleorState = new SaleorState(
finalConfig,
localStorageHandler,
apolloClientManager,
jobsManager
Expand All @@ -63,17 +63,12 @@ export class SaleorAPI {
saleorState.subscribeToNotifiedChanges(onStateUpdate);
}

this.auth = new AuthAPI(saleorState, loadOnStart.auth, jobsManager);
this.checkout = new SaleorCheckoutAPI(
saleorState,
loadOnStart.checkout,
jobsManager
);
this.auth = new AuthAPI(saleorState, jobsManager);
this.checkout = new SaleorCheckoutAPI(saleorState, jobsManager);
this.cart = new SaleorCartAPI(
localStorageManager,
apolloClientManager,
saleorState,
loadOnStart.cart,
jobsManager
);

Expand Down
1 change: 0 additions & 1 deletion src/@sdk/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Config } from "./types";
export const defaultConfig: Config = {
loadOnStart: {
auth: true,
cart: true,
checkout: true,
},
};
5 changes: 1 addition & 4 deletions src/@sdk/data/ApolloClientManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ import {
CheckoutProductVariants,
CheckoutProductVariants_productVariants,
} from "@sdk/queries/gqlTypes/CheckoutProductVariants";
import {
GetShopPaymentGateways,
GetShopPaymentGateways_shop_availablePaymentGateways,
} from "@sdk/queries/gqlTypes/GetShopPaymentGateways";
import { GetShopPaymentGateways } from "@sdk/queries/gqlTypes/GetShopPaymentGateways";
import { UserCheckoutDetails } from "@sdk/queries/gqlTypes/UserCheckoutDetails";
import { UserDetails } from "@sdk/queries/gqlTypes/UserDetails";
import * as ShopQueries from "@sdk/queries/shop";
Expand Down
29 changes: 19 additions & 10 deletions src/@sdk/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
LocalStorageItems,
} from "../helpers/LocalStorageHandler";
import { JobsManager } from "../jobs";
import { Config } from "../types";
import { ISaleorStateSummeryPrices, StateItems } from "./types";

export interface SaleorStateLoaded {
Expand Down Expand Up @@ -51,6 +52,7 @@ export class SaleorState extends NamedObservable<StateItems> {
private jobsManager: JobsManager;

constructor(
config: Config,
localStorageHandler: LocalStorageHandler,
apolloClientManager: ApolloClientManager,
jobsManager: JobsManager
Expand All @@ -63,7 +65,7 @@ export class SaleorState extends NamedObservable<StateItems> {
this.loaded = defaultSaleorStateLoaded;

this.subscribeStateToChanges();
this.initializeState();
this.initializeState(config);
}

/**
Expand Down Expand Up @@ -96,14 +98,22 @@ export class SaleorState extends NamedObservable<StateItems> {
/**
* Initialize class members with cached or fetched data.
*/
private initializeState = async () => {
this.onSignInTokenUpdate(this.localStorageHandler.getSignInToken());
await this.jobsManager.run("auth", "provideUser", undefined);
await this.jobsManager.run("checkout", "provideCheckout", {
isUserSignedIn: !!this.user,
});
this.onPaymentUpdate(this.localStorageHandler.getPayment());
await this.jobsManager.run("checkout", "providePaymentGateways", undefined);
private initializeState = async (config: Config) => {
if (config.loadOnStart.auth) {
this.onSignInTokenUpdate(this.localStorageHandler.getSignInToken());
await this.jobsManager.run("auth", "provideUser", undefined);
}
if (config.loadOnStart.checkout) {
await this.jobsManager.run("checkout", "provideCheckout", {
isUserSignedIn: !!this.user,
});
this.onPaymentUpdate(this.localStorageHandler.getPayment());
await this.jobsManager.run(
"checkout",
"providePaymentGateways",
undefined
);
}
};

private onLoadedUpdate = (newLoaded: Partial<SaleorStateLoaded>) => {
Expand All @@ -129,7 +139,6 @@ export class SaleorState extends NamedObservable<StateItems> {
};
private onUserUpdate = (user: User | null) => {
this.user = user;
console.log("update", user);
this.notifyChange(StateItems.USER, this.user);
this.onLoadedUpdate({
user: true,
Expand Down
1 change: 0 additions & 1 deletion src/@sdk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ export interface Config {
loadOnStart: {
auth: boolean;
checkout: boolean;
cart: boolean;
};
}
3 changes: 1 addition & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,10 @@ const startApp = async () => {
}
}, [updateAvailable]);

const { authenticated, user } = useAuth();
const { authenticated } = useAuth();
const [prevAuthenticated, setPrevAuthenticated] = React.useState<
boolean | undefined
>();
console.log(user);

React.useEffect(() => {
if (prevAuthenticated !== undefined && authenticated !== undefined) {
Expand Down

0 comments on commit 83a7cc1

Please sign in to comment.