-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vrack-services): create hooks for order creation
ref: MANAGER-16503 Signed-off-by: Quentin Pavy <[email protected]>
- Loading branch information
Quentin Pavy
committed
Jan 6, 2025
1 parent
5620f56
commit 2916fc9
Showing
7 changed files
with
194 additions
and
104 deletions.
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
packages/manager/apps/vrack-services/src/data/hooks/useCreateCart.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,44 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { ApiError } from '@ovh-ux/manager-core-api'; | ||
import { CreateCartResult } from '@ovh-ux/manager-module-order'; | ||
import { createVrackServicesCart } from '@/utils/cart'; | ||
import { useSendOrder } from '@/data/hooks'; | ||
|
||
export const useCreateCart = () => { | ||
const { | ||
sendOrder, | ||
isPending: isSendOrderPending, | ||
error: sendOrderError, | ||
isError: isSendOrderError, | ||
sendOrderState, | ||
} = useSendOrder(); | ||
|
||
const { mutate: createCart, data, error, isError, isPending } = useMutation< | ||
CreateCartResult, | ||
ApiError, | ||
{ hasVrack?: boolean; region: string; ovhSubsidiary: string } | ||
>({ | ||
mutationFn: async (params) => { | ||
const createCartResponse = await createVrackServicesCart({ | ||
...params, | ||
}); | ||
|
||
if (createCartResponse.contractList.length === 0) | ||
await sendOrder({ cartId: createCartResponse.cartId }); | ||
|
||
return Promise.resolve(createCartResponse); | ||
}, | ||
}); | ||
|
||
return { | ||
createCart, | ||
data, | ||
error, | ||
isError, | ||
isPending, | ||
isSendOrderPending, | ||
isSendOrderError, | ||
sendOrderError, | ||
sendOrderState, | ||
}; | ||
}; |
24 changes: 24 additions & 0 deletions
24
packages/manager/apps/vrack-services/src/data/hooks/useCreateCartWithVrack.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,24 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { ApiError } from '@ovh-ux/manager-core-api'; | ||
import { CreateCartResult } from '@ovh-ux/manager-module-order'; | ||
import { createVrackOnlyCart } from '@/utils/cart'; | ||
|
||
/** | ||
* @returns create a cart with 1 vrack inside | ||
*/ | ||
export const useCreateCartWithVrack = (ovhSubsidiary: string) => { | ||
const { mutate: createCart, data, error, isError, isPending } = useMutation< | ||
CreateCartResult, | ||
ApiError | ||
>({ | ||
mutationFn: () => createVrackOnlyCart(ovhSubsidiary), | ||
}); | ||
|
||
return { | ||
createCart, | ||
data, | ||
error, | ||
isError, | ||
isPending, | ||
}; | ||
}; |
71 changes: 71 additions & 0 deletions
71
packages/manager/apps/vrack-services/src/data/hooks/useSendOrder.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,71 @@ | ||
import { useState } from 'react'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { ApiError, ApiResponse } from '@ovh-ux/manager-core-api'; | ||
import { | ||
Order, | ||
postOrderCartCartIdCheckout, | ||
} from '@ovh-ux/manager-module-order'; | ||
|
||
export enum SendOrderState { | ||
INACTIVE = 'inactive', | ||
PENDING = 'pending', | ||
DONE = 'done', | ||
ERROR = 'error', | ||
} | ||
/** | ||
* Try to create an order with automatic validation | ||
* If it fails, redirect to express order | ||
*/ | ||
export const useSendOrder = () => { | ||
const [sendOrderState, setSendOrderState] = useState<SendOrderState>( | ||
SendOrderState.INACTIVE, | ||
); | ||
const { | ||
mutate: sendOrder, | ||
isPending, | ||
error: orderError, | ||
isError, | ||
data, | ||
} = useMutation<ApiResponse<Order>, ApiError, { cartId: string }>({ | ||
mutationFn: ({ cartId }) => { | ||
setSendOrderState(SendOrderState.PENDING); | ||
return postOrderCartCartIdCheckout({ | ||
cartId, | ||
autoPayWithPreferredPaymentMethod: true, | ||
waiveRetractationPeriod: true, | ||
}); | ||
}, | ||
onSuccess: () => setSendOrderState(SendOrderState.DONE), | ||
onError: async (error, { cartId }) => { | ||
const { | ||
request: { status }, | ||
} = error; | ||
if (status === 400) { | ||
try { | ||
const sendOrderResponse = await postOrderCartCartIdCheckout({ | ||
cartId, | ||
autoPayWithPreferredPaymentMethod: false, | ||
waiveRetractationPeriod: true, | ||
}); | ||
setSendOrderState(SendOrderState.DONE); | ||
window.top.location.href = sendOrderResponse.data.url; | ||
return Promise.resolve(sendOrderResponse); | ||
} catch (e) { | ||
setSendOrderState(SendOrderState.ERROR); | ||
return Promise.reject(e); | ||
} | ||
} | ||
setSendOrderState(SendOrderState.ERROR); | ||
return Promise.reject(error); | ||
}, | ||
}); | ||
|
||
return { | ||
sendOrder, | ||
isPending: isPending && sendOrderState === SendOrderState.PENDING, | ||
sendOrderState, | ||
error: orderError, | ||
isError, | ||
data, | ||
}; | ||
}; |
Oops, something went wrong.