From 737612656ff09ba1842cd374f4d20903eb1df577 Mon Sep 17 00:00:00 2001 From: GunaTrika Date: Wed, 6 Oct 2021 17:28:19 +0530 Subject: [PATCH] Adding checkout files --- .../api/endpoints/customer/address.ts | 1 + .../api/endpoints/customer/card.ts | 1 + .../customer/address/use-add-item.tsx | 15 ++++++++ .../customer/address/use-addresses.tsx | 35 +++++++++++++++++++ .../customer/card/use-add-item.tsx | 15 ++++++++ .../elasticpath/customer/card/use-cards.tsx | 33 +++++++++++++++++ framework/elasticpath/index.tsx | 29 ++------------- framework/elasticpath/provider.ts | 17 ++++++++- 8 files changed, 119 insertions(+), 27 deletions(-) create mode 100644 framework/elasticpath/api/endpoints/customer/address.ts create mode 100644 framework/elasticpath/api/endpoints/customer/card.ts create mode 100644 framework/elasticpath/customer/address/use-add-item.tsx create mode 100644 framework/elasticpath/customer/address/use-addresses.tsx create mode 100644 framework/elasticpath/customer/card/use-add-item.tsx create mode 100644 framework/elasticpath/customer/card/use-cards.tsx diff --git a/framework/elasticpath/api/endpoints/customer/address.ts b/framework/elasticpath/api/endpoints/customer/address.ts new file mode 100644 index 000000000..491bf0ac9 --- /dev/null +++ b/framework/elasticpath/api/endpoints/customer/address.ts @@ -0,0 +1 @@ +export default function noopApi(...args: any[]): void {} diff --git a/framework/elasticpath/api/endpoints/customer/card.ts b/framework/elasticpath/api/endpoints/customer/card.ts new file mode 100644 index 000000000..491bf0ac9 --- /dev/null +++ b/framework/elasticpath/api/endpoints/customer/card.ts @@ -0,0 +1 @@ +export default function noopApi(...args: any[]): void {} diff --git a/framework/elasticpath/customer/address/use-add-item.tsx b/framework/elasticpath/customer/address/use-add-item.tsx new file mode 100644 index 000000000..ac9dcd5cf --- /dev/null +++ b/framework/elasticpath/customer/address/use-add-item.tsx @@ -0,0 +1,15 @@ +import useAddItem, { UseAddItem } from '@commerce/customer/address/use-add-item' +import { MutationHook } from '@commerce/utils/types' + +export default useAddItem as UseAddItem + +export const handler: MutationHook = { + fetchOptions: { + query: '', + }, + async fetcher({ input, options, fetch }) {}, + useHook: + ({ fetch }) => + () => + async () => ({}), +} diff --git a/framework/elasticpath/customer/address/use-addresses.tsx b/framework/elasticpath/customer/address/use-addresses.tsx new file mode 100644 index 000000000..e9ddc7001 --- /dev/null +++ b/framework/elasticpath/customer/address/use-addresses.tsx @@ -0,0 +1,35 @@ +import type { GetAddressesHook } from '@commerce/types/customer/address' + +import { useMemo } from 'react' +import { SWRHook } from '@commerce/utils/types' +import useAddresses, { + UseAddresses, +} from '@commerce/customer/address/use-addresses' + +export default useAddresses as UseAddresses + +export const handler: SWRHook = { + fetchOptions: { + url: '/api/customer/address', + method: 'GET', + }, + useHook: ({ useData }) => + function useHook(input) { + const response = useData({ + swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, + }) + + return useMemo( + () => + Object.create(response, { + isEmpty: { + get() { + return (response.data?.length ?? 0) <= 0 + }, + enumerable: true, + }, + }), + [response] + ) + }, +} diff --git a/framework/elasticpath/customer/card/use-add-item.tsx b/framework/elasticpath/customer/card/use-add-item.tsx new file mode 100644 index 000000000..7e3afa9c5 --- /dev/null +++ b/framework/elasticpath/customer/card/use-add-item.tsx @@ -0,0 +1,15 @@ +import useAddItem, { UseAddItem } from '@commerce/customer/card/use-add-item' +import { MutationHook } from '@commerce/utils/types' + +export default useAddItem as UseAddItem + +export const handler: MutationHook = { + fetchOptions: { + query: '', + }, + async fetcher({ input, options, fetch }) {}, + useHook: + ({ fetch }) => + () => + async () => ({}), +} diff --git a/framework/elasticpath/customer/card/use-cards.tsx b/framework/elasticpath/customer/card/use-cards.tsx new file mode 100644 index 000000000..92236deb2 --- /dev/null +++ b/framework/elasticpath/customer/card/use-cards.tsx @@ -0,0 +1,33 @@ +import type { GetCardsHook } from '@commerce/types/customer/card' + +import { useMemo } from 'react' +import { SWRHook } from '@commerce/utils/types' +import useCard, { UseCards } from '@commerce/customer/card/use-cards' + +export default useCard as UseCards + +export const handler: SWRHook = { + fetchOptions: { + url: '/api/customer/card', + method: 'GET', + }, + useHook: ({ useData }) => + function useHook(input) { + const response = useData({ + swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, + }) + + return useMemo( + () => + Object.create(response, { + isEmpty: { + get() { + return (response.data?.length ?? 0) <= 0 + }, + enumerable: true, + }, + }), + [response] + ) + }, +} diff --git a/framework/elasticpath/index.tsx b/framework/elasticpath/index.tsx index 8dd625332..5cb935fa2 100644 --- a/framework/elasticpath/index.tsx +++ b/framework/elasticpath/index.tsx @@ -1,7 +1,6 @@ -import type { ReactNode } from 'react' import { CommerceConfig, - CommerceProvider as CoreCommerceProvider, + getCommerceProvider, useCommerce as useCoreCommerce, } from '@commerce' import { elasticpathProvider } from './provider' @@ -10,27 +9,5 @@ import type { ElasticpathProvider } from './provider' export { elasticpathProvider } export type { ElasticpathProvider } -export const elasticpathConfig: CommerceConfig = { - locale: 'en-us', - cartCookie: 'bc_cartId', -} - -export type ElasticpathConfig = Partial - -export type ElasticpathProps = { - children?: ReactNode - locale: string -} & ElasticpathConfig - -export function CommerceProvider({ children, ...config }: ElasticpathProps) { - return ( - - {children} - - ) -} - -export const useCommerce = () => useCoreCommerce() \ No newline at end of file +export const CommerceProvider = getCommerceProvider(elasticpathProvider) +export const useCommerce = () => useCoreCommerce() diff --git a/framework/elasticpath/provider.ts b/framework/elasticpath/provider.ts index 8be96445d..7a224de29 100644 --- a/framework/elasticpath/provider.ts +++ b/framework/elasticpath/provider.ts @@ -14,6 +14,10 @@ import { handler as useLogin } from './auth/use-login' import { handler as useLogout } from './auth/use-logout' import { handler as useSignup } from './auth/use-signup' +import { handler as useCheckout } from './checkout/use-checkout' +import { handler as useCards } from './customer/card/use-cards' +import { handler as useAddresses } from './customer/address/use-addresses' + import {fetcher} from './fetcher' export const elasticpathProvider = { @@ -26,7 +30,18 @@ export const elasticpathProvider = { useAddItem: useWishlistAddItem, useRemoveItem: useWishlistRemoveItem, }, - customer: { useCustomer }, + checkout: { + useCheckout, + }, + customer: { + useCustomer, + card: { + useCards + }, + address: { + useAddresses + } + }, products: { useSearch }, auth: { useLogin, useLogout, useSignup }, }