-
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.
Merge pull request #3 from AsumVictor/ft-react-redux
React Redux
- Loading branch information
Showing
7 changed files
with
117 additions
and
36 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
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
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,13 +1,57 @@ | ||
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, | ||
title, | ||
}), | ||
|
||
); | ||
setAuthor(''); | ||
setTitle(''); | ||
|
||
return null; | ||
}; | ||
|
||
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 type="submit" className="px-3 py-1 bg-blue-700 text-white mx-4 rounded-md"> | ||
Add book | ||
</button> | ||
</form> | ||
); | ||
}; | ||
|
||
export default Form; |
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,11 +1,15 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import './index.css'; | ||
import { Provider } from 'react-redux'; | ||
import App from './App'; | ||
import store from './redux/store'; | ||
|
||
const root = ReactDOM.createRoot(document.getElementById('root')); | ||
root.render( | ||
<React.StrictMode> | ||
<App /> | ||
<Provider store={store}> | ||
<App /> | ||
</Provider> | ||
</React.StrictMode>, | ||
); |
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