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

Commit

Permalink
Update sdk state initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
orzechdev committed May 20, 2020
1 parent dcddba6 commit f269df3
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 212 deletions.
4 changes: 4 additions & 0 deletions src/@sdk/api/Auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export class AuthAPI extends ErrorListener {
);
}

await this.jobsManager.run("checkout", "provideCheckout", {
isUserSignedIn: !!data?.user,
});

return {
data,
dataError,
Expand Down
1 change: 1 addition & 0 deletions src/@sdk/api/Auth/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum FunctionErrorAuthTypes {}
export enum DataErrorAuthTypes {
"SIGN_IN",
"GET_USER",
}
6 changes: 3 additions & 3 deletions src/@sdk/api/Checkout/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PaymentGateway } from "@sdk/fragments/gqlTypes/PaymentGateway";
import { ErrorListener } from "@sdk/helpers";
import {
ICheckoutModel,
Expand All @@ -12,7 +13,6 @@ import {
DataErrorCheckoutTypes,
FunctionErrorCheckoutTypes,
IAddress,
IAvailablePaymentGateways,
IAvailableShippingMethods,
ICheckout,
ICreditCard,
Expand All @@ -28,7 +28,7 @@ export class SaleorCheckoutAPI extends ErrorListener {
selectedShippingAddressId?: string;
selectedBillingAddressId?: string;
availableShippingMethods?: IAvailableShippingMethods;
availablePaymentGateways?: IAvailablePaymentGateways;
availablePaymentGateways?: PaymentGateway[];
payment?: IPayment;

private saleorState: SaleorState;
Expand Down Expand Up @@ -95,7 +95,7 @@ export class SaleorCheckoutAPI extends ErrorListener {
);
this.saleorState.subscribeToChange(
StateItems.PAYMENT_GATEWAYS,
(paymentGateways: IAvailablePaymentGateways) => {
(paymentGateways: PaymentGateway[]) => {
console.log("StateItems.PAYMENT_GATEWAYS", paymentGateways);
this.availablePaymentGateways = paymentGateways;
}
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 @@ -27,7 +27,6 @@ export interface IAddress {
}

export type IAvailableShippingMethods = Checkout_availableShippingMethods[];
export type IAvailablePaymentGateways = GetShopPaymentGateways_shop_availablePaymentGateways[];

export interface IShippingMethod {
id: string;
Expand Down
11 changes: 6 additions & 5 deletions src/@sdk/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@ export class SaleorAPI {

const localStorageHandler = new LocalStorageHandler();
const apolloClientManager = new ApolloClientManager(client);
const saleorState = new SaleorState(
const jobsManager = new JobsManager(
localStorageHandler,
apolloClientManager
);
const localStorageManager = new LocalStorageManager(
const saleorState = new SaleorState(
localStorageHandler,
saleorState
apolloClientManager,
jobsManager
);
const jobsManager = new JobsManager(
const localStorageManager = new LocalStorageManager(
localStorageHandler,
apolloClientManager
saleorState
);

if (onStateUpdate) {
Expand Down
5 changes: 0 additions & 5 deletions src/@sdk/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ export function setAuthToken(token: string) {
dispatchEvent(authEvent);
}

export function removeAuthToken() {
localStorage.removeItem("token");
dispatchEvent(authEvent);
}

export function clearStorage(): void {
localStorage.clear();
dispatchEvent(authEvent);
Expand Down
62 changes: 31 additions & 31 deletions src/@sdk/data/ApolloClientManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ApolloClient from "apollo-client";
import { Checkout } from "@sdk/fragments/gqlTypes/Checkout";
import { OrderDetail } from "@sdk/fragments/gqlTypes/OrderDetail";
import { Payment } from "@sdk/fragments/gqlTypes/Payment";
import { PaymentGateway } from "@sdk/fragments/gqlTypes/PaymentGateway";
import { User } from "@sdk/fragments/gqlTypes/User";
import { CountryCode } from "@sdk/gqlTypes/globalTypes";
import {
Expand Down Expand Up @@ -94,6 +95,23 @@ export class ApolloClientManager {
.subscribe(value => next(value.data?.me), error, complete);
};

subscribeToPaymentGatewaysChange = (
next: (value: PaymentGateway[] | null) => void,
error?: (error: any) => void,
complete?: () => void
) => {
this.client
.watchQuery<GetShopPaymentGateways, any>({
fetchPolicy: "cache-only",
query: ShopQueries.getShopPaymentGateways,
})
.subscribe(
value => next(value.data.shop?.availablePaymentGateways),
error,
complete
);
};

getUser = async () => {
const { data, errors } = await this.client.query<UserDetails, any>({
fetchPolicy: "network-only",
Expand Down Expand Up @@ -342,41 +360,23 @@ export class ApolloClientManager {
};

getPaymentGateways = async () => {
let paymentGateways:
| GetShopPaymentGateways_shop_availablePaymentGateways[]
| null;
try {
paymentGateways = await new Promise((resolve, reject) => {
const observable = this.client.watchQuery<GetShopPaymentGateways, any>({
fetchPolicy: "network-only",
query: ShopQueries.getShopPaymentGateways,
});
observable.subscribe(
result => {
const { data, errors } = result;
if (errors?.length) {
reject(errors);
} else {
resolve(data.shop.availablePaymentGateways);
}
},
error => {
reject(error);
}
);
});
const { data, errors } = await this.client.query<
GetShopPaymentGateways,
any
>({
fetchPolicy: "network-only",
query: ShopQueries.getShopPaymentGateways,
});

if (paymentGateways) {
return {
data: paymentGateways,
};
}
} catch (error) {
if (errors?.length) {
return {
error,
error: errors,
};
} else {
return {
data: data.shop.availablePaymentGateways,
};
}
return {};
};

createCheckout = async (
Expand Down
35 changes: 35 additions & 0 deletions src/@sdk/fragments/gqlTypes/PaymentGateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* tslint:disable */
/* eslint-disable */
// This file was automatically generated and should not be edited.

// ====================================================
// GraphQL fragment: PaymentGateway
// ====================================================

export interface PaymentGateway_config {
__typename: "GatewayConfigLine";
/**
* Gateway config key.
*/
field: string;
/**
* Gateway config value for key.
*/
value: string | null;
}

export interface PaymentGateway {
__typename: "PaymentGateway";
/**
* Payment gateway ID.
*/
id: string;
/**
* Payment gateway name.
*/
name: string;
/**
* Payment gateway client configuration.
*/
config: PaymentGateway_config[];
}
12 changes: 12 additions & 0 deletions src/@sdk/fragments/shop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import gql from "graphql-tag";

export const paymentGatewayFragment = gql`
fragment PaymentGateway on PaymentGateway {
id
name
config {
field
value
}
}
`;
35 changes: 18 additions & 17 deletions src/@sdk/jobs/Auth/AuthJobs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DataErrorCheckoutTypes } from "@sdk/api/Checkout/types";
import { ApolloClientManager } from "@sdk/data/ApolloClientManager";
import { LocalStorageHandler } from "@sdk/helpers/LocalStorageHandler";
import {
Expand All @@ -6,7 +7,6 @@ import {
} from "@temp/@sdk/api/Auth/types";

import { JobRunResponse } from "../types";
import { DataErrorCheckoutTypes } from "@temp/@sdk/api/Checkout/types";

export type PromiseAuthJobRunResponse = Promise<
JobRunResponse<
Expand All @@ -27,6 +27,23 @@ export class AuthJobs {
this.localStorageHandler = localStorageHandler;
}

provideUser = async (): PromiseAuthJobRunResponse => {
const { data, error } = await this.apolloClientManager.getUser();

if (error) {
return {
dataError: {
error,
type: DataErrorAuthTypes.GET_USER,
},
};
}

return {
data,
};
};

signIn = async ({
email,
password,
Expand All @@ -50,22 +67,6 @@ export class AuthJobs {

this.localStorageHandler.setSignInToken(data?.token || null);

const {
data: checkoutData,
error: checkoutError,
} = await this.apolloClientManager.getCheckout(true, null);

if (checkoutError) {
return {
dataError: {
error: checkoutError,
type: DataErrorCheckoutTypes.GET_CHECKOUT,
},
};
} else if (checkoutData) {
this.localStorageHandler.setCheckout(checkoutData);
}

return {
data,
};
Expand Down
45 changes: 45 additions & 0 deletions src/@sdk/jobs/Checkout/CheckoutJobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,51 @@ export class CheckoutJobs {
this.localStorageHandler = localStorageHandler;
}

provideCheckout = async ({
isUserSignedIn,
}: {
isUserSignedIn: boolean;
}): PromiseCheckoutJobRunResponse => {
const checkout = this.localStorageHandler.getCheckout();

const { data, error } = await this.apolloClientManager.getCheckout(
isUserSignedIn,
checkout?.token
);

if (error) {
return {
dataError: {
error,
type: DataErrorCheckoutTypes.GET_CHECKOUT,
},
};
} else {
this.localStorageHandler.setCheckout(data || checkout);

return {
data,
};
}
};

providePaymentGateways = async (): PromiseCheckoutJobRunResponse => {
const { data, error } = await this.apolloClientManager.getPaymentGateways();

if (error) {
return {
dataError: {
error,
type: DataErrorCheckoutTypes.GET_PAYMENT_GATEWAYS,
},
};
}

return {
data,
};
};

createCheckout = async ({
email,
lines,
Expand Down
2 changes: 1 addition & 1 deletion src/@sdk/mutations/gqlTypes/TokenAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export interface TokenAuth_tokenCreate {
export interface TokenAuth {
/**
* Mutation that authenticates a user and returns token and user data.
*
*
* It overrides the default graphql_jwt.ObtainJSONWebToken to wrap potential
* authentication errors in our Error type, which is consistent to how the rest of
* the mutation works.
Expand Down
9 changes: 3 additions & 6 deletions src/@sdk/queries/shop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gql from "graphql-tag";
import { paymentGatewayFragment } from "../fragments/shop";

export const getShop = gql`
query GetShop {
Expand All @@ -23,15 +24,11 @@ export const getShop = gql`
`;

export const getShopPaymentGateways = gql`
${paymentGatewayFragment}
query GetShopPaymentGateways {
shop {
availablePaymentGateways {
id
name
config {
field
value
}
...PaymentGateway
}
}
}
Expand Down
Loading

0 comments on commit f269df3

Please sign in to comment.