-
Notifications
You must be signed in to change notification settings - Fork 0
/
BookCheckoutPage.tsx
157 lines (138 loc) · 6.15 KB
/
BookCheckoutPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { useEffect, useState } from "react";
import BookModel from "../../models/BookModel";
import { SpinnerLoading } from "../Utils/SpinnerLoading";
import { StarsReview } from "../Utils/StarsReview";
import { CheckoutAndReviewBox } from "./CheckoutAndReviewBox";
import ReviewModel from "../../models/ReviewModel";
import { LatestReviews } from "./LatestReviews";
export const BookCheckoutPage = () => {
const [book, setBook] = useState<BookModel>();
const [isLoading, setIsLoading] = useState(true);
const [httpError, setHttpError] = useState(null);
const [reviews,setReviews] = useState<ReviewModel[]>([]);
const [totalStars, setTotalStars] = useState(0);
const [isLoadingReview, setIsLoadingReview] = useState(true);
const bookId = (window.location.pathname).split('/')[2];
useEffect(() => {
const fetchBook = async () => {
const baseUrl: string = `http://localhost:8080/api/books/${bookId}`;
const response = await fetch(baseUrl);
if (!response.ok) {
throw new Error('Something went wrong!');
}
const responseJson = await response.json();
const loadedBook: BookModel = {
id: responseJson.id,
title: responseJson.title,
author: responseJson.author,
description: responseJson.description,
copies: responseJson.copies,
copiesAvailable: responseJson.copiesAvailable,
category: responseJson.category,
img: responseJson.img,
};
setBook(loadedBook);
setIsLoading(false);
};
fetchBook().catch((error: any) => {
setIsLoading(false);
setHttpError(error.message);
})
}, []);
useEffect(() => {
const fetchBookReviews = async () => {
const reviewUrl: string =`http://localhost:8080/api/reviews/search/findByBookId?bookId=${bookId}`;
const responseReviews = await fetch(reviewUrl);
if(!responseReviews.ok){
throw new Error('Something went wrong');
}
const responseJsonReviews = await responseReviews.json();
const responseData = responseJsonReviews._embedded.reviews;
const loadedReviews: ReviewModel[] =[];
let weightedStarReviews: number =0;
for(const key in responseData){
loadedReviews.push({
id: responseData[key].id,
userEmail: responseData[key].userEmail,
date: responseData[key].date,
rating: responseData[key].rating,
book_id: responseData[key].bookId,
reviewDescription: responseData[key].reviewDescription
});
weightedStarReviews = weightedStarReviews + responseData[key].rating;
}
if(loadedReviews){
const round =(Math.round((weightedStarReviews/loadedReviews.length)*2)/2).toFixed(1);
setTotalStars(Number(round));
}
setReviews(loadedReviews);
setIsLoadingReview(false);
};
fetchBookReviews().catch((error:any)=>{
setIsLoadingReview(false);
setHttpError(error.message);
})
},[]);
if (isLoading || isLoadingReview) {
return (
<div className='conatiner m-5'>
<SpinnerLoading />
</div>
)
}
if (httpError) {
return (
<div className='conatiner m-5'>
<p>{httpError}</p>
</div>
)
}
return (
<div>
<div className='container d-none d-lg-block'>
<div className='row mt-5'>
<div className='col-sm-2 col-md-2'>
{book?.img ?
<img src={book?.img} width='226' height='349' alt='Book' />
:
<img src={require('./../../Images/BooksImages/book-luv2code-1000.png')} width='226'
height='349' alt='Book' />
}
</div>
<div className='col-4 col-md-4 container'>
<div className='ml-2'>
<h2>{book?.title}</h2>
<h5 className='text-primary'>{book?.author}</h5>
<p className='lead'>{book?.description}</p>
<StarsReview rating={totalStars} size={32} />
</div>
</div>
<CheckoutAndReviewBox book={book} mobile={false} />
</div>
<hr />
<LatestReviews reviews={reviews} bookId={book?.id} mobile={false} />
</div>
<div className='container d-lg-none mt-5'>
<div className='d-flex justify-content-center alighn-items-center'>
{book?.img ?
<img src={book?.img} width='226' height='349' alt='Book' />
:
<img src={require('./../../Images/BooksImages/book-luv2code-1000.png')} width='226'
height='349' alt='Book' />
}
</div>
<div className='mt-4'>
<div className='ml-2'>
<h2>{book?.title}</h2>
<h5 className='text-primary'>{book?.author}</h5>
<p className='lead'>{book?.description}</p>
<StarsReview rating={totalStars} size={32} />
</div>
</div>
<CheckoutAndReviewBox book={book} mobile={true} />
<hr />
<LatestReviews reviews={reviews} bookId={book?.id} mobile={true} />
</div>
</div>
);
}