-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from massimoteplovsky/sprint-2/step-1
sprint-2/step-1
- Loading branch information
Showing
15 changed files
with
381 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 127 additions & 54 deletions
181
src/components/burger-constructor/burger-constructor.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,153 @@ | ||
import { useMemo } from 'react'; | ||
import { useContext, useEffect } from 'react'; | ||
import { | ||
Button, | ||
ConstructorElement, | ||
CurrencyIcon, | ||
DragIcon, | ||
BurgerIcon, | ||
} from '@ya.praktikum/react-developer-burger-ui-components'; | ||
import pt from 'prop-types'; | ||
import s from './burger-constructor.module.css'; | ||
import { PropValidator } from '../../utils/prop-validator'; | ||
import { AppContext } from '../../services/app-context'; | ||
import { ENDPOINT, ApiRoute, ModalType } from '../../utils/constants'; | ||
import { | ||
SET_ORDER_NUMBER, | ||
SEND_ORDER_ERROR, | ||
DELETE_INGREDIENT, | ||
SET_TOTAL_PRICE, | ||
} from '../../services/modules/app'; | ||
|
||
const BurgerConstructor = ({ ingredients, handleSetModalData }) => { | ||
const slicedIngredients = useMemo(() => ingredients.slice(0, 10), [ | ||
ingredients, | ||
]); | ||
const totalPrice = slicedIngredients.reduce( | ||
(total, ingredeint) => (total += ingredeint.price), | ||
0 | ||
); | ||
// Components | ||
import NoIngredient from '../no-ingredient/no-ingredient'; | ||
|
||
const BurgerConstructor = () => { | ||
const { | ||
burgerData: { bunIngredient, mainIngredients }, | ||
totalPrice, | ||
dispatch, | ||
} = useContext(AppContext); | ||
const isIngredientsExist = bunIngredient || mainIngredients.length > 0; | ||
|
||
const handleSendOrder = async () => { | ||
const ingredients = [bunIngredient, ...mainIngredients].map( | ||
({ _id }) => _id | ||
); | ||
|
||
const handleSendOrder = () => { | ||
handleSetModalData({ orderNumber: 345679 }); | ||
try { | ||
const response = await fetch(`${ENDPOINT}/${ApiRoute.ORDERS}`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ ingredients }), | ||
}); | ||
|
||
if (response?.ok) { | ||
const { order } = await response.json(); | ||
return dispatch({ | ||
type: SET_ORDER_NUMBER, | ||
payload: { orderNumber: order.number, modalType: ModalType.ORDER }, | ||
}); | ||
} | ||
|
||
throw new Error(); | ||
} catch (err) { | ||
dispatch({ | ||
type: SEND_ORDER_ERROR, | ||
}); | ||
} | ||
}; | ||
|
||
const handleDeleteIngredient = (ingredientIndex) => { | ||
const newMainIngredients = mainIngredients.filter( | ||
(_, index) => index !== ingredientIndex | ||
); | ||
dispatch({ type: DELETE_INGREDIENT, payload: newMainIngredients }); | ||
}; | ||
|
||
useEffect(() => { | ||
const bunPrice = bunIngredient?.price * 2 || 0; | ||
|
||
const totalPrice = | ||
mainIngredients.reduce( | ||
(totalPrice, { price }) => (totalPrice += price), | ||
0 | ||
) + bunPrice; | ||
|
||
dispatch({ | ||
type: SET_TOTAL_PRICE, | ||
payload: totalPrice, | ||
}); | ||
}, [bunIngredient, mainIngredients, dispatch]); | ||
|
||
return ( | ||
<section className={`${s.burgerConstructorSection} mr-5 ml-5 pt-25`}> | ||
<div className={`${s.ingredientsContainer} mb-10`}> | ||
<ConstructorElement | ||
type="top" | ||
isLocked={true} | ||
text={slicedIngredients[0].name} | ||
price={slicedIngredients[0].price} | ||
thumbnail={slicedIngredients[0].image} | ||
/> | ||
<ul className={`${s.ingredientsList} pr-4 pl-4`}> | ||
{useMemo( | ||
() => | ||
slicedIngredients | ||
.slice(1, 9) | ||
.map(({ name, price, image }, index) => ( | ||
{!isIngredientsExist ? ( | ||
<div className={s.noIngredientsBlock}> | ||
<BurgerIcon type="primary" /> | ||
<h2 className="text text_type_main-medium"> | ||
Выберите ингредиенты для вашего бургера | ||
</h2> | ||
</div> | ||
) : ( | ||
<> | ||
<div className={`${s.ingredientsContainer} mb-10`}> | ||
{bunIngredient ? ( | ||
<ConstructorElement | ||
type="top" | ||
isLocked={true} | ||
text={`${bunIngredient.name} (верх)`} | ||
price={bunIngredient.price} | ||
thumbnail={bunIngredient.image_mobile} | ||
/> | ||
) : ( | ||
<NoIngredient>Выберите булку (верх)</NoIngredient> | ||
)} | ||
<ul className={`${s.ingredientsList}`}> | ||
{mainIngredients.length ? ( | ||
mainIngredients.map(({ name, price, image_mobile }, index) => ( | ||
<li key={index} className={s.ingredientItem}> | ||
<DragIcon type="primary" /> | ||
<ConstructorElement | ||
isLocked={false} | ||
text={name} | ||
price={price} | ||
thumbnail={image} | ||
thumbnail={image_mobile} | ||
handleClose={() => handleDeleteIngredient(index)} | ||
/> | ||
</li> | ||
)), | ||
[slicedIngredients] | ||
)} | ||
</ul> | ||
<ConstructorElement | ||
type="bottom" | ||
isLocked={true} | ||
text={slicedIngredients[slicedIngredients.length - 1].name} | ||
price={slicedIngredients[slicedIngredients.length - 1].price} | ||
thumbnail={slicedIngredients[slicedIngredients.length - 1].image} | ||
/> | ||
</div> | ||
<div className={`${s.submitSection} mr-6`}> | ||
<p className={`${s.totalPrice} text text_type_digits-medium mr-10`}> | ||
{totalPrice} | ||
<CurrencyIcon clasName="text_type_main-large" type="primary" /> | ||
</p> | ||
<Button type="primary" size="large" onClick={handleSendOrder}> | ||
Оформить заказ | ||
</Button> | ||
</div> | ||
)) | ||
) : ( | ||
<NoIngredient>Выберите основные ингредиенты</NoIngredient> | ||
)} | ||
</ul> | ||
{bunIngredient ? ( | ||
<ConstructorElement | ||
type="bottom" | ||
isLocked={true} | ||
text={`${bunIngredient.name} (низ)`} | ||
price={bunIngredient.price} | ||
thumbnail={bunIngredient.image_mobile} | ||
/> | ||
) : ( | ||
<NoIngredient>Выберите булку (низ)</NoIngredient> | ||
)} | ||
</div> | ||
<div className={s.submitSection}> | ||
<p className={`${s.totalPrice} text text_type_digits-medium mr-10`}> | ||
{totalPrice} | ||
<CurrencyIcon clasName="text_type_main-large" type="primary" /> | ||
</p> | ||
<Button | ||
type="primary" | ||
size="large" | ||
disabled={!bunIngredient} | ||
onClick={handleSendOrder} | ||
> | ||
Оформить заказ | ||
</Button> | ||
</div> | ||
</> | ||
)} | ||
</section> | ||
); | ||
}; | ||
|
||
BurgerConstructor.propTypes = { | ||
ingredients: pt.arrayOf(PropValidator.INGREDIENT).isRequired, | ||
handleSetModalData: pt.func.isRequired, | ||
}; | ||
|
||
export default BurgerConstructor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.