-
Notifications
You must be signed in to change notification settings - Fork 20
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
[ht4] normalisation fixes and review posts #65
base: master
Are you sure you want to change the base?
Changes from all commits
8ab3c44
96c37e5
f36e921
5315d31
9fcd0f2
91013d3
523acc1
cafc682
6cf13c1
904c7ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,5 @@ | |
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
.idea/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,36 @@ | ||
import React from 'react'; | ||
import React, { useEffect } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import PropTypes from 'prop-types'; | ||
import Restaurant from '../restaurant'; | ||
import Tabs from '../tabs'; | ||
import { setActive } from '../../redux/actions'; | ||
|
||
const Restaurants = ({ restaurants }) => { | ||
const tabs = restaurants.map((restaurant) => ({ | ||
const Restaurants = ({ restaurants, setActiveRestaurant }) => { | ||
useEffect(() => { | ||
setActiveRestaurant({ id: Object.keys(restaurants)[0] }); | ||
}, [restaurants, setActiveRestaurant]); | ||
|
||
const tabs = Object.values(restaurants).map((restaurant) => ({ | ||
title: restaurant.name, | ||
id: restaurant.id, | ||
content: <Restaurant restaurant={restaurant} />, | ||
})); | ||
|
||
return <Tabs tabs={tabs} />; | ||
return <Tabs tabs={tabs} onSetActiveCallback={setActiveRestaurant} />; | ||
}; | ||
|
||
Restaurants.propTypes = { | ||
restaurants: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
id: PropTypes.string.isRequired, | ||
}).isRequired | ||
).isRequired, | ||
restaurants: PropTypes.object.isRequired, | ||
}; | ||
|
||
export default connect((state) => ({ | ||
const mapStateToProps = (state) => ({ | ||
restaurants: state.restaurants, | ||
}))(Restaurants); | ||
}); | ||
|
||
const mapDispatchToProps = (dispatch) => { | ||
return { | ||
setActiveRestaurant: (entity) => dispatch(setActive(entity.id)), | ||
}; | ||
}; | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(Restaurants); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,21 @@ | ||
import { INCREMENT, DECREMENT, REMOVE } from './constants'; | ||
import { | ||
INCREMENT, | ||
DECREMENT, | ||
REMOVE, | ||
ADD_REVIEW, | ||
SET_ACTIVE, | ||
} from './constants'; | ||
|
||
/*products*/ | ||
export const increment = (id) => ({ type: INCREMENT, payload: { id } }); | ||
export const decrement = (id) => ({ type: DECREMENT, payload: { id } }); | ||
export const remove = (id) => ({ type: REMOVE, payload: { id } }); | ||
|
||
/*form*/ | ||
export const addReview = (data) => ({ type: ADD_REVIEW, payload: { ...data } }); | ||
|
||
/*restaurant*/ | ||
export const setActive = (restaurantId) => ({ | ||
type: SET_ACTIVE, | ||
payload: restaurantId, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
/*product*/ | ||
export const INCREMENT = 'INCREMENT'; | ||
export const DECREMENT = 'DECREMENT'; | ||
export const REMOVE = 'REMOVE'; | ||
|
||
/*form*/ | ||
export const ADD_REVIEW = 'ADD_REVIEW'; | ||
|
||
/*restaurant*/ | ||
export const SET_ACTIVE = 'SET_ACTIVE'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { v1 as uuidv4 } from 'uuid'; | ||
import { ADD_REVIEW } from '../constants'; | ||
|
||
const uuidGenerator = () => (next) => (action) => { | ||
if (action.type === ADD_REVIEW) { | ||
action.payload.userId = uuidv4(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не нужно мутировать action |
||
action.payload.id = uuidv4(); | ||
} | ||
next(action); | ||
}; | ||
|
||
export default uuidGenerator; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { SET_ACTIVE } from '../constants'; | ||
|
||
const reducer = (activeRestaurant = '', action) => { | ||
const { type } = action; | ||
|
||
switch (type) { | ||
case SET_ACTIVE: | ||
return action.payload; | ||
default: | ||
return activeRestaurant; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. теперь знание о какой ресторан активный храниться в двух местах, в этом редьюсере и в компоненте |
||
} | ||
}; | ||
|
||
export default reducer; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
import { normalizedRestaurants as defaultRestaurants } from '../../fixtures'; | ||
import { normalizedRestaurants } from '../../fixtures'; | ||
import { ADD_REVIEW } from '../constants'; | ||
|
||
const defaultRestaurants = normalizedRestaurants.reduce( | ||
(acc, restaurant) => ({ ...acc, [restaurant.id]: restaurant }), | ||
{} | ||
); | ||
|
||
const reducer = (restaurants = defaultRestaurants, action) => { | ||
const { type } = action; | ||
|
||
switch (type) { | ||
case ADD_REVIEW: | ||
const { id, activeRestaurant } = action.payload; | ||
const restaurant = restaurants[activeRestaurant]; | ||
restaurant.reviews.push(id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. мутации в редьюсере недопустимы |
||
return { ...restaurants }; | ||
default: | ||
return restaurants; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { normalizedUsers } from '../../fixtures'; | ||
import { ADD_REVIEW } from '../constants'; | ||
|
||
const defaultUsers = normalizedUsers.reduce( | ||
(acc, user) => ({ ...acc, [user.id]: user.name }), | ||
{} | ||
); | ||
|
||
const reducer = (users = defaultUsers, action) => { | ||
const { type } = action; | ||
|
||
switch (type) { | ||
case ADD_REVIEW: | ||
const { userId, name } = action.payload; | ||
return { ...users, [userId]: name }; | ||
default: | ||
return users; | ||
} | ||
}; | ||
|
||
export default reducer; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { applyMiddleware, createStore } from 'redux'; | ||
import { composeWithDevTools } from 'redux-devtools-extension'; | ||
import logger from './middleware/logger'; | ||
import uuidGenerator from './middleware/uuid-generator'; | ||
|
||
import reducer from './reducer'; | ||
|
||
const store = createStore( | ||
reducer, | ||
composeWithDevTools(applyMiddleware(logger)) | ||
composeWithDevTools(applyMiddleware(logger, uuidGenerator)) | ||
); | ||
|
||
export default store; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
это нужно выносить в селекторы и мемоизировать.