Skip to content

Commit

Permalink
commit passing props as id to fetch book details on backend
Browse files Browse the repository at this point in the history
  • Loading branch information
asher-lab committed Jan 19, 2023
1 parent eb7df95 commit fe5ad78
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 7 deletions.
93 changes: 93 additions & 0 deletions src/layouts/CheckoutPage/BookCheckoutPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* eslint-disable no-console */
/* eslint-disable no-underscore-dangle */

import { Container, Grid, Text } from '@mantine/core';
import Image from 'next/image';
import React, { useEffect, useState } from 'react';

import type BookModel from '@/models/BookModel';
import SpinnerLoading from '@/utils/SpinnerLoading';

type Props = {
book_id: string;
};

export default function BookCheckoutPage({ book_id }: Props) {
const [book, setBook] = useState<BookModel>();
const [isLoading, setIsLoading] = useState(true);
const [httpError, setHttpError] = useState(null);

useEffect(() => {
const fetchBooks = async () => {
const baseUrl: string = `http://localhost:8080/api/books/${book_id}/hello`;

const response = await fetch(baseUrl);

if (!response.ok) {
throw new Error('Something went wrong!');
}
const responseJson = await response.json();

const loadedBooks: 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(loadedBooks);
setIsLoading(false);
};

// catch if there are any errors in async
fetchBooks().catch((error: any) => {
setIsLoading(false);
setHttpError(error.message);
});
window.scrollTo(0, 0);
}, []); // each time the current page changes, we wanted to recall this hook.

if (isLoading) {
return (
<div>
<SpinnerLoading />
</div>
);
}

if (httpError) {
return (
<div>
<p>{httpError}</p>
</div>
);
}

return (
<div>
<h1> Value of data is {book_id}</h1>
<Grid gutter={5} gutterXs="md" gutterMd="xl" gutterXl={50}>
<Grid.Col span={4}>1</Grid.Col>
<Grid.Col span={4}>2</Grid.Col>
<Grid.Col span={4}>3</Grid.Col>
</Grid>
<Container size={'xl'} p={20}>
<Grid>
<Grid.Col span={3}>
<Image src={book?.img} alt="ImageTable" height="300" width="200" />
</Grid.Col>
<Grid.Col span={7} pl={150}>
<Text fw={700}>{book?.title}</Text>
<Text c="blue">{book?.author}</Text>
{book?.description}
</Grid.Col>
<Grid.Col span={2}>____</Grid.Col>
</Grid>
</Container>
</div>
);
}
12 changes: 6 additions & 6 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import '../styles/global.css';
import { MantineProvider } from '@mantine/core';
import type { AppProps } from 'next/app';
import Head from 'next/head';
import Script from 'next/script';

export default function App(props: AppProps) {
const { Component, pageProps } = props;
Expand All @@ -21,13 +22,12 @@ export default function App(props: AppProps) {
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous"
></link>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"
></script>
</Head>

<Script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"
/>
<MantineProvider
withGlobalStyles
withNormalizeCSS
Expand Down
17 changes: 17 additions & 0 deletions src/pages/checkout/[id].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useRouter } from 'next/router';

import BookCheckoutPage from '@/layouts/CheckoutPage/BookCheckoutPage';
import Footer from '@/layouts/NavbarAndFooter/Footer';
import Navbar from '@/layouts/NavbarAndFooter/Navbar';

export default function BookCheckoutDetails() {
const router = useRouter();
const { id } = router.query;
return (
<div>
<Navbar />
<BookCheckoutPage book_id={typeof id === 'string' ? id : ''} />
<Footer />
</div>
);
}
5 changes: 4 additions & 1 deletion src/pages/search.tsx → src/pages/search/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* eslint-disable no-empty-pattern */
import React from 'react';

import Footer from '@/layouts/NavbarAndFooter/Footer';
import Navbar from '@/layouts/NavbarAndFooter/Navbar';
import SearchBooksPage from '@/layouts/SearchBooksPage/SearchBooksPage';

export default function search({}: props) {
export default function index() {
return (
<div>
<Navbar />
Expand Down

0 comments on commit fe5ad78

Please sign in to comment.