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

hw 4 #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

hw 4 #50

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
15,553 changes: 15,553 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"react-scripts": "4.0.3",
"redux": "^4.1.2",
"redux-devtools-extension": "^2.13.9",
"reselect": "^4.1.5"
"reselect": "^4.1.5",
"uuid": "^8.3.2"
},
"scripts": {
"start": "react-scripts start",
Expand Down
6 changes: 4 additions & 2 deletions src/components/basket/basket.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ function Basket({ title = 'Basket', total, orderProducts }) {
);
}

export default connect((state) => {
const mapStateToProps = (state) => {
return {
total: totalSelector(state),
orderProducts: orderProductsSelector(state),
};
})(Basket);
};

export default connect(mapStateToProps)(Basket);
6 changes: 1 addition & 5 deletions src/components/menu/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import styles from './menu.module.css';

class Menu extends Component {
static propTypes = {
menu: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string.isRequired,
}).isRequired
).isRequired,
menu: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
};

state = { error: null };
Expand Down
5 changes: 3 additions & 2 deletions src/components/product/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
import styles from './product.module.css';
import Button from '../button';
import { decrement, increment } from '../../redux/actions';
import { amountSelector, productSelector } from '../../redux/selectors';

function Product({ product, amount, decrement, increment, fetchData }) {
useEffect(() => {
Expand Down Expand Up @@ -57,8 +58,8 @@ Product.propTypes = {
};

const mapStateToProps = (state, props) => ({
amount: state.order[props.id] || 0,
product: state.products[props.id],
amount: amountSelector(state, props),
product: productSelector(state, props),
});

// const mapDispatchToProps = {
Expand Down
37 changes: 20 additions & 17 deletions src/components/restaurant/restaurant.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import Menu from '../menu';
import Reviews from '../reviews';
import {
averageRatingSelector,
restaurantSelector,
} from '../../redux/selectors';

import Banner from '../banner';
import Menu from '../menu';
import PropTypes from 'prop-types';
import Rate from '../rate';
import Reviews from '../reviews';
import Tabs from '../tabs';
import { connect } from 'react-redux';
import { useState } from 'react';

const Restaurant = ({ restaurant }) => {
const Restaurant = ({ restaurant, averageRating }) => {
const { id, name, menu, reviews } = restaurant;

const [activeTab, setActiveTab] = useState('menu');

const averageRating = useMemo(() => {
const total = reviews.reduce((acc, { rating }) => acc + rating, 0);
return Math.round(total / reviews.length);
}, [reviews]);

const tabs = [
{ id: 'menu', label: 'Menu' },
{ id: 'reviews', label: 'Reviews' },
Expand All @@ -28,7 +29,7 @@ const Restaurant = ({ restaurant }) => {
</Banner>
<Tabs tabs={tabs} activeId={activeTab} onChange={setActiveTab} />
{activeTab === 'menu' && <Menu menu={menu} key={id} />}
{activeTab === 'reviews' && <Reviews reviews={reviews} />}
{activeTab === 'reviews' && <Reviews reviews={reviews} restId={id} />}
</div>
);
};
Expand All @@ -38,12 +39,14 @@ Restaurant.propTypes = {
id: PropTypes.string.isRequired,
name: PropTypes.string,
menu: PropTypes.array,
reviews: PropTypes.arrayOf(
PropTypes.shape({
rating: PropTypes.number.isRequired,
}).isRequired
).isRequired,
reviews: PropTypes.array,
}).isRequired,
averageRating: PropTypes.number,
};

export default Restaurant;
const mapStateToProps = (state, props) => ({
restaurant: restaurantSelector(state, props),
averageRating: averageRatingSelector(state, props),
});

export default connect(mapStateToProps)(Restaurant);
10 changes: 3 additions & 7 deletions src/components/restaurants/restaurants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import Restaurant from '../restaurant';
import Tabs from '../tabs';
import { restaurantsListSelector } from '../../redux/selectors';

function Restaurants({ restaurants }) {
const [activeId, setActiveId] = useState(restaurants[0].id);
Expand All @@ -12,15 +13,10 @@ function Restaurants({ restaurants }) {
[restaurants]
);

const activeRestaurant = useMemo(
() => restaurants.find((restaurant) => restaurant.id === activeId),
[activeId, restaurants]
);

return (
<div>
<Tabs tabs={tabs} onChange={setActiveId} activeId={activeId} />
<Restaurant restaurant={activeRestaurant} />
<Restaurant id={activeId} />
</div>
);
}
Expand All @@ -35,7 +31,7 @@ Restaurants.propTypes = {
};

const mapStateToProps = (state) => ({
restaurants: state.restaurants,
restaurants: restaurantsListSelector(state),
});

export default connect(mapStateToProps)(Restaurants);
22 changes: 14 additions & 8 deletions src/components/reviews/review-form/review-form.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { connect } from 'react-redux';

import useForm from '../../../hooks/use-form';
import Rate from '../../rate';
import Button from '../../button';

import PropTypes from 'prop-types';
import Rate from '../../rate';
import { addReview } from '../../../redux/actions';
import { connect } from 'react-redux';
import styles from './review-form.module.css';
import useForm from '../../../hooks/use-form';

const INITIAL_VALUES = { name: '', text: '', rating: 3 };

Expand Down Expand Up @@ -51,6 +51,12 @@ const ReviewForm = ({ onSubmit }) => {
);
};

export default connect(null, () => ({
onSubmit: (values) => console.log(values), // TODO
}))(ReviewForm);
ReviewForm.propTypes = {
onSubmit: PropTypes.func.isRequired,
};

const mapDispatchToProps = (dispatch, props) => ({
onSubmit: (review) => dispatch(addReview(review, props.restId)),
});

export default connect(null, mapDispatchToProps)(ReviewForm);
13 changes: 12 additions & 1 deletion src/components/reviews/review/review.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { connect } from 'react-redux';
import PropTypes from 'prop-types';

import Rate from '../../rate';
import styles from './review.module.css';

import { reviewWitUserSelector } from '../../../redux/selectors';

const Review = ({ user, text, rating }) => (
<div className={styles.review} data-id="review">
<div className={styles.content}>
Expand Down Expand Up @@ -31,4 +34,12 @@ Review.defaultProps = {
user: 'Anonymous',
};

export default Review;
// const mapStateToProps = (state, props) => ({
// ...reviewWitUserSelector(state, props),
// });

// const mapStateToProps = (state, props) => reviewWitUserSelector(state, props);

const mapStateToProps = reviewWitUserSelector;

export default connect(mapStateToProps)(Review);
15 changes: 6 additions & 9 deletions src/components/reviews/reviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ import Review from './review';
import ReviewForm from './review-form';
import styles from './reviews.module.css';

const Reviews = ({ reviews }) => {
const Reviews = ({ reviews, restId }) => {
return (
<div className={styles.reviews}>
{reviews.map((review) => (
<Review key={review.id} {...review} />
{reviews.map((id) => (
<Review key={id} id={id} />
))}
<ReviewForm />
<ReviewForm restId={restId} />
</div>
);
};

Reviews.propTypes = {
reviews: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string.isRequired,
}).isRequired
).isRequired,
restId: PropTypes.string,
reviews: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
};

export default Reviews;
9 changes: 8 additions & 1 deletion src/redux/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { DECREMENT, INCREMENT, REMOVE } from './constants';
import { ADD_REVIEW, DECREMENT, INCREMENT, REMOVE } from './constants';

export const increment = (id) => ({ type: INCREMENT, id });
export const decrement = (id) => ({ type: DECREMENT, id });
export const remove = (id) => ({ type: REMOVE, id });

export const addReview = (review, restId) => ({
type: ADD_REVIEW,
review,
restId,
generateId: ['reviewId', 'userId'],
});
1 change: 1 addition & 0 deletions src/redux/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
export const REMOVE = 'REMOVE';
export const ADD_REVIEW = 'ADD_REVIEW';
11 changes: 11 additions & 0 deletions src/redux/middleware/generateId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { v4 as uuid } from 'uuid';

export default (store) => (next) => (action) => {
if (!action.generateId) return next(action);

const { generateId, ...rest } = action;
next({
...rest,
...generateId.reduce((acc, key) => ({ ...acc, [key]: uuid() }), {}),
});
};
4 changes: 3 additions & 1 deletion src/redux/reducer/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { combineReducers } from 'redux';
import order from './order';
import restaurants from './restaurants';
import products from './products';
import restaurants from './restaurants';
import reviews from './reviews';
import users from './users';

export default combineReducers({
order,
restaurants,
products,
reviews,
users,
});
10 changes: 3 additions & 7 deletions src/redux/reducer/products.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { arrToMap } from '../utils';
import { normalizedProducts } from '../../fixtures';

const defaultProducts = normalizedProducts.reduce(
(acc, product) => ({ ...acc, [product.id]: product }),
{}
);

export default (products = defaultProducts, action) => {
export default (state = arrToMap(normalizedProducts), action) => {
const { type } = action;

switch (type) {
default:
return products;
return state;
}
};
20 changes: 16 additions & 4 deletions src/redux/reducer/restaurants.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import { normalizedRestaurants as defaultRestaurants } from '../../fixtures';
import { ADD_REVIEW } from '../constants';
import { arrToMap } from '../utils';
import { normalizedRestaurants } from '../../fixtures';

export default (restaurants = defaultRestaurants, action) => {
const { type } = action;
export default (state = arrToMap(normalizedRestaurants), action) => {
const { type, restId, reviewId } = action;

switch (type) {
case ADD_REVIEW:
const restaurant = state[restId];
return {
...state,
[restId]: {
...restaurant,
reviews: [...restaurant.reviews, reviewId],
},
};

default:
return restaurants;
return state;
}
};
16 changes: 12 additions & 4 deletions src/redux/reducer/reviews.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { normalizedReviews as defaultReviews } from '../../fixtures';
import { ADD_REVIEW } from '../constants';
import { arrToMap } from '../utils';
import { normalizedReviews } from '../../fixtures';

export default (reviews = defaultReviews, action) => {
const { type } = action;
export default (state = arrToMap(normalizedReviews), action) => {
const { type, review, reviewId, userId } = action;

switch (type) {
case ADD_REVIEW:
const { text, rating } = review;
return {
...state,
[reviewId]: { id: reviewId, userId, text, rating },
};
default:
return reviews;
return state;
}
};
19 changes: 19 additions & 0 deletions src/redux/reducer/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ADD_REVIEW } from '../constants';
import { arrToMap } from '../utils';
import { normalizedUsers } from '../../fixtures';

export default (state = arrToMap(normalizedUsers), action) => {
const { type, review, userId } = action;

switch (type) {
case ADD_REVIEW:
const { name } = review;
return {
...state,
[userId]: { id: userId, name },
};

default:
return state;
}
};
Loading