Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full stack code test Jose Ramón Bohopo #4

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
edit, styled-components, forms..
  • Loading branch information
JoseBohopo committed Nov 6, 2021
commit f010d788ac6cee33ebe0abb77059f7d87641873a
6 changes: 6 additions & 0 deletions client/src/App.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
.body{
font-family: 'Roboto', sans-serif !important;

}

.App {
text-align: center;
font-family: 'Roboto', sans-serif !important;
}

.App-logo {
Expand Down
10 changes: 5 additions & 5 deletions client/src/components/Routes/Index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// import { Route, Router } from "react-router"
import { Routes, Route } from "react-router-dom";
import BookDetails from "../pages/BookDetails/BookDetails";
import BooksList from "../pages/BooksList/BooksList";
import BookDetails from "../pages/Book/BookList/BookDetails/BookDetails";
import BooksList from "../pages/Book/BookList/BookList";
import Home from "../pages/Home/Home"


const AllRoutes = (props) => {

return(
return (
<Routes>
<Route exact path="/" element={<Home />} />
<Route exact path="/books" element={<BooksList/>} />
<Route path="/books/:id" element={<BookDetails {...props} />} />
<Route exact path="/books" element={<BooksList />} />
<Route path="/books/:id" element={<BookDetails {...props} />} />
</Routes>
)
}
Expand Down
21 changes: 9 additions & 12 deletions client/src/components/layout/Navigation.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { Container, Nav, Navbar } from "react-bootstrap";
import { Nav, Navbar } from "react-bootstrap";
import "./Navigation.css"


const Navigation = () => {

const title1 = "Books"

return(
return (
<Navbar collapseOnSelect expand="lg" bg="dark" variant="dark">

<Navbar.Brand className="logo" align="start" href="/">Soamee</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Container >
<Nav className="me-auto">
<Nav.Link href="/books">{title1}</Nav.Link>
<Navbar.Brand className="logo" align="start" href="/">Soamee</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav >
<Nav.Link href="/books">{title1}</Nav.Link>
</Nav>
</Container>
</Navbar.Collapse>

</Navbar.Collapse>
</Navbar>
)
}
Expand Down
47 changes: 47 additions & 0 deletions client/src/components/pages/Book/BookList/AddBook/AddBook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Button from "@restart/ui/esm/Button"
import { useState } from "react"
import { Container, Modal } from "react-bootstrap"
import { ButtonStyled } from "./UI/AddBook.style"
import FormAddBook from "./FormAddBook/FormAddBook"


const AddBook = () => {

const [showForm, setShowForm] = useState(false)

const openModal = () => {

setShowForm(true)
}

const closeModal = () => {

setShowForm(false)
}

return (
<Container className="d-flex justify-content-center">
<ButtonStyled>
<Button
block
className="mt-5 mb-5 btn btn-add add-book"
onClick={() => openModal()}
>
Add a Book
</Button>
</ButtonStyled>
<Modal show={showForm} onHide={() => closeModal()}>
<Modal.Header closeButton>
<Modal.Title className="modal__title">Would you like to add a book ?</Modal.Title>
</Modal.Header>
<Modal.Body>
<FormAddBook
closeModal={closeModal}
/>
</Modal.Body>
</Modal>
</Container>
)
}

export default AddBook
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import Button from "@restart/ui/esm/Button"
import { useState } from "react"
import { Form, Row } from "react-bootstrap"
import AuthorService from "../../../../../../services/authors.service"
import BookService from "../../../../../../services/books.service"

const authorService = new AuthorService()
const bookService = new BookService()


const FormAddBook = ({ closeModal }) => {

const [name, setName] = useState("")
const [isbn, setIsbn] = useState(0)
const [first_name, setFirst_name] = useState("")
const [last_name, setLast_name] = useState("")
const [avatar, setavatar] = useState("https://images.fineartamerica.com/images/artworkimages/mediumlarge/2/ancient-books-bernard-jaubert.jpg")

const clearState = () => {

setName("")
setIsbn("")
setFirst_name("")
setLast_name("")
}

const handleChange = (e) => {

const { value, name } = e.target

switch (name) {

case "name":
setName(value);
break;
case "isbn":
setIsbn(value);
break;
case "first_name":
setFirst_name(value);
break;
case "last_name":
setLast_name(value);
break;
default:
break;
}
}

const handleSubmit = (e) => {

e.preventDefault()

authorService
.createAuthor({ first_name, last_name })
.then(res => {

const author = res.data.author[0]._id

return bookService.createBook({ name, isbn, author })
})
.then(book => alert(`The book ${book.data.bookCreated.name} has been created.`))
.catch(err => alert(err.response.data.message))

clearState()
closeModal()

}

return (

<Form className="container send__message" onSubmit={handleSubmit}>
<Row className="justify-content-center d-flex">
<h6 className="text-center">
Add a book <img width="100%" src={avatar} alt="Books avatar" />
</h6>
</Row>
<Row>
<Form.Group className="mb-3" controlId="subject">
<Form.Label>Title:* </Form.Label>

<Form.Control
className="rounded-pill"
onChange={(e) => handleChange(e)}
name="name"
value={name}
type="text"
placeholder="Add the Title here."
/>
</Form.Group>

<Form.Group className="mb-3" controlId="content">
<Form.Label>Isbn:* </Form.Label>
<Form.Control
className="rounded-pill"
onChange={(e) => handleChange(e)}
name="isbn"
value={isbn}
type="number"
placeholder="Add the isbn here"
/>
</Form.Group>

<hr />

<Row className="justify-content-center d-flex">
<h6 className="text-center">
Add it's Author
</h6>
</Row>

<Form.Group className="mb-3" controlId="content">
<Form.Label>Firstname:* </Form.Label>
<Form.Control
className="rounded-pill"
onChange={(e) => handleChange(e)}
name="first_name"
value={first_name}
type="text"
placeholder="Add the firstname here"
/>
</Form.Group>

<Form.Group className="mb-3" controlId="content">
<Form.Label>Lastname:* </Form.Label>
<Form.Control
className="rounded-pill"
onChange={(e) => handleChange(e)}
name="last_name"
value={last_name}
type="text"
placeholder="Add the lastname here"
/>
</Form.Group>


<Button className="rounded-pill mt-3" variant="success" type="submit">
Submit
</Button>
</Row>
</Form>

)
}

export default FormAddBook
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import styled from "styled-components";

export const ButtonStyled = styled.div`

.btn-add {

border-radius: 4px;
margin: 2em;
box-shadow: 0.05rem 0.1rem 0.3rem -0.03rem rgba(0, 0, 0, 0.45);
background-color: #326173;
color: #fff;
:hover{
background-color: #6D96A6;
}
}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { useEffect, useState } from "react"
import { useParams } from "react-router"
import { Link } from "react-router-dom"
import BookService from "../../../../../services/books.service"
import EditBook from "./EditBook/EditBook"
import { CardItem, EditContainer } from "./UI/BookDetails.style"
const bookService = new BookService()


const BookDetails = props => {

const { id } = useParams()
const [book, setBook] = useState({})
const fullName = `${book.author?.first_name} ${book.author?.last_name}`
let [showForm, setShowForm] = useState(false)

useEffect(() => {

bookService
.findSingleBook(id)
.then(singleBook => setBook(singleBook.data.bookFound))
.catch(err => console.log(err.message))
}, [showForm])

const editForm = async () => {

showForm ? await setShowForm(false) : setShowForm(true)
}

if (showForm) {

return <EditBook item={book} onShowForm={editForm} />
}

return (
<CardItem>

<h2>
Title: {book.name}
</h2>
<p>
Author: {fullName}
</p>
<span>
Isbn: {book.isbn}
</span>

<EditContainer>
<button className="btn btn-edit" onClick={editForm}>
Edit Book
</button>
<Link className="btn btn-back" to={"/books"}>
Back to Books
</Link>
</EditContainer>
</CardItem>
)
}

export default BookDetails

Loading