-
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
ДЗ7 #84
base: master
Are you sure you want to change the base?
ДЗ7 #84
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 |
---|---|---|
|
@@ -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 { CurrencyConsumer } from '../../../contexts/currency-context'; | ||
|
||
function BasketItem({ | ||
product, | ||
|
@@ -38,7 +39,11 @@ function BasketItem({ | |
small | ||
/> | ||
</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 }) => `${subtotal} ${currency}`} | ||
</CurrencyConsumer> | ||
</p> | ||
<Button | ||
onClick={() => remove(product.id)} | ||
icon="delete" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { useMemo, useContext, useState } from 'react'; | ||
import Select from 'react-dropdown-select'; | ||
import CurrencyContext from '../../../contexts/currency-context'; | ||
import CURRENCIES from '../../../constants/currencies'; | ||
|
||
const CurrencySelect = ({ className }) => { | ||
const { setCurrency } = useContext(CurrencyContext); | ||
const options = useMemo( | ||
() => | ||
Object.keys(CURRENCIES).map((currency) => ({ | ||
value: currency, | ||
label: currency, | ||
})), | ||
[] | ||
); | ||
const [selectedOption, setSelectedOption] = useState([options[0]]); | ||
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 из CurrencyContext |
||
|
||
const handleOnChange = (value) => { | ||
setSelectedOption(value); | ||
setCurrency(value[0]?.value); | ||
}; | ||
|
||
return ( | ||
<Select | ||
onChange={handleOnChange} | ||
className={className} | ||
options={options} | ||
values={selectedOption} | ||
/> | ||
); | ||
}; | ||
|
||
export default CurrencySelect; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './currency-select'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
import React, { useContext } from 'react'; | ||
import React from 'react'; | ||
|
||
import Logo from './logo'; | ||
import CurrencySelect from './currency-select'; | ||
import styles from './header.module.css'; | ||
import userContext from '../../contexts/user-context'; | ||
|
||
const Header = () => { | ||
const { name, setName } = useContext(userContext); | ||
|
||
return ( | ||
<header className={styles.header} onClick={() => setName('Ivan')}> | ||
const Header = () => ( | ||
<div className={styles.wrapper}> | ||
<header className={styles.header}> | ||
<Logo /> | ||
<h2>{name}</h2> | ||
</header> | ||
); | ||
}; | ||
<CurrencySelect className={styles.currency} /> | ||
</div> | ||
); | ||
export default Header; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const CURRENCIES = { | ||
USD: 1, | ||
RUB: 73, | ||
UAH: 28, | ||
JPY: 104, | ||
}; | ||
|
||
export default CURRENCIES; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createContext } from 'react'; | ||
|
||
const currencyContext = createContext({}); | ||
|
||
export const CurrencyConsumer = currencyContext.Consumer; | ||
export const CurrencyProvider = currencyContext.Provider; | ||
|
||
export default currencyContext; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { createStructuredSelector } from 'reselect'; | ||
import { currentErrorRouteSelector } from '../redux/selectors'; | ||
|
||
function ErrorPage({ error }) { | ||
return ( | ||
<> | ||
<h1>Error Page</h1> | ||
<p>{error}</p> | ||
</> | ||
); | ||
} | ||
|
||
export default connect( | ||
createStructuredSelector({ | ||
error: currentErrorRouteSelector, | ||
}) | ||
)(ErrorPage); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import { | |
DECREMENT, | ||
REMOVE, | ||
ADD_REVIEW, | ||
CHECKOUT, | ||
LOAD_RESTAURANTS, | ||
LOAD_REVIEWS, | ||
LOAD_PRODUCTS, | ||
|
@@ -67,3 +68,28 @@ export const loadUsers = () => async (dispatch, getState) => { | |
|
||
dispatch({ type: LOAD_USERS, CallAPI: '/api/users' }); | ||
}; | ||
|
||
export const checkout = (checkoutProducts) => async (dispatch, getState) => { | ||
dispatch({ type: CHECKOUT + REQUEST }); | ||
try { | ||
const response = await fetch('/api/order', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(checkoutProducts), | ||
}).then(async (res) => { | ||
if (!res.ok) { | ||
const text = await res.json(); | ||
|
||
throw new Error(text); | ||
} else { | ||
return res.json(); | ||
} | ||
}); | ||
|
||
dispatch({ type: CHECKOUT + SUCCESS, response }); | ||
dispatch(replace('/success')); | ||
} catch (error) { | ||
dispatch({ type: CHECKOUT + FAILURE, error: error?.message }); | ||
dispatch(replace('/error', { error: error?.message })); | ||
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. это состояние лучше все таки хранить в redux |
||
} | ||
}; |
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 с рута лучше ставить самым первым, так мы сразу понимаем, что происходит при первом заходе на /