Skip to content

Commit 00b293f

Browse files
committed
refactor bookstore-client
1 parent 4fb376e commit 00b293f

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

bookstore-client/src/api.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,23 @@ export async function fetchBooks(): Promise<Book[]> {
1818
const books = await res.json();
1919
return books || [];
2020
}
21+
22+
export interface CheckoutFormValues {
23+
address: string;
24+
cardNum: string;
25+
code: string;
26+
email: string;
27+
phone: string;
28+
}
29+
30+
export interface CheckoutDTO {
31+
items: CartItem[];
32+
cartUserInformation: CheckoutFormValues;
33+
}
34+
35+
export async function postCart(body: CheckoutDTO) {
36+
return fetch(`${BASE_API}/api/checkout`, {
37+
method: "POST",
38+
body: JSON.stringify(body),
39+
});
40+
}

bookstore-client/src/components/Checkout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22
import { useForm } from "react-hook-form";
3-
import { CheckoutFormValues } from "./interfaces";
3+
import { CheckoutFormValues } from "../api";
44

55
interface Props {
66
onSubmit: (data: CheckoutFormValues) => Promise<void>;

bookstore-client/src/components/Store.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ import React, { useEffect, useState } from "react";
22
import { BookComponent } from "./Book";
33
import { Cart } from "./CartItem";
44
import { Checkout } from "./Checkout";
5-
import { CheckoutFormValues, CheckoutDTO } from "./interfaces";
6-
import { fetchBooks, Book, CartItem } from "../api";
5+
import {
6+
fetchBooks,
7+
Book,
8+
CartItem,
9+
postCart,
10+
CheckoutFormValues,
11+
CheckoutDTO,
12+
} from "../api";
713

814
export function Store() {
915
const [books, setBooks] = useState(new Map<string, Book>());
@@ -25,11 +31,7 @@ export function Store() {
2531
items: cart,
2632
cartUserInformation: values,
2733
};
28-
console.log(body);
29-
const res = await fetch("http://localhost:8080/api/checkout", {
30-
method: "POST",
31-
body: JSON.stringify(body),
32-
});
34+
const res = await postCart(body);
3335
if (res.status == 200) {
3436
alert("Order requested!");
3537
getBooks();

bookstore-client/src/components/interfaces.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)