Skip to content

Commit

Permalink
complete all the flow
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-joseph committed Oct 28, 2024
1 parent 493f3ac commit f482a86
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 9 deletions.
2 changes: 2 additions & 0 deletions ticketing/client/components/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export default ({ currentUser }) => {
const links = [
!currentUser && { label: 'Sign Up', href: '/auth/signup' },
!currentUser && { label: 'Sign In', href: '/auth/signin' },
currentUser && {label: 'Sell Tickets', href: '/tickets/new'},
currentUser && {label: 'My Orders', href: '/orders'},
currentUser && { label: 'Sign Out', href: '/auth/signout' },
]
.filter((linkConfig) => linkConfig)
Expand Down
4 changes: 2 additions & 2 deletions ticketing/client/hooks/use-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { useState } from "react";
const useRequest = ({url, method, body, onSuccess}) => {
const [errors, setErrors] = useState(null);

const doRequest = async () => {
const doRequest = async (props = {}) => {
try {
setErrors(null);
const response = await axios[method](url, body);
const response = await axios[method](url,{...body, ...props});
if(onSuccess) {
onSuccess(response.data);
}
Expand Down
36 changes: 35 additions & 1 deletion ticketing/client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion ticketing/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"axios": "^1.7.7",
"bootstrap": "^5.3.3",
"next": "^13.5.7",
"prop-types": "^15.8.1",
"react": "^18.3.1",
"react-dom": "^18.3.1"
"react-dom": "^18.3.1",
"react-stripe-checkout": "^2.6.3"
}
}
26 changes: 23 additions & 3 deletions ticketing/client/pages/orders/[orderId].js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import {useEffect, useState} from 'react'
const OrderShow = ({order}) => {
import {useEffect, useState} from 'react';
import StripeCheckout from 'react-stripe-checkout';
import Router from 'next/router';
import userRequest from '../../hooks/use-request';

const OrderShow = ({order, currentUser}) => {

const [timeLeft, setTimeLeft] = useState('');
const {doRequest, errors} = userRequest({
url: '/api/payments',
method: 'post',
body: {
orderId: order.id
},
onSuccess: (payment) => Router.push('/orders')
})

useEffect(()=>{
const findTimeLeft = () => {
Expand All @@ -22,7 +34,15 @@ const OrderShow = ({order}) => {

return (
<div>
<div>Tme letf to pay: {timeLeft} seconds</div>
<div>Tme letf to pay: {timeLeft} seconds
<StripeCheckout
token = {({id}) => doRequest({token: id})}
stripeKey='<TODO: add Pub Key>'
amount={order.ticket.price * 100}
email={currentUser.email}
/>
{errors}
</div>
</div>
)
}
Expand Down
20 changes: 20 additions & 0 deletions ticketing/client/pages/orders/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const OrderIndex = ({orders}) => {
return (
<div>
<ul>
{orders.map(order => {
return <li key={order.id}>
{order.ticket.title} - {order.status}
</li>
})}
</ul>
</div>
)
}

OrderIndex.getInitialProps = async (context, client) => {
const {data} = await client.get('/api/orders')
return {orders: data};
}

export default OrderIndex;
2 changes: 1 addition & 1 deletion ticketing/client/pages/tickets/[ticketId].js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const TicketShow = ({ticket}) => {
<h1>{ticket.title}</h1>
<h4>Price: {ticket.price}</h4>
{errors}
<button onClick={doRequest} className="btn btn-primary">Purchase</button>
<button onClick={() => doRequest()} className="btn btn-primary">Purchase</button>
</div>
)

Expand Down
2 changes: 1 addition & 1 deletion ticketing/tickets/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Ticket } from '../models/ticket';
const router = express.Router();

router.get('/api/tickets', async (req: Request, res: Response) => {
const tickets = await Ticket.find({});
const tickets = await Ticket.find({orderId: undefined});
res.send(tickets);
});

Expand Down

0 comments on commit f482a86

Please sign in to comment.