import React from "react"; import { FormGroup, FormControl, FormLabel, Form, FormControlProps } from "react-bootstrap"; import "./checkoutForm.css"; import supportedCards from "../../../images/supportedCards.png"; import { API } from "aws-amplify"; import { Navigate } from "react-router-dom"; interface CheckoutFormProps {} interface CheckoutFormState { card: string; expDate: string | undefined; ccv: string; isLoading: boolean; toCart: boolean; orders: any[]; toConfirm: boolean; } export class CheckoutForm extends React.Component { constructor(props: CheckoutFormProps) { super(props); this.state = { card: '1010101010101010', expDate: undefined, ccv: '123', isLoading: true, toCart: false, orders: [], toConfirm: false, }; } async componentDidMount() { try { const orders = await this.listOrdersInCart(); this.setState({ orders: orders }); } catch (e) { alert(e); } this.setState({ isLoading: false }); } listOrdersInCart() { return API.get("cart", "/cart", null); } getOrderTotal = () => { return this.state.orders.reduce((total, book) => { return total + book.price * book.quantity }, 0).toFixed(2); } getCardNumberValidationState() { const length = this.state.card.length; if (length >= 15 && length <= 19) return 'success'; else if (length !== 0 && (length < 15 || length > 19)) return 'error'; return null; } handleChange = (event: React.FormEvent) => { const target = event.target as HTMLInputElement this.setState({ ...this.state, [target.name as any]: target.value }); } onCheckout = () => { const orders = this.state.orders; API.post("orders", "/orders", { body: { books: orders } }).then(() => this.setState({ toConfirm: true })); } render() { if (this.state.toConfirm) return if (this.state.isLoading) return null; return (
Supported cards
Card number
Expiration date CCV
); } } export default CheckoutForm;