Skip to content

Commit

Permalink
feat(vrack-services): create hooks for order creation
Browse files Browse the repository at this point in the history
ref: MANAGER-16503

Signed-off-by: Quentin Pavy <[email protected]>
  • Loading branch information
Quentin Pavy committed Jan 6, 2025
1 parent 5620f56 commit 2916fc9
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
OsdsMessage,
} from '@ovhcloud/ods-components/react';
import {
CreateCartResult,
OrderDescription,
getDeliveringOrderQueryKey,
useOrderPollingStatus,
Expand All @@ -29,14 +28,13 @@ import {
} from '@ovh-ux/manager-react-shell-client';
import { useNavigate } from 'react-router-dom';
import { handleClick } from '@ovh-ux/manager-react-components';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { ApiError } from '@ovh-ux/manager-core-api';
import { useQueryClient } from '@tanstack/react-query';
import { getVrackListQueryKey } from '@/data/api';
import { DeliveringMessages } from '@/components/DeliveringMessages.component';
import { MessagesContext } from './feedback-messages/Messages.context';
import { LoadingText } from './LoadingText.component';
import { OrderSubmitModalContent } from './OrderSubmitModalContent.component';
import { createVrackOnlyCart } from '@/utils/cart';
import { useCreateCartWithVrack } from '@/data/hooks';

const trackingParams = {
location: PageLocation.popup,
Expand All @@ -55,12 +53,13 @@ export const CreateVrack: React.FC<CreateVrackProps> = ({ closeModal }) => {
const { trackClick } = useOvhTracking();
const navigate = useNavigate();

const { mutate: createCart, data, error, isError, isPending } = useMutation<
CreateCartResult,
ApiError
>({
mutationFn: () => createVrackOnlyCart(environment.user.ovhSubsidiary),
});
const {
createCart,
data,
error,
isError,
isPending,
} = useCreateCartWithVrack(environment.user.ovhSubsidiary);

const {
data: vrackDeliveringOrders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ import {
PageLocation,
useOvhTracking,
} from '@ovh-ux/manager-react-shell-client';
import {
Contract,
Order,
postOrderCartCartIdCheckout,
} from '@ovh-ux/manager-module-order';
import { useMutation } from '@tanstack/react-query';
import { Contract, Order } from '@ovh-ux/manager-module-order';
import { ApiError, ApiResponse } from '@ovh-ux/manager-core-api';
import { LoadingText } from '@/components/LoadingText.component';
import { SendOrderState, useSendOrder } from '@/data/hooks';

export type OrderSubmitModalContentProps = {
submitButtonLabel: string;
Expand All @@ -53,38 +49,23 @@ export const OrderSubmitModalContent: React.FC<OrderSubmitModalContentProps> = (
const { t } = useTranslation('vrack-services');
const { trackClick } = useOvhTracking();
const [isContractAccepted, setIsContractAccepted] = React.useState(false);
const { mutate: sendOrder, isPending, error, isError } = useMutation<
ApiResponse<Order>,
ApiError
>({
mutationFn: () =>
postOrderCartCartIdCheckout({
cartId,
autoPayWithPreferredPaymentMethod: true,
waiveRetractationPeriod: true,
}),
onSuccess,
onError: async (response) => {
const {
request: { status },
} = response;
const {
sendOrder,
isPending,
error,
isError,
data,
sendOrderState,
} = useSendOrder();

if (status === 400) {
try {
const { data } = await postOrderCartCartIdCheckout({
cartId,
autoPayWithPreferredPaymentMethod: false,
waiveRetractationPeriod: true,
});
window.top.location.href = data.url;
} catch (err) {
onError(response);
}
} else {
onError(response);
}
},
});
React.useEffect(() => {
if (!isPending && sendOrderState === SendOrderState.DONE) {
onSuccess(data);
}
if (sendOrderState === SendOrderState.ERROR && onError) {
onError(error);
}
}, [isPending, sendOrderState, error]);

return (
<>
Expand Down Expand Up @@ -151,13 +132,13 @@ export const OrderSubmitModalContent: React.FC<OrderSubmitModalContentProps> = (
variant={ODS_BUTTON_VARIANT.flat}
disabled={!isContractAccepted || undefined}
color={ODS_THEME_COLOR_INTENT.primary}
{...handleClick(() => {
{...handleClick(async () => {
trackClick({
location: PageLocation.popup,
buttonType: ButtonType.button,
actions: ['order', 'confirm'],
});
sendOrder();
sendOrder({ cartId });
})}
>
{submitButtonLabel}
Expand Down
5 changes: 5 additions & 0 deletions packages/manager/apps/vrack-services/src/data/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useSendOrder } from './useSendOrder';

export * from './useVrackServicesList';
export * from './useVrackServices';
export * from './useUpdateVrackServices';
Expand All @@ -6,3 +8,6 @@ export * from './useAssociateVrack';
export * from './useDissociateVrack';
export * from './useAllowedVrackList';
export * from './useVrackList';
export * from './useCreateCartWithVrack';
export * from './useSendOrder';
export * from './useCreateCart';
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,
};
};
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,
};
};
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,
};
};
Loading

0 comments on commit 2916fc9

Please sign in to comment.