Skip to content
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

feat(react-centra-checkout): add support for passing token details to useCentraOrders #80

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions packages/react-centra-checkout/src/Context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -636,21 +636,54 @@ export function useCentraReceipt(token: string): Centra.CheckoutApi.OrderComplet
/** Returns the latest orders for the currently logged in user
@param from - Display orders from this index. Defaults to 0.
@param size - Display this many orders. Defaults lists all orders.
@param token.tokenName - The name of the cookie used to use/store as Centra checkout token
@param token.tokenExpires - When the cookie used to store the Centra checkout token will expire, days as a number or a Date
@param token.tokenCookieOptions - Cookie options
*/
export function useCentraOrders(from?: number, size?: number): Centra.CheckoutApi.OrdersResponse {
export function useCentraOrders(
from?: number,
size?: number,
token?: {
tokenName?: string
tokenExpires?: number | Date
tokenCookieOptions?: Cookies.CookieAttributes
},
Comment on lines +646 to +650
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Is this necessary?
suggestion: Would be nice if we can enforce that tokenName in both the Context as well as inside useCentraOrders.

): Centra.CheckoutApi.OrdersResponse {
const [result, setResult] = React.useState<Centra.CheckoutApi.OrdersResponse>({})

React.useEffect(() => {
const fetchOrders = React.useCallback(() => {
let apiToken: string | undefined

if (token?.tokenName) {
apiToken = cookies.get(token.tokenName)
if (apiToken) {
apiClient.headers.set('api-token', apiToken)
}
}

// fetch orders
apiClient
.request('POST', 'orders', {
...(from && { from }),
...(size && { size }),
})
.then((response) => {
if (apiToken !== undefined && response.token && response.token !== apiToken) {
apiClient.headers.set('api-token', response.token)
if (token?.tokenName) {
cookies.set(token.tokenName, response.token, {
expires: token?.tokenExpires || 365,
...token?.tokenCookieOptions,
})
}
}
setResult(response)
})
}, [from, size])
}, [setResult, from, size, token?.tokenName, token?.tokenCookieOptions, token?.tokenExpires])

React.useEffect(() => {
fetchOrders()
}, [from, size, fetchOrders])

return result
}
Expand Down