-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add success and error page for order after payment success or e…
…rror
- Loading branch information
Showing
6 changed files
with
177 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Button } from "@chakra-ui/react"; | ||
import { GetServerSideProps } from "next"; | ||
import { useRouter } from "next/router"; | ||
import LayoutOrderStatus from "~/components/obiz/LayoutOrderStatus"; | ||
|
||
type OrderSuccessProps = { | ||
order_id: string; | ||
}; | ||
|
||
export const OrderError = ({ order_id }: OrderSuccessProps) => { | ||
const router = useRouter(); | ||
|
||
return ( | ||
<LayoutOrderStatus | ||
title="Erreur lors de la livraison de la commande" | ||
subtitle="Quelque chose n'a pas fonctionné" | ||
status="error" | ||
onClose={() => router.push(`/dashboard/order/${order_id}`)} | ||
> | ||
<Button | ||
mt="auto" | ||
colorScheme="blackBtn" | ||
onClick={() => router.push(`/dashboard/order/${order_id}`)} | ||
> | ||
Retourner à la commande | ||
</Button> | ||
</LayoutOrderStatus> | ||
); | ||
}; | ||
|
||
export const getServerSideProps: GetServerSideProps = async ({ query }) => { | ||
const order_id = query.id; | ||
return { | ||
props: { order_id }, | ||
}; | ||
}; | ||
|
||
export default OrderError; |
File renamed without changes.
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,92 @@ | ||
import _ from "lodash"; | ||
import { GetServerSideProps } from "next"; | ||
import { useRouter } from "next/router"; | ||
import { useEffect, useMemo, useState } from "react"; | ||
import LayoutOrderStatus, { | ||
LayoutOrderStatusProps, | ||
} from "~/components/obiz/LayoutOrderStatus"; | ||
import { useAuth } from "~/providers/Auth"; | ||
import { api } from "~/utils/api"; | ||
|
||
type OrderSuccessProps = { | ||
order_id: string; | ||
}; | ||
|
||
type Step = { | ||
status: LayoutOrderStatusProps["status"]; | ||
title: string; | ||
subtitle: string; | ||
}; | ||
|
||
export const OrderSuccess = ({ order_id }: OrderSuccessProps) => { | ||
const utils = api.useUtils(); | ||
const { user } = useAuth(); | ||
const router = useRouter(); | ||
|
||
const [stepInterval, setStepInterval] = useState<NodeJS.Timeout>(); | ||
const [currentStep, setCurrentStep] = useState<Step>({ | ||
title: `Nous générons vos bons d'achat ${_.capitalize(user?.firstName ?? "")}...`, | ||
subtitle: "Validation du paiement...", | ||
status: "loading", | ||
}); | ||
|
||
const { mutate } = api.order.synchronizeOrder.useMutation({ | ||
onSuccess: ({ data }) => { | ||
if (data.ticket) { | ||
clearInterval(stepInterval); | ||
utils.order.getById.invalidate({ id: parseInt(order_id) }); | ||
setCurrentStep({ | ||
status: "success", | ||
title: "C'est prêt !", | ||
subtitle: "Tout est bon", | ||
}); | ||
setTimeout(() => router.push(`/dashboard/order/${order_id}`), 1000); | ||
} | ||
}, | ||
onError: (error) => { | ||
switch (error.data?.code) { | ||
case "FORBIDDEN": | ||
case "INTERNAL_SERVER_ERROR": | ||
router.push(`/dashboard/order/${order_id}/error`); | ||
break; | ||
} | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
mutate({ order_id: parseInt(order_id) }); | ||
|
||
const steps: Partial<Step>[] = [ | ||
{ | ||
subtitle: "Créations des bons d'achat...", | ||
}, | ||
]; | ||
|
||
let currentStepIndex = 0; | ||
let interval = setInterval(() => { | ||
const step = steps[currentStepIndex]; | ||
setCurrentStep((prev) => ({ ...prev, ...step })); | ||
if (currentStepIndex < steps.length - 1) { | ||
currentStepIndex++; | ||
} else { | ||
clearInterval(interval); | ||
router.push(`/dashboard/order/${order_id}`); | ||
} | ||
}, 2000); | ||
|
||
setStepInterval(interval); | ||
|
||
return () => clearInterval(interval); | ||
}, []); | ||
|
||
return <LayoutOrderStatus {...currentStep} />; | ||
}; | ||
|
||
export const getServerSideProps: GetServerSideProps = async ({ query }) => { | ||
const order_id = query.id; | ||
return { | ||
props: { order_id }, | ||
}; | ||
}; | ||
|
||
export default OrderSuccess; |
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