Skip to content

Commit

Permalink
add userContext
Browse files Browse the repository at this point in the history
  • Loading branch information
koretskiyav committed Dec 8, 2020
1 parent 01dbe1c commit cef9805
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
22 changes: 13 additions & 9 deletions src/components/app/app.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import React, { PureComponent } from 'react';
import React, { useState } from 'react';
import { Route, Switch } from 'react-router-dom';
import Header from '../header';
import Basket from '../basket';
import RestaurantsPage from '../../pages/restaurants-page';
import { UserProvider } from '../../contexts/user-context';

export default class App extends PureComponent {
render() {
return (
<div>
const App = () => {
const [name, setName] = useState('Igor');
return (
<div>
<UserProvider value={{ name, setName }}>
<Header />
<Switch>
<Route path="/checkout" component={Basket} />
<Route path="/restaurants" component={RestaurantsPage} />
<Route path="/error" component={() => <h1>Error Page</h1>} />
<Route path="/" component={() => '404 - not found'} />
</Switch>
</div>
);
}
}
</UserProvider>
</div>
);
};

export default App;
8 changes: 7 additions & 1 deletion src/components/basket/basket.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import BasketRow from './basket-row';
import BasketItem from './basket-item';
import Button from '../button';
import { orderProductsSelector, totalSelector } from '../../redux/selectors';
import { UserConsumer } from '../../contexts/user-context';

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

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

return (
<div className={styles.basket}>
<h4 className={styles.title}>{title}</h4>
{/* <h4 className={styles.title}>{`${name}'s ${title}`}</h4> */}
<h4 className={styles.title}>
<UserConsumer>{({ name }) => `${name}'s ${title}`}</UserConsumer>
</h4>
{orderProducts.map(({ product, amount, subtotal, restaurantId }) => (
<BasketItem
product={product}
Expand Down
17 changes: 11 additions & 6 deletions src/components/header/header.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import React from 'react';
import React, { useContext } from 'react';

import Logo from './logo';
import styles from './header.module.css';
import userContext from '../../contexts/user-context';

const Header = () => (
<header className={styles.header}>
<Logo />
</header>
);
const Header = () => {
const { name, setName } = useContext(userContext);

return (
<header className={styles.header} onClick={() => setName('Ivan')}>
<Logo />
<h2>{name}</h2>
</header>
);
};
export default Header;
8 changes: 8 additions & 0 deletions src/components/header/header.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@
justify-content: center;
height: 60px;
background: var(--black);
position: relative;
}

.header h2 {
position: absolute;
color: var(--white);
right: 20px;
top: 0;
}
8 changes: 8 additions & 0 deletions src/contexts/user-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createContext } from 'react';

const userContext = createContext({ name: 'default name' });

export const UserConsumer = userContext.Consumer;
export const UserProvider = userContext.Provider;

export default userContext;

0 comments on commit cef9805

Please sign in to comment.