From 31bc7892172b83f7d0f8fc5b11b05012d99d806f Mon Sep 17 00:00:00 2001 From: Mateusz Kula Date: Thu, 27 Feb 2020 15:46:27 +0100 Subject: [PATCH] Use sdk for fetching shop details --- src/@sdk/index.ts | 2 + src/@sdk/queries/index.ts | 13 ++++ .../queries.ts => @sdk/queries/shop.ts} | 9 +-- src/@sdk/queries/types/GetShop.ts | 78 +++++++++++++++++++ src/@sdk/react/queries.ts | 2 + src/components/ShopProvider/index.tsx | 20 ++--- 6 files changed, 107 insertions(+), 17 deletions(-) rename src/{components/ShopProvider/queries.ts => @sdk/queries/shop.ts} (57%) create mode 100644 src/@sdk/queries/types/GetShop.ts diff --git a/src/@sdk/index.ts b/src/@sdk/index.ts index 89e26e846c..5d7c0c9ae2 100644 --- a/src/@sdk/index.ts +++ b/src/@sdk/index.ts @@ -103,6 +103,8 @@ export class SaleorAPI { data => data.productVariants ); + getShopDetails = this.watchQuery(QUERIES.GetShopDetails, data => data); + setUserDefaultAddress = this.fireQuery( MUTATIONS.AddressTypeUpdate, data => data!.accountSetDefaultAddress diff --git a/src/@sdk/queries/index.ts b/src/@sdk/queries/index.ts index fdd1b39eb7..a4fda7a587 100644 --- a/src/@sdk/queries/index.ts +++ b/src/@sdk/queries/index.ts @@ -11,6 +11,7 @@ import * as Category from "./category"; import * as Checkout from "./checkout"; import * as Orders from "./orders"; import * as Product from "./products"; +import * as Shop from "./shop"; import * as WishlistQuery from "./wishlist"; import { @@ -32,6 +33,8 @@ import { CategoryDetailsVariables, } from "./types/CategoryDetails"; +import { GetShop } from "./types/GetShop"; + import { OrdersByUser, OrdersByUserVariables } from "./types/OrdersByUser"; import { UserCheckoutDetails } from "./types/UserCheckoutDetails"; import { UserDetails } from "./types/UserDetails"; @@ -79,6 +82,16 @@ export const QUERIES = { `, ...options, }), + GetShopDetails: ( + client: ApolloClient, + options: QueryOptions + ): ObservableQuery => + client.watchQuery({ + query: gql` + ${Shop.getShop} + `, + ...options, + }), OrderDetails: ( client: ApolloClient, options: QueryOptions diff --git a/src/components/ShopProvider/queries.ts b/src/@sdk/queries/shop.ts similarity index 57% rename from src/components/ShopProvider/queries.ts rename to src/@sdk/queries/shop.ts index e75660c69e..8fd06ce0d5 100644 --- a/src/components/ShopProvider/queries.ts +++ b/src/@sdk/queries/shop.ts @@ -1,10 +1,7 @@ import gql from "graphql-tag"; -import { TypedQuery } from "../../core/queries"; -import { getShop } from "./types/getShop"; - -const getShopQuery = gql` - query getShop { +export const getShop = gql` + query GetShop { shop { displayGrossPrices defaultCountry { @@ -24,5 +21,3 @@ const getShopQuery = gql` } } `; - -export const TypedGetShopQuery = TypedQuery(getShopQuery); diff --git a/src/@sdk/queries/types/GetShop.ts b/src/@sdk/queries/types/GetShop.ts new file mode 100644 index 0000000000..f51ab5aad9 --- /dev/null +++ b/src/@sdk/queries/types/GetShop.ts @@ -0,0 +1,78 @@ +/* tslint:disable */ +/* eslint-disable */ +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL query operation: GetShop +// ==================================================== + +export interface GetShop_shop_defaultCountry { + __typename: "CountryDisplay"; + /** + * Country code. + */ + code: string; + /** + * Country name. + */ + country: string; +} + +export interface GetShop_shop_countries { + __typename: "CountryDisplay"; + /** + * Country name. + */ + country: string; + /** + * Country code. + */ + code: string; +} + +export interface GetShop_shop_geolocalization_country { + __typename: "CountryDisplay"; + /** + * Country code. + */ + code: string; + /** + * Country name. + */ + country: string; +} + +export interface GetShop_shop_geolocalization { + __typename: "Geolocalization"; + /** + * Country of the user acquired by his IP address. + */ + country: GetShop_shop_geolocalization_country | null; +} + +export interface GetShop_shop { + __typename: "Shop"; + /** + * Display prices with tax in store. + */ + displayGrossPrices: boolean; + /** + * Shop's default country. + */ + defaultCountry: GetShop_shop_defaultCountry | null; + /** + * List of countries available in the shop. + */ + countries: (GetShop_shop_countries | null)[]; + /** + * Customer's geolocalization data. + */ + geolocalization: GetShop_shop_geolocalization | null; +} + +export interface GetShop { + /** + * Return information about the shop. + */ + shop: GetShop_shop | null; +} diff --git a/src/@sdk/react/queries.ts b/src/@sdk/react/queries.ts index c3dfdac6c6..a80d2fc96b 100644 --- a/src/@sdk/react/queries.ts +++ b/src/@sdk/react/queries.ts @@ -3,6 +3,8 @@ import { queryFactory, queryWithVariablesFactory } from "./useQuery"; export const useProductDetails = queryWithVariablesFactory("getProductDetails"); export const useProductList = queryWithVariablesFactory("getProductList"); +export const useShopDetails = queryFactory("getShopDetails"); + export const useUserDetails = queryFactory("getUserDetails"); export const useUserCheckout = queryFactory("getUserCheckout"); diff --git a/src/components/ShopProvider/index.tsx b/src/components/ShopProvider/index.tsx index 78f802ecad..33b2c524fd 100644 --- a/src/components/ShopProvider/index.tsx +++ b/src/components/ShopProvider/index.tsx @@ -2,16 +2,16 @@ import * as React from "react"; import { maybe } from "../../core/utils"; import { defaultContext, ShopContext } from "./context"; -import { TypedGetShopQuery } from "./queries"; -const ShopProvider: React.FC = ({ children }) => ( - - {({ data }) => ( - data.shop, defaultContext)}> - {children} - - )} - -); +import { useShopDetails } from "@sdk/react"; + +const ShopProvider: React.FC = ({ children }) => { + const { data } = useShopDetails(); + return ( + data.shop, defaultContext)}> + {children} + + ); +}; export default ShopProvider;