Skip to content

Commit

Permalink
Added a book with redux
Browse files Browse the repository at this point in the history
  • Loading branch information
AsumVictor committed Jul 27, 2023
1 parent b1cc6f5 commit 5cbe4ba
Showing 1 changed file with 53 additions and 11 deletions.
64 changes: 53 additions & 11 deletions src/components/Form.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,55 @@
import React from 'react';
import Button from './Button';

const Form = () => (
<form>
<h3>ADD NEW BOOK</h3>
<input type="text" placeholder="Book title" />
<input type="text" placeholder="Author" />
<Button name="Submit" />
</form>
);
import React, { useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import { toast } from "react-toastify";
import { AddBook } from "../redux/book/bookSlice";

const Form = () => {
const { books } = useSelector((state) => state.book);
const dispatch = useDispatch();
const [author, setAuthor] = useState("");
const [title, setTitle] = useState("");

const handleSubmit = (e) => {
e.preventDefault();
if (title.trim() === "" || author.trim() === "") {
toast.error("All fileds must be completed");
return null;
}

dispatch(
AddBook({
item_id: `item${books.length + 1}`,
author: author,
title: title,
})

);
setAuthor('')
setTitle('')
};

return (
<form className="flex flex-col gap-2" onSubmit={handleSubmit}>
<h3>ADD NEW BOOK</h3>
<input
type="text"
placeholder="Book title"
className="border outline-none border-blue-300"
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
<input
type="text"
placeholder="Author"
className="border outline-none border-blue-300"
value={author}
onChange={(e) => setAuthor(e.target.value)}
/>
<button className="px-3 py-1 bg-blue-700 text-white mx-4 rounded-md">
Add book
</button>
</form>
);
};

export default Form;

0 comments on commit 5cbe4ba

Please sign in to comment.