-
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.
Merge pull request #235 from alwinsimon/v1
V1
- Loading branch information
Showing
11 changed files
with
287 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,56 @@ | ||
import buildClient from "../api/build-client"; | ||
import Link from 'next/link'; | ||
|
||
const IndexPage = ({ currentUser }) => { | ||
const IndexPage = ({ currentUser, tickets }) => { | ||
if (!currentUser) { | ||
return ( | ||
<> | ||
<h1>Landing Page</h1> | ||
<br/> | ||
<br /> | ||
<h5>Signed Out</h5> | ||
</> | ||
); | ||
} | ||
|
||
let ticketList; | ||
if (currentUser) { | ||
ticketList = tickets.map((ticket) => { | ||
return ( | ||
<tr key={ticket.id}> | ||
<td>{ticket.title}</td> | ||
<td>{ticket.price}</td> | ||
<td> | ||
<Link href="/tickets/[ticketId]" as={`/tickets/${ticket.id}`}> | ||
View | ||
</Link> | ||
</td> | ||
</tr> | ||
); | ||
}); | ||
} | ||
|
||
return ( | ||
<> | ||
<h1>Landing Page</h1> | ||
<br /> | ||
<h5>Welcome {currentUser.email}</h5> | ||
|
||
<h1>Tickets</h1> | ||
<table className="table table-striped"> | ||
<thead> | ||
<tr> | ||
<th>Title</th> | ||
<th>Price</th> | ||
<th>Action</th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody>{currentUser && ticketList}</tbody> | ||
</table> | ||
</> | ||
); | ||
}; | ||
|
||
IndexPage.getInitialProps = async (context) => { | ||
try { | ||
const client = buildClient(context); | ||
const response = await client.get("/api/users/currentuser"); | ||
return response.data; | ||
} catch (err) { | ||
console.error("Error in making request to get current user:", err.message); | ||
return { currentUser: null }; | ||
} | ||
IndexPage.getInitialProps = async (context, client, currentUser) => { | ||
const { data } = await client.get("/api/tickets"); | ||
return { tickets: data }; | ||
}; | ||
|
||
export default IndexPage; |
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,66 @@ | ||
import { useEffect, useState } from "react"; | ||
import Router from "next/router"; | ||
import StripeCheckout from "react-stripe-checkout"; | ||
import useRequest from "../../hooks/use-request"; | ||
|
||
const OrderShow = ({ order, currentUser }) => { | ||
const [timeLeft, setTimeLeft] = useState(0); | ||
const [makeRequest, errors] = useRequest({ | ||
url: "/api/payments", | ||
method: "post", | ||
body: { | ||
orderId: order.id, | ||
}, | ||
onSuccess: (payment) => { | ||
Router.push("/orders"); | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
const findTimeLeft = () => { | ||
const milliSecondsLeft = new Date(order.expiresAt) - new Date(); | ||
const secondsLeft = Math.round(milliSecondsLeft / 1000); | ||
|
||
setTimeLeft(secondsLeft); | ||
}; | ||
|
||
findTimeLeft(); | ||
const timer = setInterval(findTimeLeft, 1000); | ||
|
||
return () => { | ||
clearInterval(timer); | ||
}; | ||
}, [order]); | ||
|
||
if (timeLeft < 0) { | ||
return ( | ||
<div> | ||
<h3 className="text text-danger">Order Expired !!!</h3> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div> | ||
Time left to complete payment: {timeLeft} seconds. | ||
<StripeCheckout | ||
token={(token) => { | ||
makeRequest({ token: token.id }); | ||
}} | ||
stripeKey="pk_test_51O53zRSJtuYafghXhYNZzaJqYAh6afqRduQ3UAMs6Wm4vkv30ayq09gBPgU3jYkQPXrofQa9aRbIlb4uuCp3FC6O000J86xaKc" | ||
amount={order.ticket.price * 100} | ||
email={currentUser.email} | ||
/> | ||
{errors} | ||
</div> | ||
); | ||
}; | ||
|
||
OrderShow.getInitialProps = async (context, client) => { | ||
const { orderId } = context.query; | ||
const { data } = await client.get(`/api/orders/${orderId}`); | ||
|
||
return { order: data }; | ||
}; | ||
|
||
export default OrderShow; |
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,23 @@ | ||
const OrderIndex = ({ orders }) => { | ||
return ( | ||
<> | ||
<ul> | ||
{orders.map((order) => { | ||
return ( | ||
<li key={orders.id}> | ||
{order.ticket.title} - {order.status} | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
</> | ||
); | ||
}; | ||
|
||
OrderIndex.getInitialProps = async (context, client) => { | ||
const response = await client.get("/api/orders"); | ||
|
||
return { orders: response.data }; | ||
}; | ||
|
||
export default OrderIndex; |
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,40 @@ | ||
import Router from "next/router"; | ||
import useRequest from "../../hooks/use-request"; | ||
|
||
const TicketShow = ({ ticket }) => { | ||
const [makeRequest, errors] = useRequest({ | ||
url: "/api/orders", | ||
method: "post", | ||
body: { | ||
ticketId: ticket.id, | ||
}, | ||
onSuccess: (orderData) => { | ||
Router.push("/orders/[orderId]", `/orders/${orderData.id}`); | ||
}, | ||
}); | ||
|
||
const handleClick = async () => { | ||
makeRequest(); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h1>{ticket.title}</h1> | ||
<h4>Price: {ticket.price} INR</h4> | ||
{errors} | ||
<button className="btn btn-primary" onClick={handleClick}> | ||
Purchase | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
TicketShow.getInitialProps = async (context, client) => { | ||
const { ticketId } = context.query; | ||
|
||
const { data } = await client.get(`/api/tickets/${ticketId}`); | ||
|
||
return { ticket: data }; | ||
}; | ||
|
||
export default TicketShow; |
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,70 @@ | ||
import { useState } from "react"; | ||
import Router from "next/router"; | ||
import useRequest from "../../hooks/use-request"; | ||
|
||
const NewTicket = () => { | ||
const [title, setTitle] = useState(""); | ||
const [price, setPrice] = useState(""); | ||
const [makeRequest, errors] = useRequest({ | ||
url: "/api/tickets", | ||
method: "post", | ||
body: { | ||
title: title, | ||
price: price, | ||
}, | ||
onSuccess: () => { | ||
Router.push("/"); | ||
}, | ||
}); | ||
|
||
const handleSubmit = (event) => { | ||
event.preventDefault(); | ||
|
||
if (!title || !price) { | ||
return; | ||
} | ||
|
||
makeRequest(); | ||
}; | ||
|
||
const roundOff = () => { | ||
const roundedPrice = parseFloat(price); | ||
|
||
if (isNaN(roundedPrice)) { | ||
// If price is not a number | ||
return; | ||
} | ||
|
||
setPrice(roundedPrice.toFixed(2)); | ||
}; | ||
return ( | ||
<div> | ||
<h1>Create A Ticket</h1> | ||
<form onSubmit={handleSubmit}> | ||
<div className="form-group"> | ||
<label>Title</label> | ||
<input | ||
className="form-control" | ||
value={title} | ||
onChange={(e) => setTitle(e.target.value)} | ||
/> | ||
</div> | ||
<div className="form-group"> | ||
<label>Price</label> | ||
<input | ||
className="form-control" | ||
type="number" | ||
step="0.01" | ||
onBlur={roundOff} | ||
value={price} | ||
onChange={(e) => setPrice(e.target.value)} | ||
/> | ||
</div> | ||
{errors} | ||
<button className="btn btn-primary">Submit</button> | ||
</form> | ||
</div> | ||
); | ||
}; | ||
|
||
export default NewTicket; |
Oops, something went wrong.