-
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
[ht7] post reviews, context & animations #81
base: master
Are you sure you want to change the base?
Changes from all commits
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 @@ node_modules | |
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
.idea/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import cn from 'classnames'; | |
import { increment, decrement, remove } from '../../../redux/actions'; | ||
import Button from '../../button'; | ||
import styles from './basket-item.module.css'; | ||
import { convert, CurrencyConsumer } from '../../../contexts/currency-context'; | ||
|
||
function BasketItem({ | ||
product, | ||
|
@@ -14,6 +15,7 @@ function BasketItem({ | |
increment, | ||
decrement, | ||
remove, | ||
disabled, | ||
}) { | ||
return ( | ||
<div className={styles.basketItem}> | ||
|
@@ -29,21 +31,28 @@ function BasketItem({ | |
icon="minus" | ||
secondary | ||
small | ||
disabled={disabled} | ||
/> | ||
<span className={styles.count}>{amount}</span> | ||
<Button | ||
onClick={() => increment(product.id)} | ||
icon="plus" | ||
secondary | ||
small | ||
disabled={disabled} | ||
/> | ||
</div> | ||
<p className={cn(styles.count, styles.price)}>{subtotal} $</p> | ||
<p className={cn(styles.count, styles.price)}> | ||
<CurrencyConsumer> | ||
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. не самы удобный АПИ получается у фунции конвертации, покажу как удобнее на своей домашке |
||
{({ currency }) => convert(subtotal, currency)} | ||
</CurrencyConsumer> | ||
</p> | ||
<Button | ||
onClick={() => remove(product.id)} | ||
icon="delete" | ||
secondary | ||
small | ||
disabled={disabled} | ||
/> | ||
</div> | ||
</div> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import React from 'react'; | ||
import React, { useContext } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { Link } from 'react-router-dom'; | ||
import { createStructuredSelector } from 'reselect'; | ||
import { CSSTransition, TransitionGroup } from 'react-transition-group'; | ||
|
||
|
@@ -10,11 +9,25 @@ import './basket.css'; | |
import BasketRow from './basket-row'; | ||
import BasketItem from './basket-item'; | ||
import Button from '../button'; | ||
import { orderProductsSelector, totalSelector } from '../../redux/selectors'; | ||
import { | ||
orderProductsSelector, | ||
postOrderLoadingSelector, | ||
totalSelector, | ||
} from '../../redux/selectors'; | ||
import { UserConsumer } from '../../contexts/user-context'; | ||
import { checkoutOrder } from '../../redux/actions'; | ||
import Loader from '../loader'; | ||
import currencyContext, { convert } from '../../contexts/currency-context'; | ||
|
||
function Basket({ title = 'Basket', total, orderProducts }) { | ||
function Basket({ | ||
title = 'Basket', | ||
total, | ||
orderProducts, | ||
checkoutOrder, | ||
loading, | ||
}) { | ||
// const { name } = useContext(userContext); | ||
const { currency } = useContext(currencyContext); | ||
|
||
if (!total) { | ||
return ( | ||
|
@@ -26,7 +39,7 @@ function Basket({ title = 'Basket', total, orderProducts }) { | |
|
||
return ( | ||
<div className={styles.basket}> | ||
{/* <h4 className={styles.title}>{`${name}'s ${title}`}</h4> */} | ||
{loading && <Loader />} | ||
<h4 className={styles.title}> | ||
<UserConsumer>{({ name }) => `${name}'s ${title}`}</UserConsumer> | ||
</h4> | ||
|
@@ -42,26 +55,30 @@ function Basket({ title = 'Basket', total, orderProducts }) { | |
amount={amount} | ||
subtotal={subtotal} | ||
restaurantId={restaurantId} | ||
disabled={loading} | ||
/> | ||
</CSSTransition> | ||
))} | ||
</TransitionGroup> | ||
<hr className={styles.hr} /> | ||
<BasketRow label="Sub-total" content={`${total} $`} /> | ||
<BasketRow label="Sub-total" content={convert(total, currency)} /> | ||
<BasketRow label="Delivery costs:" content="FREE" /> | ||
<BasketRow label="total" content={`${total} $`} bold /> | ||
<Link to="/checkout"> | ||
<Button primary block> | ||
checkout | ||
</Button> | ||
</Link> | ||
<BasketRow label="total" content={convert(total, currency)} bold /> | ||
<Button primary block onClick={checkoutOrder} disabled={loading}> | ||
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 - ордер мы должны делать только с этой страницы |
||
checkout | ||
</Button> | ||
</div> | ||
); | ||
} | ||
|
||
const mapStateToProps = createStructuredSelector({ | ||
total: totalSelector, | ||
orderProducts: orderProductsSelector, | ||
loading: postOrderLoadingSelector, | ||
}); | ||
|
||
export default connect(mapStateToProps)(Basket); | ||
const mapDispatchToProps = (dispatch) => ({ | ||
checkoutOrder: () => dispatch(checkoutOrder()), | ||
}); | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(Basket); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { createContext } from 'react'; | ||
|
||
export const USD = 'USD'; | ||
export const EUR = 'EUR'; | ||
export const GBP = 'GBP'; | ||
|
||
const exchangeMultiplier = { | ||
USD: 1, | ||
EUR: 0.8, | ||
GBP: 0.75, | ||
}; | ||
|
||
const exchangeSign = { | ||
USD: '$', | ||
EUR: '€', | ||
GBP: '£', | ||
}; | ||
|
||
const currencyContext = createContext({ | ||
currency: USD, | ||
}); | ||
|
||
export const CurrencyConsumer = currencyContext.Consumer; | ||
export const CurrencyProvider = currencyContext.Provider; | ||
|
||
export const convert = (price, curr) => { | ||
return `${(price * exchangeMultiplier[curr]).toFixed(1)} ${ | ||
exchangeSign[curr] | ||
}`; | ||
}; | ||
|
||
export default currencyContext; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { createStructuredSelector } from 'reselect'; | ||
import styles from './error-page.module.css'; | ||
import { errorSelector } from '../redux/selectors'; | ||
|
||
function ErrorPage({ error }) { | ||
return <h1 className={styles.error}>Error{error && `: ${error}`}</h1>; | ||
} | ||
|
||
const mapStateToProps = createStructuredSelector({ | ||
error: errorSelector, | ||
}); | ||
|
||
export default connect(mapStateToProps)(ErrorPage); |
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.
Redirect с рута лучше ставить самым первым, так мы сразу понимаем, что происходит при первом заходе на /