-
Notifications
You must be signed in to change notification settings - Fork 3
Feature/bgf 100 #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jimmy-ventura-elasticpath
wants to merge
9
commits into
main
Choose a base branch
from
feature/bgf-100
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/bgf 100 #378
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4f12c18
implementation of short order number
jimmy-ventura-elasticpath e804294
added a new type for order-with-short-number and changed implementati…
jimmy-ventura-elasticpath 6f5990e
restored pnpm-lock, hoping this solves vercel deployment
jimmy-ventura-elasticpath 168b4db
added new pnpm lock that has only the commerse essentials installation
jimmy-ventura-elasticpath 12d2f3c
Changed readme to reflect that it is no longer being generated from C…
jimmy-ventura-elasticpath 68b8fae
updating to latest next.js version to avoid issues with sharp (see fa…
jimmy-ventura-elasticpath d8ca83c
removing comment
jimmy-ventura-elasticpath b7cf053
added short circuit for recursive short order search
jimmy-ventura-elasticpath e5961b4
removed external ref from OrderItem component
jimmy-ventura-elasticpath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
37 changes: 37 additions & 0 deletions
37
examples/commerce-essentials/src/app/(checkout)/checkout/actions.ts
This file contains hidden or 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 @@ | ||
"use server"; | ||
|
||
import ShortUniqueId from "short-unique-id"; | ||
import { getServerSideCredentialsClient } from "../../../lib/epcc-server-side-credentials-client"; | ||
import { Order, OrderFilter, OrderInclude, OrdersEndpoint, OrderSort, QueryableResource } from "@elasticpath/js-sdk"; | ||
|
||
interface OrderFilterWithShortOrder extends OrderFilter { | ||
eq?: OrderFilter['eq'] & { | ||
order_number: string; | ||
}; | ||
} | ||
|
||
interface OrdersEndpointWithShortOrder extends QueryableResource<Order, OrderFilterWithShortOrder, OrderSort, OrderInclude> { | ||
endpoint: 'orders' | ||
} | ||
const shortIdGen = new ShortUniqueId(); | ||
|
||
export async function getShortOrderNumber():Promise<string> { | ||
return generateShortOrder(); | ||
} | ||
|
||
async function generateShortOrder() { | ||
const shortId = shortIdGen.rnd(); | ||
if (await found(shortId)) | ||
return generateShortOrder(); | ||
return shortId; | ||
} | ||
|
||
async function found(shortId: string) { | ||
const client=getServerSideCredentialsClient(); | ||
const orderEndpoint=client.Orders as OrdersEndpointWithShortOrder; | ||
orderEndpoint.Filter({eq:{order_number: shortId}}); | ||
const previousShortOrders=await orderEndpoint.All(); | ||
if(previousShortOrders.data.length>0) | ||
return true; | ||
return false; | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,127 @@ | ||
"use client" | ||
|
||
import { useMutation, UseMutationOptions } from "@tanstack/react-query" | ||
import { useElasticPath } from "@elasticpath/react-shopper-hooks" | ||
import { | ||
Address, | ||
CartAdditionalHeaders, | ||
CheckoutCustomer, | ||
CheckoutCustomerObject, | ||
ElasticPath, | ||
Order, | ||
Resource, | ||
} from "@elasticpath/js-sdk" | ||
|
||
export type UseCheckoutReq = { | ||
customer: string | CheckoutCustomer | CheckoutCustomerObject | ||
billingAddress: Partial<Address> | ||
shippingAddress?: Partial<Address> | ||
additionalHeaders?: CartAdditionalHeaders, | ||
shortOrder?: string | ||
} | ||
|
||
export const useCheckout = ( | ||
cartId: string, | ||
options?: UseMutationOptions<Resource<Order>, Error, UseCheckoutReq>, | ||
) => { | ||
const { client } = useElasticPath() | ||
return useMutation({ | ||
mutationFn: async ({ | ||
customer, | ||
billingAddress, | ||
shippingAddress, | ||
additionalHeaders, | ||
shortOrder | ||
}: UseCheckoutReq) => { | ||
return shortOrder ? checkoutWithShortOrder(client, cartId, shortOrder, billingAddress, shippingAddress, customer, undefined, additionalHeaders as { [key: string]: string }) : | ||
jimmy-ventura-elasticpath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
checkoutAsGuest(client, cartId, customer, billingAddress, shippingAddress, additionalHeaders) | ||
}, | ||
...options, | ||
}) | ||
} | ||
|
||
export type UseCheckoutWithAccountReq = { | ||
contact: string | CheckoutCustomer | CheckoutCustomerObject | ||
billingAddress: Partial<Address> | ||
token: string | ||
shippingAddress?: Partial<Address> | ||
additionalHeaders?: CartAdditionalHeaders | ||
shortOrder?: string | ||
} | ||
|
||
export const useCheckoutWithAccount = ( | ||
cartId: string, | ||
options?: UseMutationOptions< | ||
Resource<Order>, | ||
Error, | ||
UseCheckoutWithAccountReq | ||
>, | ||
) => { | ||
const { client } = useElasticPath() | ||
return useMutation({ | ||
mutationFn: async ({ | ||
contact, | ||
billingAddress, | ||
shippingAddress, | ||
token, | ||
additionalHeaders, | ||
shortOrder | ||
}: UseCheckoutWithAccountReq) => { | ||
if (token && additionalHeaders) { | ||
additionalHeaders["EP-Account-Management-Authentication-Token"] = token; | ||
} else if (token) { | ||
additionalHeaders = { "EP-Account-Management-Authentication-Token": token }; | ||
} | ||
return shortOrder ? checkoutWithShortOrder(client, cartId, shortOrder, billingAddress, shippingAddress, undefined, contact, additionalHeaders as { [key: string]: string }) : | ||
checkoutWithContact(client, cartId, contact, billingAddress, shippingAddress, token, additionalHeaders) | ||
}, | ||
...options, | ||
}) | ||
} | ||
function checkoutAsGuest(client: ElasticPath, cartId: string, customer: string | CheckoutCustomer | CheckoutCustomerObject, billingAddress: Partial<Address>, shippingAddress: Partial<Address> | undefined, additionalHeaders: CartAdditionalHeaders | undefined): Resource<Order> | PromiseLike<Resource<Order>> { | ||
return client | ||
.Cart(cartId) | ||
.Checkout(customer, billingAddress, shippingAddress, additionalHeaders) | ||
} | ||
|
||
function checkoutWithContact(client: ElasticPath, cartId: string, contact: string | CheckoutCustomer | CheckoutCustomerObject, billingAddress: Partial<Address>, shippingAddress: Partial<Address> | undefined, token: string, additionalHeaders: CartAdditionalHeaders | undefined): Resource<Order> | PromiseLike<Resource<Order>> { | ||
return client.Cart(cartId).CheckoutWithAccountManagementToken( | ||
contact, | ||
billingAddress, | ||
shippingAddress, | ||
token, | ||
// @ts-ignore TODO: Fix type definition in js-sdk | ||
additionalHeaders | ||
) | ||
} | ||
|
||
function checkoutWithShortOrder(client: ElasticPath, | ||
cartId: string, | ||
shortOrderId: string, | ||
billingAddress: Partial<Address>, | ||
shippingAddress: Partial<Address> | undefined, | ||
customer?: string | CheckoutCustomer | CheckoutCustomerObject, | ||
contact?: string | CheckoutCustomer | CheckoutCustomerObject, | ||
additionalHeaders?: { [key: string]: string }) { | ||
|
||
const body: { | ||
customer?: string | CheckoutCustomer | CheckoutCustomerObject, | ||
contact?:string | CheckoutCustomer | CheckoutCustomerObject, | ||
order_number: string, | ||
billing_address: Partial<Address>, | ||
shipping_address?: Partial<Address> | ||
|
||
} = { | ||
order_number: shortOrderId, | ||
billing_address: billingAddress, | ||
shipping_address: shippingAddress | ||
} | ||
body.customer = customer; | ||
body.contact = contact; | ||
|
||
|
||
return client.request.send(`/carts/${cartId}/checkout`, "POST", body, undefined, client, undefined, "v2", | ||
additionalHeaders, | ||
); | ||
} | ||
|
4 changes: 4 additions & 0 deletions
4
examples/commerce-essentials/src/lib/types/order-with-short-number.ts
This file contains hidden or 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,4 @@ | ||
import { Order } from "@elasticpath/js-sdk"; | ||
export interface OrderWithShortNumber extends Order { | ||
order_number?: string; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.