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 1 commit
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
13 changes: 11 additions & 2 deletions components/CheckoutForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import {
import { Button } from "@cheapreats/react-ui";
import styled from "styled-components";

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 +30,12 @@ export const CheckoutForm = (): React.ReactElement => {
return;
}

const successURL = "http://localhost:8080/checkout?id=".concat(orderId);
Copy link
Member

Choose a reason for hiding this comment

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

Use an ENV variable for this since production != development URL


const result = await stripe.confirmPayment({
elements,
confirmParams: {
return_url: "http://localhost:8080/success",
return_url: successURL,
// shipping: {Shipping Object}
},
});
Expand Down
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
38 changes: 36 additions & 2 deletions pages/success.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
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";

export const Success: React.VFC = () => {
const [orderId, setOrderId] = useState<string>("");
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(() => {
const orderIdString: string = router.query.id as string;
Copy link
Member

Choose a reason for hiding this comment

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

As string is usually something you want to avoid if you can

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was used to get around this typescript error, "Type 'string[]' is not assignable to type 'string'." Would this be a case where it is something we can't avoid?

Copy link
Member

Choose a reason for hiding this comment

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

Why can't you do [0] to grab the first string in the index since that's what it is? Just becuase the override the type doesn't override the underlying JavaScript structure. This will lead to confusion down the road ;).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When I tried getting the element at index 0 I would only get the first char in the order id which made me think that the id should be a string but for some reason its being identified as an array of string by typescript.

Copy link
Member

Choose a reason for hiding this comment

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

Ok let's take a look during our meeting. In the meantime I would suggest looking at the typing for Router for the entire application as the answer will probs be there.

Also try removing as string and seeing if the error goes away


if (!!router.query) {
setOrderId(orderIdString);
}
}, [router.query]);

useEffect(() => {
const timer = setTimeout(() => window.location.replace("/order"), 7000);
Copy link
Member

Choose a reason for hiding this comment

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

7000 is a magic number

return () => clearTimeout(timer);
}, []);

const headingProps = {
color: "green",
textAlign: "center",
Expand All @@ -16,6 +34,11 @@ export const Success: React.VFC = () => {
textAlign: "center",
};

const buttonProps = {
color: "red",
Copy link
Member

Choose a reason for hiding this comment

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

Put this in styled component

onClick: () => window.location.replace("/order"),
Copy link
Member

Choose a reason for hiding this comment

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

Just create a function

};

return (
<div
style={{
Expand All @@ -30,9 +53,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 {...buttonProps}>
Start New Order
</StyledButton>
</StyledCard>
</div>
);
Expand All @@ -49,3 +76,10 @@ const StyledCard = styled(Card)`
postion: relative;
padding: 3em;
`;

const StyledButton = styled(Button)`
margin-left: auto;
margin-right: auto;
Copy link
Member

Choose a reason for hiding this comment

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

Use margin short form notation

text-align: center;
margin-top: 1em;
`;