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

Ht7 #92

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

Ht7 #92

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

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions src/components/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ import { Redirect, Route, Switch } from 'react-router-dom';
import Restaurants from '../restaurants';
import Header from '../header';
import Basket from '../basket';
import { UserProvider } from '../../contexts/user-context';
import Error from '../error';
import { CurrencyProvider } from '../../contexts/currency-context';
import { useState } from 'react';

const App = () => {
const [name, setName] = useState('Andrey');
const [currency, setCurrency] = useState('$');
return (
<div>
<UserProvider value={{ name, setName }}>
<CurrencyProvider value={{ currency, setCurrency }}>
<Header />
<Switch>
<Redirect exact from="/" to="/restaurants" />
<Route path="/checkout" component={Basket} />
<Route path="/restaurants" component={Restaurants} />
<Route path="/error" component={() => <h2>Error Page!</h2>} />
<Route path="/error" component={Error} />
<Route path="/success" component={() => <h2 style={{ 'text-align': 'center' }}>Thanks for your order!</h2>} />
<Route component={() => <h2>404 - Not found :(</h2>} />
</Switch>
</UserProvider>
</div>
</CurrencyProvider>
</div >
);
};

Expand Down
5 changes: 3 additions & 2 deletions src/components/basket/basket-item/basket-item.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { connect } from 'react-redux';
import cn from 'classnames';

import { Link } from 'react-router-dom';
import { increment, decrement, remove } from '../../../redux/actions';
import Button from '../../button';
import styles from './basket-item.module.css';
import Currency from '../../currency/'

function BasketItem({
product,
Expand All @@ -25,7 +26,7 @@ function BasketItem({
<span className={styles.count}>{amount}</span>
<Button onClick={increment} icon="plus" secondary small />
</div>
<p className={cn(styles.count, styles.price)}>{subtotal} $</p>
<Currency cost={subtotal} />
<Button onClick={remove} icon="delete" secondary small />
</div>
</div>
Expand Down
41 changes: 28 additions & 13 deletions src/components/basket/basket.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import { Link, Route, Switch } from 'react-router-dom';
import { CSSTransition, TransitionGroup } from 'react-transition-group';

import styles from './basket.module.css';
Expand All @@ -8,11 +8,16 @@ 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 { UserConsumer } from '../../contexts/user-context';
import { placeOrder } from '../../redux/actions';
import Currency from '../currency/currency';

function Basket({ title = 'Basket', total, orderProducts }) {
function Basket({ title = 'Basket', total, orderProducts, placeOrder }) {
// const { name } = useContext(userContext);

// function placeOrder() {
// console.log(orderProducts)
// }

if (!total) {
return (
<div className={styles.basket}>
Expand All @@ -23,9 +28,7 @@ function Basket({ title = 'Basket', total, orderProducts }) {

return (
<div className={styles.basket}>
<h4 className={styles.title}>
<UserConsumer>{({ name }) => `${name}'s ${title}`}</UserConsumer>
</h4>
<h4 className={styles.title}>{title}</h4>
{/* <h4 className={styles.title}>{`${name}'s ${title}`}</h4> */}
<TransitionGroup>
{orderProducts.map(({ product, amount, subtotal, restId }) => (
Expand All @@ -49,14 +52,24 @@ function Basket({ title = 'Basket', total, orderProducts }) {
<p>Total</p>
</div>
<div className={itemStyles.info}>
<p>{`${total} $`}</p>
<Currency cost={total} />

</div>
</div>
<Link to="/checkout">
<Button primary block>
checkout
</Button>
</Link>
<Switch>
<Route path="/checkout">
<Button primary block onClick={placeOrder}>
place an order
</Button>
</Route>
<Route>
<Link to="/checkout">
<Button primary block>
go to cart
</Button>
</Link>
</Route>
</Switch>
</div>
);
}
Expand All @@ -68,4 +81,6 @@ const mapStateToProps = (state) => {
};
};

export default connect(mapStateToProps)(Basket);
const mapDispatchToProps = { placeOrder }

export default connect(mapStateToProps, mapDispatchToProps)(Basket);
21 changes: 21 additions & 0 deletions src/components/currency/currency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useContext } from 'react';
import { currencyContext } from '../../contexts/currency-context';
import cn from 'classnames';
import styles from '../basket/basket-item/basket-item.module.css';


export default function Currency({ cost }) {
const { currency } = useContext(currencyContext);

const getConvertedCost = (value) => {
if (currency === '₽') {
return value * 71
} else if (currency === '₴') {
return value * 26
} else return cost
}

return (
<p className={cn(styles.count, styles.price)}>{getConvertedCost(cost)} {currency}</p>
)
}
3 changes: 3 additions & 0 deletions src/components/currency/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


export { default } from './currency';
21 changes: 21 additions & 0 deletions src/components/error/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import { connect } from 'react-redux';
import { errorSelector } from '../../redux/selectors';
import styles from './error.module.css';

function Error({ errorMessage }) {
return (
<div className={styles.container}>
<h1>Error!</h1>
<p>{errorMessage}</p>
</div>
)
}

const mapStateToProps = (state) => {
return {
errorMessage: errorSelector(state),
};
}

export default connect(mapStateToProps)(Error);
4 changes: 4 additions & 0 deletions src/components/error/error.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.container {
text-align: center;
}
2 changes: 2 additions & 0 deletions src/components/error/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export { default } from './error';
22 changes: 17 additions & 5 deletions src/components/header/header.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { useContext } from 'react';
import { Link } from 'react-router-dom';
import { userContext } from '../../contexts/user-context';
import { currencyContext } from '../../contexts/currency-context';
import { ReactComponent as Logo } from '../../icons/logo.svg';
import styles from './header.module.css';

const Header = () => {
const { name, setName } = useContext(userContext);
const { currency, setCurrency } = useContext(currencyContext);
const currencies = ['$', '₽', '₴']

return (
<header className={styles.header} onClick={() => setName('Igor')}>
<Link to="/restaurants">
<header className={styles.header} >

<Link className={styles.link} to="/restaurants">
<Logo />
</Link>
<h2>{name}</h2>

<div className={styles.currencies}>
{currencies.map((curr, id) =>
(<h2
key={id}
className={curr === currency && styles.active}
onClick={() => setCurrency(curr)}
>{curr}</h2>))
}
</div>

</header>
);
};
Expand Down
27 changes: 22 additions & 5 deletions src/components/header/header.module.css
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@

.header {
display: flex;
display: grid;
grid: 1fr / 1fr 1fr 1fr;
align-items: center;
justify-content: center;
height: 60px;
background: var(--black);
position: relative;
}

.link {
grid-column-start: 2;
justify-self: center;
}

.currencies {
grid-column-start: 3;
justify-self: end;
display: flex;
flex-direction: row;
margin-left: auto;
}

.header h2 {
position: absolute;
color: var(--white);
right: 20px;
top: 0;
color: white;
margin: 0 10px;
}

.active {
border-bottom: 1px solid var(--white);
}
6 changes: 5 additions & 1 deletion src/components/product/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styles from './product.module.css';
import Button from '../button';
import { decrement, increment } from '../../redux/actions';
import { amountSelector, productSelector } from '../../redux/selectors';
import Currency from '../currency/currency'

function Product({ product, amount, decrement, increment }) {
return (
Expand All @@ -12,7 +13,10 @@ function Product({ product, amount, decrement, increment }) {
<div>
<h4 className={styles.title}>{product.name}</h4>
<p className={styles.description}>{product.ingredients.join(', ')}</p>
<div className={styles.price}>{product.price} $</div>
<div className={styles.price}>
<Currency cost={product.price} />

</div>
</div>
<div>
<div className={styles.counter}>
Expand Down
15 changes: 12 additions & 3 deletions src/components/reviews/reviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Review from './review';
import Loader from '../loader';
import ReviewForm from './review-form';
import styles from './reviews.module.css';
import { CSSTransition, TransitionGroup } from 'react-transition-group';

import { loadReviews, loadUsers } from '../../redux/actions';
import {
Expand All @@ -29,9 +30,17 @@ const Reviews = ({

return (
<div className={styles.reviews}>
{reviews.map((id) => (
<Review key={id} id={id} />
))}
<TransitionGroup>
{reviews.map((id) => (
<CSSTransition
key={id}
timeout={500}
classNames='review'
>
<Review id={id} />
</CSSTransition>
))}
</TransitionGroup>
<ReviewForm restId={restId} />
</div>
);
Expand Down
21 changes: 21 additions & 0 deletions src/components/reviews/reviews.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,24 @@
max-width: 884px;
width: 100%;
}

.review-enter {
opacity: 1;
transform: scale(0.9);
}

.review-enter-active {
opacity: 1;
transform: translateX(0);
transition: opacity 500ms, transform 500ms;
}

.review-exit {
opacity: 1;
}

.review-exit-active {
opacity: 1;
transform: scale(0.9);
transition: opacity 500ms, transform 500ms;
}
7 changes: 7 additions & 0 deletions src/contexts/currency-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import { createContext } from 'react';

export const currencyContext = createContext('Default currency');

export const CurrencyProvider = currencyContext.Provider;
export const CurrencyConsumer = currencyContext.Consumer;
Loading