Skip to content

Commit

Permalink
Merge pull request #4 from svetanti/sprint-2/step-2
Browse files Browse the repository at this point in the history
Sprint 2/step 2
  • Loading branch information
svetanti committed Jan 29, 2022
2 parents 7254928 + 0d367e9 commit 343af5a
Show file tree
Hide file tree
Showing 24 changed files with 1,337 additions and 438 deletions.
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"no-unused-vars": ["error", { "vars": "all", "args": "none", "ignoreRestSiblings": false }],
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx", ".tsx", ".ts"] }],
"no-console": "off",
"no-plusplus":"off",
"no-param-reassign" :"off",
"no-underscore-dangle": [
"error",
{
Expand Down
716 changes: 565 additions & 151 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "^1.7.1",
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.0.3",
"@types/node": "^16.11.15",
"@types/react": "^17.0.37",
"@types/react-dom": "^17.0.11",
"@ya.praktikum/react-developer-burger-ui-components": "^1.13.1",
"immutability-helper": "^3.1.1",
"prop-types": "^15.8.1",
"react": "^17.0.2",
"react-dnd": "^14.0.5",
"react-dnd-html5-backend": "^14.1.0",
"react-dom": "^17.0.2",
"react-redux": "^7.2.6",
"react-scripts": "5.0.0",
"redux": "^4.1.2",
"redux-thunk": "^2.4.1",
"typescript": "^4.5.4",
"uuid": "^8.3.2",
"web-vitals": "^2.1.2"
},
"scripts": {
Expand Down Expand Up @@ -44,6 +48,10 @@
]
},
"devDependencies": {
"@types/jest": "^27.0.3",
"@types/node": "^16.11.15",
"@types/react": "^17.0.37",
"@types/react-dom": "^17.0.11",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"eslint": "^8.6.0",
Expand Down
168 changes: 168 additions & 0 deletions src/components/app/app.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import React, {
useCallback, useEffect, useState,
} from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import update from 'immutability-helper';
import AppHeader from '../app-header/app-header';
import BurgerIngredients from '../burger-ingredients/burger-ingredients';
import appStyles from './app.module.css';
import Main from '../main/main';
import Modal from '../modal/modal';
import OrderDetails from '../order-details/order-details';
import IngredientDetails from '../ingredient-details/ingredient-details';
import useWindowSize from '../../hooks/useWindowSize';
import {
ADD_INGREDIENT,
ADD_INGREDIENT_DATA,
DELETE_INGREDIENT,
DELETE_INGREDIENT_DATA,
MOVE_CONSTRUCTOR_ELEMENT,
getIngredients,
TOGGLE_MODAL,
getOrder,
} from '../../services/actions/actions';
import BurgerConstructor from '../burger-constructor/burger-constructor';

function App() {
const dispatch = useDispatch();
const [isMenuOpen] = useState(false);
const [currentModal, setCurrentModal] = useState('');
const { width } = useWindowSize();
const isTablet = width <= 1024;
const [isConstructorOpened, setIsConstructorOpened] = useState(true);

const { currentBurger } = useSelector((store) => store.currentBurgerReducer);
const { isModalOpened } = useSelector((store) => store.modalReducer);
const { order } = useSelector((store) => store.orderReducer);

const currentBurgerIngredients = [...currentBurger].filter((item) => item.type !== 'bun');

let modalContent;
let headerText;
switch (currentModal) {
case 'ingredientDetails': {
modalContent = <IngredientDetails />;
headerText = 'Детали ингредиента';
break;
}
case 'orderDetails': {
modalContent = <OrderDetails orderNumber={order.number} />;
headerText = isTablet ? 'Заказ оформлен' : '';
break;
}
default: {
modalContent = '';
}
}

const openIngredientDetails = (item) => {
dispatch({ type: ADD_INGREDIENT_DATA, item });
dispatch({ type: TOGGLE_MODAL });
setCurrentModal('ingredientDetails');
};

const openOrderDetails = () => {
setCurrentModal('orderDetails');
dispatch({ type: TOGGLE_MODAL });
};

const makeOrder = (orderData) => {
dispatch(getOrder(orderData));
openOrderDetails();
};

const closeModal = () => {
dispatch({ type: DELETE_INGREDIENT_DATA });
dispatch({ type: TOGGLE_MODAL });
};

const openConstructor = () => {
setIsConstructorOpened(true);
};

const closeConstructor = () => {
setIsConstructorOpened(false);
};

const handleDrop = (item) => {
if (item.type === 'bun') {
const bun = currentBurger.find((el) => el.type === 'bun');
const index = currentBurger.indexOf(bun);
if (index !== -1) {
dispatch({ type: DELETE_INGREDIENT, index });
}
}
dispatch({ type: ADD_INGREDIENT, item });
};

const handleMove = useCallback((dragIndex, hoverIndex) => {
const bun = [...currentBurger].find((item) => item.type === 'bun');
const dragElement = currentBurgerIngredients[dragIndex];
const payload = bun
? [bun, ...update(currentBurgerIngredients, {
$splice: [
[dragIndex, 1],
[hoverIndex, 0, dragElement],
],
})]
: update(currentBurgerIngredients, {
$splice: [
[dragIndex, 1],
[hoverIndex, 0, dragElement],
],
});
dispatch({ type: MOVE_CONSTRUCTOR_ELEMENT, payload });
}, [currentBurgerIngredients]);

const handleDeleteIngredient = (item) => {
const index = currentBurger.indexOf(item);
dispatch({ type: DELETE_INGREDIENT, index });
};

useEffect(() => {
dispatch(getIngredients());
}, [dispatch]);

useEffect(() => {
setIsConstructorOpened(!isTablet);
}, [isTablet]);

return (
<div className={appStyles.app}>
<DndProvider backend={HTML5Backend}>
<AppHeader isMenuOpen={isMenuOpen} isTablet={isTablet} />
<Main>
<BurgerIngredients
onModalOpen={openIngredientDetails}
onOpenConstructor={openConstructor}
onIngredientAdd={handleDrop}
isTablet={isTablet}
/>
{ isConstructorOpened && (
<BurgerConstructor
onOrder={makeOrder}
isTablet={isTablet}
onCloseConstructor={closeConstructor}
onDropHandler={handleDrop}
onMove={handleMove}
onDelete={handleDeleteIngredient}
/>
)}
</Main>
{isModalOpened && (
<Modal
onClose={closeModal}
header={headerText}
>
{modalContent}
</Modal>
)}
</DndProvider>
</div>

);
}

export default App;
114 changes: 0 additions & 114 deletions src/components/app/app.tsx

This file was deleted.

Loading

0 comments on commit 343af5a

Please sign in to comment.