-
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.
- Loading branch information
Showing
13 changed files
with
223 additions
and
34 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import buildClient from '@/api/build-client'; | ||
import useRequest from '@/hooks/use-request'; | ||
import { Router } from 'next/router'; | ||
import { useEffect } from 'react'; | ||
import StripeCheckout from 'react-stripe-checkout'; | ||
|
||
export default function OrderShow({ order, currentUser }) { | ||
const [timeLeft, setTimeLeft] = useState(0); | ||
const [errorComponent, doRequest] = useRequest({ | ||
url: '/api/payments', | ||
method: 'post', | ||
body: { | ||
orderId: order.id, | ||
}, | ||
onSuccess: () => Router.push('/orders'), | ||
}); | ||
|
||
useEffect(() => { | ||
const findTimeLeft = () => { | ||
const msLeft = new Date(order.expiresAt) - new Date(); | ||
setTimeLeft(Math.round(msLeft / 1000)); | ||
}; | ||
|
||
findTimeLeft(); | ||
const timerId = setInterval(findTimeLeft, 1000); | ||
|
||
return () => { | ||
clearInterval(timerId); | ||
}; | ||
}, []); | ||
|
||
if (timeLeft < 0) { | ||
return <div>Order Expired!</div>; | ||
} | ||
|
||
return ( | ||
<div> | ||
<p>Time left to pay: {msLeft} seconds</p> | ||
<StripeCheckout | ||
token={({ id }) => doRequest({ token: id })} | ||
stripeKey="pk_test_51NAuhnLTrkqQD856STNCM5EudKDpxKGll2N5oizidAWxkDq3IJiEQyersbzPwMKZrUW7Lmi8sI78qMqa7GZXEXHC00xqYlwNqS" | ||
amount={order.ticket.price * 100} | ||
email={currentUser.email} | ||
/> | ||
{errorComponent} | ||
</div> | ||
); | ||
} | ||
|
||
export async function getServerSideProps(context) { | ||
const { orderId } = context.query; | ||
const client = buildClient(context); | ||
const { data } = client.get(`/api/orders/${orderId}`); | ||
|
||
return { props: { order: data } }; | ||
} |
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,20 @@ | ||
import buildClient from '@/api/build-client'; | ||
|
||
export default function OrderIndex({ orders }) { | ||
return ( | ||
<ul> | ||
{orders.map((order) => ( | ||
<li key={order.id}> | ||
{order.ticket.title} - {order.status} | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
} | ||
|
||
export async function getServerSideProps(context) { | ||
const client = buildClient(context); | ||
const { data } = await client.get('/api/orders'); | ||
|
||
return { props: { orders: data } }; | ||
} |
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,33 @@ | ||
import useRequest from '@/hooks/use-request'; | ||
import { Router } from 'next/router'; | ||
|
||
export default function Ticket({ ticket }) { | ||
const [errorComponent, doRequest] = useRequest({ | ||
url: '/api/orders', | ||
method: 'post', | ||
body: { | ||
ticketId: ticket.id, | ||
}, | ||
onSuccess: (order) => | ||
Router.push('/orders/[orderId]', `/orders/${order.id}`), | ||
}); | ||
|
||
return ( | ||
<div> | ||
<h1>{ticket.title}</h1> | ||
<h4>Price: {ticket.price}</h4> | ||
{errorComponent} | ||
<button className="btn btn-primary" onClick={() => doRequest()}> | ||
Purchase | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export async function getServerSideProps(context) { | ||
const { ticketId } = context.query; | ||
const client = buildClient(context); | ||
const { data } = await client.get('/api/tickets/${ticketId}'); | ||
|
||
return { props: { ticket: data } }; | ||
} |
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,59 @@ | ||
import useRequest from '@/hooks/use-request'; | ||
import { Router } from 'next/router'; | ||
import { useState } from 'react'; | ||
|
||
export default function NewTicket() { | ||
const [title, setTitle] = useState(''); | ||
const [price, setPrice] = useState(); | ||
|
||
const [errorComponent, doRequest] = useRequest({ | ||
url: '/api/tickets', | ||
method: 'post', | ||
body: { | ||
title, | ||
price, | ||
}, | ||
onSuccess: () => Router.push('/'), | ||
}); | ||
|
||
const onSubmit = (e) => { | ||
e.preventDefault(); | ||
doRequest(); | ||
}; | ||
|
||
const onBlurHandler = (e) => { | ||
const value = parseFloat(price); | ||
setPrice(value.toFixed(2)); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h1>Create a Ticket</h1> | ||
<form onSubmit={onSubmit}> | ||
<div className="mb-3"> | ||
<label htmlFor="title">Title</label> | ||
<input | ||
id="title" | ||
type="text" | ||
name="title" | ||
className="form-control" | ||
value={title} | ||
onChange={(e) => setTitle(e.target.value)} | ||
/> | ||
</div> | ||
<div className="mb-3"> | ||
<label htmlFor="price">Price</label> | ||
<input | ||
id="price" | ||
type="number" | ||
onBlur={onBlurHandler} | ||
name="price" | ||
className="form-control" | ||
/> | ||
</div> | ||
{errorComponent} | ||
<button className="btn btn-primary">Submit</button> | ||
</form> | ||
</div> | ||
); | ||
} |
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