-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy paththank-you.js
98 lines (88 loc) · 3.03 KB
/
thank-you.js
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
import { useState, useEffect, useContext } from 'react';
import Router from 'next/router';
import Link from 'next/link';
import axios from 'axios';
import Layout from '../src/components/layout';
import Loading from '../src/components/icons/Loading';
import Bag from '../src/components/icons/Bag';
import { AppContext } from '../src/components/context';
import { HEADER_FOOTER_ENDPOINT } from '../src/utils/constants/endpoints';
const ThankYouContent = () => {
const [ cart, setCart ] = useContext( AppContext );
const [ isSessionFetching, setSessionFetching ] = useState( false );
const [ sessionData, setSessionData ] = useState( {} );
const session_id = process.browser ? Router.query.session_id : null;
useEffect( () => {
setSessionFetching( true );
if ( process.browser ) {
localStorage.removeItem( 'woo-next-cart' );
setCart( null );
if ( session_id ) {
axios.get( `/api/get-stripe-session/?session_id=${ session_id }` )
.then( ( response ) => {
setSessionData( response?.data ?? {} );
setSessionFetching( false );
} )
.catch( ( error ) => {
console.log( error );
setSessionFetching( false );
} );
}
}
}, [ session_id ] );
return (
<div className="h-almost-screen">
<div className="w-600px mt-10 m-auto">
{ isSessionFetching ? <Loading/> : (
<>
<h2 className="mb-6 text-xl"><Bag className="inline-block mr-1"/> <span>Thank you for placing the order.</span>
</h2>
<p>Your payment is successful and your order details are: </p>
<table className="table-auto w-full text-left whitespace-no-wrap mb-8">
<thead>
<tr>
<th className="px-4 py-3 title-font tracking-wider font-medium text-gray-900 text-sm bg-gray-100 rounded-tl rounded-bl">Name</th>
<th className="px-4 py-3 title-font tracking-wider font-medium text-gray-900 text-sm bg-gray-100">Details</th>
</tr>
</thead>
<tbody>
<tr>
<td className="px-4 py-3">Order#</td>
<td className="px-4 py-3">{ sessionData?.metadata?.orderId }</td>
</tr>
<tr>
<td className="px-4 py-3">Email</td>
<td className="px-4 py-3">{ sessionData?.customer_email }</td>
</tr>
</tbody>
</table>
<Link href="/">
<a className="bg-purple-600 text-white px-5 py-3 rounded-sm w-auto">Shop more</a>
</Link>
</>
) }
</div>
</div>
);
};
export default function ThankYou( { headerFooter } ) {
return (
<Layout headerFooter={ headerFooter || {} }>
<ThankYouContent/>
</Layout>
);
};
export async function getStaticProps() {
const { data: headerFooterData } = await axios.get( HEADER_FOOTER_ENDPOINT );
return {
props: {
headerFooter: headerFooterData?.data ?? {},
},
/**
* Revalidate means that if a new request comes to server, then every 1 sec it will check
* if the data is changed, if it is changed then it will update the
* static file inside .next folder with the new data, so that any 'SUBSEQUENT' requests should have updated data.
*/
revalidate: 1,
};
}