-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: updated account, cart, currency and payments hooks
- Loading branch information
Showing
18 changed files
with
253 additions
and
15 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
packages/react-shopper-hooks/src/account/hooks/use-account-addresses.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useElasticPath } from "../../elasticpath" | ||
import { UseQueryOptionsWrapper } from "../../types" | ||
import { AccountAddress, ResourcePage } from "@moltin/sdk" | ||
import { useQuery, UseQueryResult } from "@tanstack/react-query" | ||
import { queryKeysFactory } from "../../shared/util/query-keys-factory" | ||
|
||
const ACCOUNT_ADDRESSES_QUERY_KEY = "account-addresses" as const | ||
|
||
export const accountAddressesQueryKeys = queryKeysFactory( | ||
ACCOUNT_ADDRESSES_QUERY_KEY, | ||
) | ||
type AccountAddressesQueryKey = typeof accountAddressesQueryKeys | ||
|
||
export function useAccountAddresses( | ||
accountId: string, | ||
options?: UseQueryOptionsWrapper< | ||
ResourcePage<AccountAddress>, | ||
Error, | ||
ReturnType<AccountAddressesQueryKey["list"] & string> | ||
> & { ep?: { accountMemberToken?: string } }, | ||
): Partial<ResourcePage<AccountAddress>> & | ||
Omit<UseQueryResult<ResourcePage<AccountAddress>, Error>, "data"> { | ||
const { client } = useElasticPath() | ||
const { data, ...rest } = useQuery({ | ||
queryKey: [...accountAddressesQueryKeys.list({ accountId })], | ||
queryFn: () => | ||
client.AccountAddresses.All({ | ||
account: accountId, | ||
...(options?.ep?.accountMemberToken && { | ||
token: options.ep.accountMemberToken, | ||
}), | ||
}), | ||
...options, | ||
}) | ||
|
||
return { ...data, ...rest } as const | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./hooks/use-account-member" | ||
export * from "./hooks/use-authed-account-member" | ||
export * from "./account-provider" | ||
export * from "./hooks/use-account-addresses" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
export * from "./use-get-cart" | ||
export * from "./use-remove-cart-item" | ||
export * from "./use-update-cart-items" | ||
export * from "./use-add-cart-item" | ||
export * from "./use-add-product" | ||
export * from "./use-add-promotion" | ||
export * from "./use-add-bundle-product-to-cart" | ||
export * from "./use-checkout" | ||
export * from "./use-add-custom-item" | ||
export * from "./use-delete-cart-items" |
31 changes: 31 additions & 0 deletions
31
packages/react-shopper-hooks/src/cart/hooks/use-add-custom-item.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useMutation, UseMutationOptions } from "@tanstack/react-query" | ||
import { CartItemsResponse } from "@moltin/sdk" | ||
import { useElasticPath } from "../../elasticpath" | ||
|
||
export type CartAddCustomItemReq = { | ||
type: "custom_item" | ||
name: string | ||
description?: string | ||
sku?: string | ||
quantity: number | ||
price: { | ||
amount: number | ||
includes_tax: boolean | ||
} | ||
custom_inputs?: Record<string, any> | ||
shipping_group_id?: string | ||
tax?: any[] | ||
} | ||
|
||
export const useAddCustomItemToCart = ( | ||
cartId: string, | ||
options?: UseMutationOptions<CartItemsResponse, Error, CartAddCustomItemReq>, | ||
) => { | ||
const { client } = useElasticPath() | ||
return useMutation({ | ||
mutationFn: async (props) => { | ||
return client.Cart(cartId).AddCustomItem(props) | ||
}, | ||
...options, | ||
}) | ||
} |
1 change: 0 additions & 1 deletion
1
...hooks/src/cart/hooks/use-add-cart-item.ts → ...r-hooks/src/cart/hooks/use-add-product.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/react-shopper-hooks/src/cart/hooks/use-checkout.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { useMutation, UseMutationOptions } from "@tanstack/react-query" | ||
import { useElasticPath } from "../../elasticpath" | ||
import { | ||
Address, | ||
CartAdditionalHeaders, | ||
CheckoutCustomer, | ||
CheckoutCustomerObject, | ||
Order, | ||
Resource, | ||
} from "@moltin/sdk" | ||
|
||
export type UseCheckoutReq = { | ||
customer: string | CheckoutCustomer | CheckoutCustomerObject | ||
billingAddress: Partial<Address> | ||
shippingAddress?: Partial<Address> | ||
additionalHeaders?: CartAdditionalHeaders | ||
} | ||
|
||
export const useCheckout = ( | ||
cartId: string, | ||
options?: UseMutationOptions<Resource<Order>, Error, UseCheckoutReq>, | ||
) => { | ||
const { client } = useElasticPath() | ||
return useMutation({ | ||
mutationFn: async ({ | ||
customer, | ||
billingAddress, | ||
shippingAddress, | ||
additionalHeaders, | ||
}: UseCheckoutReq) => { | ||
return client | ||
.Cart(cartId) | ||
.Checkout(customer, billingAddress, shippingAddress, additionalHeaders) | ||
}, | ||
...options, | ||
}) | ||
} | ||
|
||
export type UseCheckoutWithAccountReq = { | ||
contact: string | CheckoutCustomer | CheckoutCustomerObject | ||
billingAddress: Partial<Address> | ||
token: string | ||
shippingAddress?: Partial<Address> | ||
additionalHeaders?: CartAdditionalHeaders | ||
} | ||
|
||
export const useCheckoutWithAccount = ( | ||
cartId: string, | ||
options?: UseMutationOptions< | ||
Resource<Order>, | ||
Error, | ||
UseCheckoutWithAccountReq | ||
>, | ||
) => { | ||
const { client } = useElasticPath() | ||
return useMutation({ | ||
mutationFn: async ({ | ||
contact, | ||
billingAddress, | ||
shippingAddress, | ||
token, | ||
additionalHeaders, | ||
}: UseCheckoutWithAccountReq) => { | ||
return client.Cart(cartId).CheckoutWithAccountManagementToken( | ||
contact, | ||
billingAddress, | ||
shippingAddress, | ||
token, | ||
// @ts-ignore TODO: Fix type definition in js-sdk | ||
additionalHeaders, | ||
) | ||
}, | ||
...options, | ||
}) | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/react-shopper-hooks/src/cart/hooks/use-delete-cart-items.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useMutation, UseMutationOptions } from "@tanstack/react-query" | ||
import { useElasticPath } from "../../elasticpath" | ||
import { CartItemsResponse } from "@moltin/sdk" | ||
|
||
type CartDeleteCartItemsReq = {} | ||
|
||
export const useDeleteCartItems = ( | ||
cartId: string, | ||
options?: UseMutationOptions< | ||
CartItemsResponse, | ||
Error, | ||
CartDeleteCartItemsReq | ||
>, | ||
) => { | ||
const { client } = useElasticPath() | ||
return useMutation({ | ||
mutationFn: async () => { | ||
return client.Cart(cartId).RemoveAllItems() | ||
}, | ||
...options, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./enhance-cart-response" | ||
export * from "./group-cart-items" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./use-currencies" |
27 changes: 27 additions & 0 deletions
27
packages/react-shopper-hooks/src/currency/hooks/use-currencies.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useElasticPath } from "../../elasticpath" | ||
import { UseQueryOptionsWrapper } from "../../types" | ||
import type { Currency, ResourcePage } from "@moltin/sdk" | ||
import { useQuery, UseQueryResult } from "@tanstack/react-query" | ||
import { queryKeysFactory } from "../../shared/util/query-keys-factory" | ||
|
||
const CURRENCY_QUERY_KEY = "currency" as const | ||
|
||
export const currencyQueryKeys = queryKeysFactory(CURRENCY_QUERY_KEY) | ||
type CurrencyQueryKey = typeof currencyQueryKeys | ||
|
||
export function useCurrencies( | ||
options?: UseQueryOptionsWrapper< | ||
ResourcePage<Currency, never>, | ||
Error, | ||
ReturnType<CurrencyQueryKey["list"]> | ||
>, | ||
): Partial<ResourcePage<Currency, never>> & | ||
Omit<UseQueryResult<ResourcePage<Currency, never>, Error>, "data"> { | ||
const { client } = useElasticPath() | ||
const { data, ...rest } = useQuery({ | ||
queryKey: currencyQueryKeys.list(), | ||
queryFn: () => client.Currencies.All(), | ||
...options, | ||
}) | ||
return { ...data, ...rest } as const | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./hooks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./use-payments" |
20 changes: 20 additions & 0 deletions
20
packages/react-shopper-hooks/src/payments/hooks/use-payments.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useMutation, UseMutationOptions } from "@tanstack/react-query" | ||
import { useElasticPath } from "../../elasticpath" | ||
import { ConfirmPaymentResponse, PaymentRequestBody } from "@moltin/sdk" | ||
|
||
export type UsePaymentsReq = { | ||
orderId: string | ||
payment: PaymentRequestBody | ||
} | ||
|
||
export const usePayments = ( | ||
options?: UseMutationOptions<ConfirmPaymentResponse, Error, UsePaymentsReq>, | ||
) => { | ||
const { client } = useElasticPath() | ||
return useMutation({ | ||
mutationFn: async ({ orderId, payment }: UsePaymentsReq) => { | ||
return client.Orders.Payment(orderId, payment) | ||
}, | ||
...options, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./hooks" |