forked from vendure-ecommerce/storefront-remix-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
confirmation.$orderCode.tsx
139 lines (129 loc) · 4 KB
/
confirmation.$orderCode.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { DataFunctionArgs } from '@remix-run/server-runtime';
import { getOrderByCode } from '~/providers/orders/order';
import { useLoaderData } from '@remix-run/react';
import { CartContents } from '~/components/cart/CartContents';
import { CartTotals } from '~/components/cart/CartTotals';
import { CheckCircleIcon, XCircleIcon } from '@heroicons/react/24/outline';
import { InformationCircleIcon } from '@heroicons/react/24/solid';
import { useRevalidator } from '@remix-run/react';
import { useEffect, useState } from 'react';
import { OrderDetailFragment } from '~/generated/graphql';
import { useTranslation } from 'react-i18next';
export async function loader({ params, request }: DataFunctionArgs) {
try {
const order = await getOrderByCode(params.orderCode!, { request });
return {
order,
error: false,
};
} catch (ex) {
return {
order: null,
error: true,
};
}
}
export default function CheckoutConfirmation() {
const { order, error } = useLoaderData<typeof loader>();
const revalidator = useRevalidator();
const [retries, setRetries] = useState(1);
const { t } = useTranslation();
const orderNotFound = !order && !error;
const orderErrored = !order && error;
const maxRetries = 5;
const retriesExhausted = retries >= maxRetries;
const retryTimeout = 2500;
const retry = () => {
if (!window) return;
setRetries(retries + 1);
window.setTimeout(() => {
if (retries > maxRetries) return;
revalidator.revalidate();
}, retryTimeout);
};
useEffect(() => {
if (orderErrored) {
retry();
}
}, [order]);
useEffect(() => {
if (
revalidator.state === 'idle' &&
orderErrored &&
retries <= maxRetries &&
retries > 1
) {
retry();
}
}, [revalidator.state]);
if (orderNotFound) {
return (
<div>
<h2 className="text-3xl sm:text-5xl font-light tracking-tight text-gray-900 my-8">
{t('checkout.orderNotFound')}
</h2>
</div>
);
}
if (orderErrored && retriesExhausted) {
return (
<div>
<h2 className="text-3xl flex items-center space-x-2 sm:text-5xl font-light tracking-tight text-gray-900 my-8">
<XCircleIcon className="text-red-600 w-8 h-8 sm:w-12 sm:h-12"></XCircleIcon>
<span>{t('checkout.orderErrorTitle')}</span>
</h2>
<p className="text-lg text-gray-700">
{t('checkout.orderErrorMessage')}
</p>
</div>
);
}
if (orderErrored) {
return (
<div>
<h2 className="text-3xl flex items-center space-x-2 sm:text-5xl font-light tracking-tight text-gray-900 my-8">
{t('checkout.orderProcessing')}
</h2>
</div>
);
}
return (
<div>
<h2 className="text-3xl flex items-center space-x-2 sm:text-5xl font-light tracking-tight text-gray-900 my-8">
<CheckCircleIcon className="text-green-600 w-8 h-8 sm:w-12 sm:h-12"></CheckCircleIcon>
<span>{t('order.summary')}</span>
</h2>
<p className="text-lg text-gray-700">
{t('checkout.orderSuccessMessage')}{' '}
<span className="font-bold">{order!.code}</span>
</p>
{order!.active && (
<div className="rounded-md bg-blue-50 p-4 my-8">
<div className="flex">
<div className="flex-shrink-0">
<InformationCircleIcon
className="h-5 w-5 text-blue-400"
aria-hidden="true"
/>
</div>
<div className="ml-3 flex-1 md:flex md:justify-between">
<p className="text-sm text-blue-700">
{t('checkout.paymentMessage')}
</p>
</div>
</div>
</div>
)}
<div className="mt-12">
<div className="mb-6">
<CartContents
orderLines={order!.lines}
currencyCode={order!.currencyCode}
editable={false}
/>
</div>
<CartTotals order={order as OrderDetailFragment}></CartTotals>
</div>
</div>
);
}