Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Added features to success page #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions components/CheckoutForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ import {
} from "@stripe/react-stripe-js";
import { Button } from "@cheapreats/react-ui";
import styled from "styled-components";
import { process } from "../index";

export const CheckoutForm = (): React.ReactElement => {
interface CheckoutFormProps {
orderId: string;
}

export const CheckoutForm: React.VFC<CheckoutFormProps> = ({
orderId,
...props
}) => {
const stripe = useStripe();
const elements = useElements();

Expand All @@ -23,10 +31,12 @@ export const CheckoutForm = (): React.ReactElement => {
return;
}

const successURL = `${process.env.BASE_URL} /checkout?id= ${orderId}`;

const result = await stripe.confirmPayment({
elements,
confirmParams: {
return_url: "http://localhost:8080/success",
return_url: successURL,
// shipping: {Shipping Object}
},
});
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export declare var process: {
env: {
BASE_URL: string;
};
};

2 changes: 1 addition & 1 deletion pages/checkout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const Checkout: React.VFC = () => {
<Header>Payment</Header>
{clientSecret && (
<Elements stripe={stripePromise} options={options}>
<CheckoutForm />
{!!cart && <CheckoutForm orderId={cart._id} />}
</Elements>
)}
</StyledCard>
Expand Down
39 changes: 34 additions & 5 deletions pages/success.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import { Card, Heading, SmallText } from "@cheapreats/react-ui";
import { Card, Heading, SmallText, Button } from "@cheapreats/react-ui";
import React from "react";
import styled from "styled-components";
import Image from "next/image";
import Snowfall from "react-snowfall";
import { useState, useEffect } from "react";
import { useRouter } from "next/router";

const SESSION_EXPIRY_TIMEOUT = 7000;

export const Success: React.VFC = () => {
const [orderId, setOrderId] = useState("");
const router = useRouter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no typing needed here like <url: string>
Might reduce the need for AS below


useEffect(() => {
if (!!router.query.id) {
setOrderId(router.query.id as string);
}
}, [router.query]);

function newOrder() {
window.location.replace("/order");
}

useEffect(() => {
const timer = setTimeout(newOrder, SESSION_EXPIRY_TIMEOUT);
return () => clearTimeout(timer);
}, []);

const headingProps = {
color: "green",
textAlign: "center",
Expand All @@ -30,9 +52,13 @@ export const Success: React.VFC = () => {
<Image src={require("../images/logo.jpg")} />
<Heading {...headingProps}>Thanks for your order!</Heading>
<SmallText {...smallTextProps}>
We appreciate your business! If you have any questions, please email
Your order id is {orderId}. We appreciate your business! If you have
any questions, please email
<a href="mailto:[email protected]"> [email protected]</a>
</SmallText>
<StyledButton primary onClick={newOrder}>
Start New Order
</StyledButton>
</StyledCard>
</div>
);
Expand All @@ -43,9 +69,12 @@ export default Success;
const StyledCard = styled(Card)`
width: 60%;
text-align: center;
margin-top: 10em;
margin-left: auto;
margin-right: auto;
margin: 10em auto auto;
postion: relative;
padding: 3em;
`;

const StyledButton = styled(Button)`
margin: 1em auto auto;
text-align: center;
`;