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

React Redux Usage #4

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 15 additions & 3 deletions src/components/BookList.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import React from 'react';
import { useSelector } from 'react-redux';
import Books from './Books';

const BookList = () => {
const books = useSelector((state) => state.books);
const books = [
{
id: 1,
title: 'The pursuits of happiness',
category: 'politics',
author: 'Frank Kaso',
},
{
id: 2,
title: 'Vampire Diaries',
category: 'Horror',
author: 'Nick KLaus',
},
];

return (
<>
{books.map((book) => (
<Books
key={book.id}
id={book.id}
title={book.title}
category={book.category}
author={book.author}
/>
))}
Expand Down
18 changes: 4 additions & 14 deletions src/components/Books.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
import React from 'react';

import PropTypes from 'prop-types';
import { useDispatch } from 'react-redux';
import { removeBook } from '../redux/books/books';

import '../assets/book.css';

const Books = (props) => {
const {
id, author, title, category,
} = props;
const dispatch = useDispatch();

const deleteHandler = () => {
dispatch(removeBook(id));
};

const { id, author, title } = props;
return (
<div className="book-container">
<div key={id} className="book-row1">
<span>{category}</span>
<span>Action</span>
<h2>{title}</h2>
<p>{author}</p>
<button type="button">Comments</button>
<button type="button" onClick={deleteHandler}>Remove</button>
<button type="button">Remove</button>
<button type="button">Edit</button>
</div>

Expand All @@ -45,7 +36,6 @@ Books.propTypes = {
id: PropTypes.number.isRequired,
title: PropTypes.string.isRequired,
author: PropTypes.string.isRequired,
category: PropTypes.string.isRequired,
};

export default Books;
18 changes: 5 additions & 13 deletions src/components/Category.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import React from 'react';
import { useDispatch } from 'react-redux';
import { checkStatus } from '../redux/categories/categories';

const Category = () => {
const dispatch = useDispatch();
const checkStatusHandler = () => {
dispatch(checkStatus());
};
return (
<div>
<button type="button" onClick={checkStatusHandler}>Check Status</button>
</div>
);
};
const Category = () => (
<div>
<button type="button">Check Status</button>
</div>
);

export default Category;
19 changes: 4 additions & 15 deletions src/components/FormList.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
import React from 'react';
import { useDispatch } from 'react-redux';
import { addBook } from '../redux/books/books';

import '../assets/Form.css';

const FormList = () => {
const dispatch = useDispatch();

const registerBook = (e) => {
e.preventDefault();
const { title, author, category } = e.target.elements;
const newBook = {
title: title.value,
author: author.value,
category: category.value,
};

dispatch(addBook(newBook));
};

return (
<div className="add-form">
<h2>ADD NEW BOOK</h2>
<form onSubmit={registerBook}>
<input className="title-input" id="title" type="text" placeholder="Book title" />
<input className="author-input" id="author" placeholder="Author" />
<input className="panel-bg" id="category" placeholder="Category" />
<input className="title-input" type="text" placeholder="Book title" />
<input className="author-input" placeholder="Author" />
<input className="panel-bg" placeholder="Category" />
<button type="submit" className="add-book-btn">ADD BOOK</button>
</form>
</div>
Expand Down
12 changes: 7 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import App from './App';
import store from './redux/configureStore';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<BrowserRouter>
<Provider store={store}>
<App />
</Provider>
<App />
</BrowserRouter>
</React.StrictMode>,
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
19 changes: 0 additions & 19 deletions src/redux/books/books.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<<<<<<< HEAD
const ADD_BOOK = 'ADD_BOOK';
const REMOVE_BOOK = 'REMOVE_BOOK';

Expand All @@ -12,24 +11,6 @@ const books = (state = [], action) => {
id: state.length,
title: action.payload.title,
author: action.payload.author,
=======
import initialData from './initialData';

const ADD_BOOK = 'bookstore/src/redux/books/ADD_BOOK';
const REMOVE_BOOK = 'bookstore/src/redux/categories/REMOVE_BOOK';

export const removeBook = (id) => ({ type: REMOVE_BOOK, payload: { id } });
export const addBook = (book) => ({ type: ADD_BOOK, payload: book });

const books = (state = initialData, action) => {
switch (action.type) {
case ADD_BOOK:
return [...state, {
id: Math.floor((Math.random() * 100) + 1),
title: action.payload.title,
author: action.payload.author,
category: action.payload.category,
>>>>>>> react-redux-usage
}];
case REMOVE_BOOK:
return state.filter((book) => book.id !== action.payload.id);
Expand Down
22 changes: 0 additions & 22 deletions src/redux/books/initialData.js

This file was deleted.