-
Notifications
You must be signed in to change notification settings - Fork 13
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
Ht7 #55
base: master
Are you sure you want to change the base?
Ht7 #55
Changes from 1 commit
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 |
---|---|---|
@@ -1,29 +1,54 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { Link } from 'react-router-dom'; | ||
import { connect, useDispatch, useSelector } from 'react-redux'; | ||
import { Link, useHistory } from 'react-router-dom'; | ||
import { createStructuredSelector } from 'reselect'; | ||
import { CSSTransition, TransitionGroup } from 'react-transition-group'; | ||
|
||
import './basket.css'; | ||
import styles from './basket.module.css'; | ||
|
||
import Loader from '../loader'; | ||
import itemStyles from './basket-item/basket-item.module.css'; | ||
import BasketItem from './basket-item'; | ||
import Button from '../button'; | ||
import { orderProductsSelector, totalSelector } from '../../redux/selectors'; | ||
import { | ||
activeCurrencySelector, | ||
orderProductsSelector, | ||
totalSelector, | ||
} from '../../redux/selectors'; | ||
import { UserConsumer } from '../../contexts/user-context'; | ||
import { checkoutProducts } from '../../redux/actions'; | ||
|
||
function Basket({ title = 'Basket', total, orderProducts }) { | ||
// const { name } = useContext(userContext); | ||
const activeCurrency = useSelector(activeCurrencySelector); | ||
const [currencyName, currencyValue] = Object.entries(activeCurrency)[0]; | ||
const history = useHistory(); | ||
const dispatch = useDispatch(); | ||
const checkOutState = useSelector((state) => state.checkout); | ||
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. используйте отдельно написаные селекторы |
||
|
||
const onCheckoutClickHandler = () => { | ||
if (history.location.pathname === '/checkout') { | ||
dispatch(checkoutProducts(orderProducts)); | ||
} | ||
}; | ||
if (checkOutState.loading) { | ||
return <Loader />; | ||
} | ||
if (checkOutState.loaded) { | ||
history.push('/checkout/success'); | ||
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 <Redirect to='/checkout/success' /> | ||
} | ||
if (checkOutState.error) { | ||
history.replace('/checkout/error'); | ||
// return <Redirect to='/checkout/error' /> | ||
} | ||
if (!total) { | ||
return ( | ||
<div className={styles.basket}> | ||
<h4 className={styles.title}>Select a meal from the list</h4> | ||
</div> | ||
); | ||
} | ||
|
||
const totalprice = Math.round(total * currencyValue); | ||
return ( | ||
<div className={styles.basket}> | ||
{/* <h4 className={styles.title}>{`${name}'s ${title}`}</h4> */} | ||
|
@@ -52,11 +77,11 @@ function Basket({ title = 'Basket', total, orderProducts }) { | |
<p>Total</p> | ||
</div> | ||
<div className={itemStyles.info}> | ||
<p>{`${total} $`}</p> | ||
<p>{`${totalprice} ${currencyName}`}</p> | ||
</div> | ||
</div> | ||
<Link to="/checkout"> | ||
<Button primary block> | ||
<Button onClick={onCheckoutClickHandler} primary block> | ||
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. в этом случае проще сделать две отдельные кнопки |
||
checkout | ||
</Button> | ||
</Link> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,17 +7,28 @@ import { | |
LOAD_REVIEWS, | ||
LOAD_PRODUCTS, | ||
LOAD_USERS, | ||
CHECKOUT_PRODUCTS, | ||
REQUEST, | ||
SUCCESS, | ||
FAILURE, | ||
CLEAR_BASKET, | ||
SET_CURRENCY, | ||
} from './constants'; | ||
import { | ||
usersLoadingSelector, | ||
usersLoadedSelector, | ||
reviewsLoadingSelector, | ||
reviewsLoadedSelector, | ||
} from './selectors'; | ||
|
||
export const increment = (id) => ({ type: INCREMENT, payload: { id } }); | ||
export const decrement = (id) => ({ type: DECREMENT, payload: { id } }); | ||
export const remove = (id) => ({ type: REMOVE, payload: { id } }); | ||
export const clearBasket = () => ({ type: CLEAR_BASKET }); | ||
|
||
export const setCurrency = (currencyName) => ({ | ||
type: SET_CURRENCY, | ||
payload: currencyName, | ||
}); | ||
|
||
export const addReview = (review, restaurantId) => ({ | ||
type: ADD_REVIEW, | ||
|
@@ -63,3 +74,25 @@ export const loadUsers = () => async (dispatch, getState) => { | |
|
||
dispatch(_loadUsers()); | ||
}; | ||
|
||
export const checkoutProducts = (basketItems) => async (dispatch) => { | ||
const itemsArr = basketItems.map((basketItem) => { | ||
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 { id: basketItem.product.id, amount: basketItem.amount }; | ||
}); | ||
|
||
dispatch({ type: CHECKOUT_PRODUCTS + REQUEST }); | ||
|
||
const request = await fetch('/api/order', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify([...itemsArr]), | ||
}); | ||
const response = await request.json(); | ||
if (response !== 'ok') { | ||
dispatch({ type: CHECKOUT_PRODUCTS + FAILURE, payload: response }); | ||
} else { | ||
dispatch({ type: CHECKOUT_PRODUCTS + SUCCESS, payload: response }); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
export const INCREMENT = 'INCREMENT'; | ||
export const DECREMENT = 'DECREMENT'; | ||
export const REMOVE = 'REMOVE'; | ||
export const CLEAR_BASKET = 'CLEAR_BASKET'; | ||
|
||
export const ADD_REVIEW = 'ADD_REVIEW'; | ||
|
||
export const SET_CURRENCY = 'SET_CURRENCY'; | ||
|
||
export const LOAD_RESTAURANTS = 'LOAD_RESTAURANTS'; | ||
export const LOAD_PRODUCTS = 'LOAD_PRODUCTS'; | ||
export const LOAD_REVIEWS = 'LOAD_REVIEWS'; | ||
export const LOAD_USERS = 'LOAD_USERS'; | ||
|
||
export const CHECKOUT_PRODUCTS = 'CHECKOUT_PRODUCTS'; | ||
|
||
export const REQUEST = '_REQUEST'; | ||
export const SUCCESS = '_SUCCESS'; | ||
export const FAILURE = '_FAILURE'; |
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.
вот это все вычисление повторяется много раза и его лучше вынести в отдельный файл, я покажу на своей домашке, как это лучше делать