-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add BookList UI with fake data and books factory
- Install @faker-js/faker dependency for generating fake data - Add BookList component with mock data using the books factory - Add custom favicon to the app
- Loading branch information
Amaro Mariño
committed
Oct 16, 2023
1 parent
ebdd859
commit c0602fb
Showing
16 changed files
with
190 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
const App = () => <h1>Welcome to Bukie!</h1>; | ||
import { Booklist } from "./components/"; | ||
|
||
const App = () => <Booklist />; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import styled from "@emotion/styled"; | ||
import { Card, CardMedia, CardMediaProps } from "@mui/material"; | ||
export const Root = styled.div` | ||
display: flex; | ||
justify-self: center; | ||
align-items: center; | ||
`; | ||
|
||
export const CardWrapper = styled(Card)` | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: space-between; | ||
width: 80%; | ||
margin: 0 auto; | ||
margin-bottom: 36px; | ||
padding: 8px; | ||
border: 1px solid #ccc; | ||
border-radius: 8px; | ||
`; | ||
|
||
export const Cover = styled(CardMedia)<CardMediaProps>` | ||
height: auto; | ||
width: 100%; | ||
margin-bottom: 4px; | ||
border-radius: 8px; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { render } from "@testing-library/react"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { Book } from "../../data/books"; | ||
import BookList from "./BookList"; | ||
|
||
describe("BookList", () => { | ||
it("renders a list of books", () => { | ||
vi.mock("../../data/books", () => ({ | ||
books: [ | ||
{ | ||
title: "The Great Gatsby", | ||
author: "F. Scott Fitzgerald", | ||
year: "1925", | ||
image: "https://picsum.photos/150/150", | ||
rating: 80, | ||
}, | ||
{ | ||
title: "To Kill a Mockingbird", | ||
author: "Harper Lee", | ||
year: "1960", | ||
image: "https://picsum.photos/150/150", | ||
rating: 90, | ||
}, | ||
{ | ||
title: "1984", | ||
author: "George Orwell", | ||
year: "1949", | ||
image: "https://picsum.photos/150/150", | ||
rating: 70, | ||
}, | ||
] as Book[], | ||
})); | ||
|
||
const { getAllByRole } = render(<BookList />); | ||
const bookTitles = getAllByRole("heading").map((heading) => heading.textContent); | ||
|
||
expect(bookTitles).toEqual(["The Great Gatsby", "To Kill a Mockingbird", "1984"]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { CardActionArea, CardContent, Grid, Typography } from "@mui/material"; | ||
import { Book, books } from "../../data/books"; | ||
import { Root, CardWrapper, Cover } from "./BookList.styles"; | ||
import { CircularProgressWithLabel } from "./components"; | ||
|
||
const BookList = () => ( | ||
<Root> | ||
<Grid container spacing={2} maxWidth='100%' mx='auto'> | ||
{books.map((book: Book) => ( | ||
<Grid item xs={12} sm={6} md={4} key={book.title}> | ||
<CardWrapper> | ||
<CardActionArea> | ||
<Cover component='img' image={book.image} aria-label={book.title} /> | ||
<CardContent> | ||
<Typography gutterBottom variant='h5' component='h2' align='center'> | ||
{book.title} | ||
</Typography> | ||
<Typography variant='body2' color='textSecondary' component='p' align='center'> | ||
{book.author} ({book.year}) | ||
</Typography> | ||
</CardContent> | ||
</CardActionArea> | ||
<CircularProgressWithLabel value={book.rating} /> | ||
</CardWrapper> | ||
</Grid> | ||
))} | ||
</Grid> | ||
</Root> | ||
); | ||
|
||
export default BookList; |
32 changes: 32 additions & 0 deletions
32
src/components/BookList/components/CircularProgressWithLabel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Box, CircularProgress, CircularProgressProps, Typography } from "@mui/material"; | ||
|
||
const CircularProgressWithLabel = (props: CircularProgressProps & { value: number }) => { | ||
let color: CircularProgressProps["color"] = "error"; | ||
if (props.value >= 70) { | ||
color = "success"; | ||
} else if (props.value >= 40) { | ||
color = "warning"; | ||
} | ||
|
||
return ( | ||
<Box display='flex' alignItems='center' justifyContent='center' position='relative'> | ||
<CircularProgress variant='determinate' {...props} value={props.value} color={color} /> | ||
<Box | ||
display='flex' | ||
alignItems='center' | ||
justifyContent='center' | ||
position='absolute' | ||
top={0} | ||
bottom={0} | ||
left={0} | ||
right={0} | ||
> | ||
<Typography variant='caption' component='div' color='text.secondary'> | ||
{`${Math.round(props.value)}%`} | ||
</Typography> | ||
</Box> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default CircularProgressWithLabel; |
14 changes: 14 additions & 0 deletions
14
src/components/BookList/components/CircularProgressWithlabel.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { render } from "@testing-library/react"; | ||
import { describe, expect, it } from "vitest"; | ||
import CircularProgressWithLabel from "./CircularProgressWithLabel"; | ||
|
||
describe("CircularProgressWithLabel", () => { | ||
it("should render a circular progress bar with a label", () => { | ||
const value = 50; | ||
|
||
const { getByText } = render(<CircularProgressWithLabel value={value} />); | ||
const progress = getByText(`${value}%`); | ||
|
||
expect(progress.textContent).toBe("50%"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as CircularProgressWithLabel } from "./CircularProgressWithLabel"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as Booklist } from "./BookList"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Booklist } from "./BookList"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { faker } from "@faker-js/faker"; | ||
|
||
export type Book = { | ||
title: string; | ||
author: string; | ||
year: string; | ||
image: string; | ||
rating: number; | ||
}; | ||
|
||
export const generateBooks = (count: number): Book[] => | ||
Array.from({ length: count }, () => ({ | ||
title: faker.lorem.words(3), | ||
author: faker.person.fullName(), | ||
year: String(faker.date.past({ years: 100 }).getFullYear()), | ||
image: faker.image.urlLoremFlickr({ width: 150, height: 150, category: "book" }), | ||
rating: faker.number.int({ min: 0, max: 100 }), | ||
})); | ||
|
||
export const books: Book[] = generateBooks(30); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ body { | |
place-items: center; | ||
min-width: 320px; | ||
min-height: 100vh; | ||
justify-content: center; | ||
} | ||
|
||
h1 { | ||
|