Browse Source

done without delete from cart

screamoviolence 2 years ago
commit
3af1b28f63
51 changed files with 17993 additions and 0 deletions
  1. 23 0
      .gitignore
  2. 5 0
      .vscode/settings.json
  3. 70 0
      README.md
  4. 16598 0
      package-lock.json
  5. 43 0
      package.json
  6. 20 0
      public/index.html
  7. 28 0
      src/App.js
  8. 22 0
      src/App.module.css
  9. BIN
      src/assets/images/1.webp
  10. BIN
      src/assets/images/github_logo.png
  11. 12 0
      src/common/FormControls/FormControls.module.css
  12. 27 0
      src/common/FormControls/FormsControls.js
  13. 14 0
      src/common/FormControls/validators/index.js
  14. 20 0
      src/components/Aside/Aside.jsx
  15. 19 0
      src/components/Aside/Aside.module.css
  16. 19 0
      src/components/Aside/AsideContainer.jsx
  17. 13 0
      src/components/Aside/CategoriesItem/CategoriesItem.jsx
  18. 0 0
      src/components/Aside/CategoriesItem/CategoriesItem.module.css
  19. 21 0
      src/components/Header/Cart/Cart.jsx
  20. 13 0
      src/components/Header/Cart/Cart.module.css
  21. 23 0
      src/components/Header/Header.jsx
  22. 25 0
      src/components/Header/Header.module.css
  23. 30 0
      src/components/Header/Login/Login.jsx
  24. 23 0
      src/components/Header/Login/Login.module.css
  25. 18 0
      src/components/Header/Register/Register.jsx
  26. 4 0
      src/components/Header/Register/Register.module.css
  27. 58 0
      src/components/Main/MainCart/MainCart.jsx
  28. 61 0
      src/components/Main/MainCart/MainCart.module.css
  29. 33 0
      src/components/Main/MainCart/MainCartContainer.jsx
  30. 20 0
      src/components/Main/MainGoods/CatByIdItem/CatByIdItem.jsx
  31. 39 0
      src/components/Main/MainGoods/CatByIdItem/CatByIdItem.module.css
  32. 27 0
      src/components/Main/MainGoods/MainGoods.jsx
  33. 13 0
      src/components/Main/MainGoods/MainGoods.module.css
  34. 28 0
      src/components/Main/MainGoods/MainGoodsContainer.jsx
  35. 58 0
      src/components/Main/MainLogin/MainLogin.jsx
  36. 29 0
      src/components/Main/MainLogin/MainLogin.module.css
  37. 20 0
      src/components/Main/MainLogin/MainLoginContainer.jsx
  38. 36 0
      src/components/Main/MainOneGood/MainOneGood.jsx
  39. 47 0
      src/components/Main/MainOneGood/MainOneGood.module.css
  40. 23 0
      src/components/Main/MainOneGood/MainOneGoodContainer.jsx
  41. 42 0
      src/components/Main/MainRegister/MainRegister.jsx
  42. 30 0
      src/components/Main/MainRegister/MainRegister.module.css
  43. 20 0
      src/components/Main/MainRegister/MainRegisterContainer.jsx
  44. 11 0
      src/index.css
  45. 24 0
      src/index.js
  46. 68 0
      src/redux/authReducer.js
  47. 64 0
      src/redux/cartReducer.js
  48. 113 0
      src/redux/promiseReducer.js
  49. 21 0
      src/redux/reduxStore.js
  50. 13 0
      src/reportWebVitals.js
  51. 5 0
      src/setupTests.js

+ 23 - 0
.gitignore

@@ -0,0 +1,23 @@
+# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
+
+# dependencies
+/node_modules
+/.pnp
+.pnp.js
+
+# testing
+/coverage
+
+# production
+/build
+
+# misc
+.DS_Store
+.env.local
+.env.development.local
+.env.test.local
+.env.production.local
+
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*

+ 5 - 0
.vscode/settings.json

@@ -0,0 +1,5 @@
+{
+    "cSpell.words": [
+        "Upsert"
+    ]
+}

+ 70 - 0
README.md

@@ -0,0 +1,70 @@
+# Getting Started with Create React App
+
+This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
+
+## Available Scripts
+
+In the project directory, you can run:
+
+### `npm start`
+
+Runs the app in the development mode.\
+Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
+
+The page will reload if you make edits.\
+You will also see any lint errors in the console.
+
+### `npm test`
+
+Launches the test runner in the interactive watch mode.\
+See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
+
+### `npm run build`
+
+Builds the app for production to the `build` folder.\
+It correctly bundles React in production mode and optimizes the build for the best performance.
+
+The build is minified and the filenames include the hashes.\
+Your app is ready to be deployed!
+
+See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
+
+### `npm run eject`
+
+**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
+
+If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
+
+Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
+
+You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
+
+## Learn More
+
+You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
+
+To learn React, check out the [React documentation](https://reactjs.org/).
+
+### Code Splitting
+
+This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
+
+### Analyzing the Bundle Size
+
+This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
+
+### Making a Progressive Web App
+
+This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
+
+### Advanced Configuration
+
+This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
+
+### Deployment
+
+This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
+
+### `npm run build` fails to minify
+
+This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)

File diff suppressed because it is too large
+ 16598 - 0
package-lock.json


+ 43 - 0
package.json

@@ -0,0 +1,43 @@
+{
+  "name": "react-rework",
+  "version": "0.1.0",
+  "private": true,
+  "dependencies": {
+    "@testing-library/jest-dom": "^5.14.1",
+    "@testing-library/react": "^11.2.7",
+    "@testing-library/user-event": "^12.8.3",
+    "react": "^17.0.2",
+    "react-dom": "^17.0.2",
+    "react-redux": "^7.2.5",
+    "react-router-dom": "^5.3.0",
+    "react-scripts": "4.0.3",
+    "redux": "^4.1.1",
+    "redux-form": "^8.3.7",
+    "redux-thunk": "^2.3.0",
+    "web-vitals": "^1.1.2"
+  },
+  "scripts": {
+    "start": "react-scripts start",
+    "build": "react-scripts build",
+    "test": "react-scripts test",
+    "eject": "react-scripts eject"
+  },
+  "eslintConfig": {
+    "extends": [
+      "react-app",
+      "react-app/jest"
+    ]
+  },
+  "browserslist": {
+    "production": [
+      ">0.2%",
+      "not dead",
+      "not op_mini all"
+    ],
+    "development": [
+      "last 1 chrome version",
+      "last 1 firefox version",
+      "last 1 safari version"
+    ]
+  }
+}

+ 20 - 0
public/index.html

@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8" />
+    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
+    <meta name="viewport" content="width=device-width, initial-scale=1" />
+    <meta name="theme-color" content="#000000" />
+    <meta
+      name="description"
+      content="Web site created using create-react-app"
+    />
+
+    <title>Shop react</title>
+  </head>
+  <body>
+    <noscript>You need to enable JavaScript to run this app.</noscript>
+    <div id="root"></div>
+   
+  </body>
+</html>

+ 28 - 0
src/App.js

@@ -0,0 +1,28 @@
+import classes from "./App.module.css";
+import Header from "../src/components/Header/Header";
+import { Route } from "react-router-dom";
+import AsideContainer from "./components/Aside/AsideContainer";
+import MainGoodsContainer from "./components/Main/MainGoods/MainGoodsContainer";
+import MainOneGoodContainer from "./components/Main/MainOneGood/MainOneGoodContainer";
+import MainLoginContainer from "./components/Main/MainLogin/MainLoginContainer";
+import MainRegisterContainer from "./components/Main/MainRegister/MainRegisterContainer";
+import MainCartContainer from "./components/Main/MainCart/MainCartContainer";
+
+function App(props) {
+  return (
+    <div className={classes.pageWrapper}>
+      <Header></Header>
+      <div className={classes.mainContainer}>
+        <AsideContainer />
+        <Route path="/categories/:id" render={() => <MainGoodsContainer />} />
+        <Route path="/good/:id" render={() => <MainOneGoodContainer />} />
+        <Route path="/login" render={() => <MainLoginContainer />} />
+        <Route path="/register" render={() => <MainRegisterContainer />} />
+        <Route path="/cart" render={() => <MainCartContainer />} />
+      </div>
+      <footer className={classes.footer}>some footer content</footer>
+    </div>
+  );
+}
+
+export default App;

File diff suppressed because it is too large
+ 22 - 0
src/App.module.css


BIN
src/assets/images/1.webp


BIN
src/assets/images/github_logo.png


+ 12 - 0
src/common/FormControls/FormControls.module.css

@@ -0,0 +1,12 @@
+.form-control {
+
+}
+
+.formControl.error input,
+.formControl.error textarea{
+    border: 1px red solid
+}
+
+.formControl.error span{
+    color:red
+}

+ 27 - 0
src/common/FormControls/FormsControls.js

@@ -0,0 +1,27 @@
+import React from "react";
+import classes from "./FormControls.module.css";
+
+export const FormControl = ({ input, meta, child, ...props }) => {
+  const hasError = meta.touched && meta.error;
+
+  return (
+    <div
+      className={classes.formControl + " " + (hasError ? classes.error : "")}
+    >
+      <div>{props.children}</div>
+      {hasError && <span>{meta.error}</span>}
+    </div>
+  );
+};
+
+
+export const Input = (props) => {
+    const { input, meta, child, ...restProps } = props
+  return (
+    <div>
+      <FormControl {...props}>
+        <input {...input} {...restProps} />
+      </FormControl>
+    </div>
+  );
+};

+ 14 - 0
src/common/FormControls/validators/index.js

@@ -0,0 +1,14 @@
+export const required = value => {
+    if (value) return undefined
+    return 'Field is required '
+}
+
+export const maxLengthCreator = (maxLength) => value => {
+    if (value && value.length > maxLength) return `Max length is ${maxLength}`
+    return undefined
+}
+
+// export const maxLengthCreator = (maxLength) => value => {
+//     if (value && value.length > maxLength) return `Max length is ${maxLength}`
+//     return undefined
+// }

+ 20 - 0
src/components/Aside/Aside.jsx

@@ -0,0 +1,20 @@
+import React from "react";
+import classes from "./Aside.module.css";
+import CategoriesItem from "./CategoriesItem/CategoriesItem";
+import { useEffect } from "react";
+
+const Aside = (state) => {
+  useEffect(() => state.drawMainMenu(),[])
+  
+
+  if (state.cats) {
+    let catsq = state.cats.map((el) => (
+      <CategoriesItem name={el.name} _id={el._id} />
+    ));
+
+    return <aside className={classes.aside}>{catsq}</aside>;
+  } else {
+    return <div>Loading...</div>;
+  }
+};
+export default Aside;

+ 19 - 0
src/components/Aside/Aside.module.css

@@ -0,0 +1,19 @@
+.aside {
+  min-width: 20%;
+  max-width: 20%;
+
+  font-size: 20px;
+  padding: 0 20px 0px 0;
+}
+
+.aside > a {
+  display: block;
+  color: black;
+  text-decoration: none;
+}
+
+.aside > a:hover {
+  color: tomato;
+  font-size: 24px;
+}
+

+ 19 - 0
src/components/Aside/AsideContainer.jsx

@@ -0,0 +1,19 @@
+import Aside from "./Aside";
+import { connect } from "react-redux";
+import { actionRootCategories } from "../../redux/promiseReducer";
+
+let mapStateToProps = (state) => {
+  return {
+    cats: state.promiseReducer.rootCategories?.payload?.data?.CategoryFind,
+  };
+};
+
+let mapDispatchToProps = (dispatch) => {
+  return {
+    drawMainMenu:  () => {
+      dispatch(actionRootCategories());
+    },
+  };
+};
+
+export default connect(mapStateToProps, mapDispatchToProps)(Aside);

+ 13 - 0
src/components/Aside/CategoriesItem/CategoriesItem.jsx

@@ -0,0 +1,13 @@
+import React from "react";
+import { NavLink } from "react-router-dom";
+
+
+const CategoriesItem = (props) => {
+  let path = `/categories/${props._id}`;
+
+  return (
+      <NavLink to={path}>{props.name}</NavLink>
+  );
+};
+
+export default CategoriesItem;

+ 0 - 0
src/components/Aside/CategoriesItem/CategoriesItem.module.css


+ 21 - 0
src/components/Header/Cart/Cart.jsx

@@ -0,0 +1,21 @@
+import React from "react";
+import { NavLink } from "react-router-dom";
+import classes from "./Cart.module.css";
+import { connect } from 'react-redux';
+
+const Cart = (props) => {
+  return (
+    <div className={classes.cartHolder}>
+      { props.loggedIn &&
+(      <NavLink to="/cart">
+        <img
+          src="https://i.pinimg.com/originals/15/4f/df/154fdf2f2759676a96e9aed653082276.png"
+          alt=""
+          className={classes.cartImg}
+        ></img>
+      </NavLink>)}
+    </div>
+  );
+};
+
+export default connect((store) => ({loggedIn: store.authReducer.token }), {})(Cart);

+ 13 - 0
src/components/Header/Cart/Cart.module.css

@@ -0,0 +1,13 @@
+.cartImg {
+  
+  height: 50px;
+  width: 50px;
+}
+
+
+
+.cartHolder {
+  display: flex;
+  flex-direction: column;
+  justify-content: center;
+}

+ 23 - 0
src/components/Header/Header.jsx

@@ -0,0 +1,23 @@
+import React from "react";
+import classes from "./Header.module.css";
+import Login from "../Header/Login/Login";
+import Register from "../Header/Register/Register";
+import Cart from "../Header/Cart/Cart";
+import logo from "../../assets/images/github_logo.png"
+
+const Header = () => {
+  return (
+    <header className={classes.header}>
+      <div><img className={classes.logo} src={logo} /></div>
+      <nav className={classes.nav}>
+        <div>
+          <Login></Login>
+          <Register></Register>
+        </div>
+        <Cart></Cart>
+      </nav>
+    </header>
+  );
+};
+
+export default Header;

File diff suppressed because it is too large
+ 25 - 0
src/components/Header/Header.module.css


+ 30 - 0
src/components/Header/Login/Login.jsx

@@ -0,0 +1,30 @@
+import React from "react";
+import { connect } from "react-redux";
+import { NavLink } from "react-router-dom";
+import { actionLogout } from "../../../redux/authReducer";
+import classes from "./Login.module.css";
+
+
+const Login = (props) => {
+  
+  return (
+    
+    <div className={classes.loginContainer}>
+      { props.loggedIn ? (
+        
+        <div>
+          {props.login} - <button className={classes.goodButton} onClick={props.actionLogout}>Logout</button>
+        </div>
+      ) : (
+        <NavLink className={classes.loginLink} to={"/login"}>Login</NavLink>
+      )}
+    </div>
+  );
+};
+
+let mapStateToProps = (store) => ({
+  loggedIn: store.authReducer.token,
+  login: store.authReducer.payload?.sub?.login,
+});
+
+export default connect(mapStateToProps, { actionLogout })(Login);

+ 23 - 0
src/components/Header/Login/Login.module.css

@@ -0,0 +1,23 @@
+.loginLink {
+    text-decoration: none;
+    color: black;
+}
+
+.loginContainer {
+    display: flex;
+    align-items: center;
+
+}
+
+.goodButton{
+    height: 50px;
+    font-weight: bold;
+    margin: 10px 0 0 0;
+    border-radius: 30px;
+    background-color: white;
+    color: black;
+  }
+  
+  .goodButton:hover {
+    background-color: tomato;
+  }

+ 18 - 0
src/components/Header/Register/Register.jsx

@@ -0,0 +1,18 @@
+import React from "react";
+import { connect } from "react-redux";
+import { NavLink } from "react-router-dom";
+import classes from "./Register.module.css";
+
+const Register = (props) => {
+  return (
+    <div>
+      {props.loggedIn ? null : (
+        <NavLink className={classes.loginLink} to="/register">
+          Register
+        </NavLink>
+      )}
+    </div>
+  );
+};
+
+export default connect((store) => ({loggedIn: store.authReducer.token }), {})(Register);

+ 4 - 0
src/components/Header/Register/Register.module.css

@@ -0,0 +1,4 @@
+.loginLink {
+    text-decoration: none;
+    color: black;
+}

+ 58 - 0
src/components/Main/MainCart/MainCart.jsx

@@ -0,0 +1,58 @@
+import React, { useEffect, useState } from "react";
+import classes from "./MainCart.module.css";
+import { Redirect } from 'react-router';
+
+const MainCart = (state) => {
+
+  if (!state.loggedIn) {
+    return <Redirect to={"/"} />
+  }
+
+const addOnClick = () => {
+  if (state.cart) {
+    state.actionCartAdd(state.good)
+  }
+}
+
+const deleteOnClick = () => {
+  if (state.cart) {
+    state.actionCartDelete(state.good)
+  }
+}
+
+
+  if (state.cart) {
+    return (
+      <div>
+        <h1 className={classes.catHeader}>Cart</h1>
+        <button onClick={() => state.actionCartClear()} className={classes.changeButton}>clear</button>
+        <div className={classes.cardWrapper}>
+          {Object.keys(state.cart).map(key => <div className={classes.divGood}>
+            <img
+              src={`http://shop-roles.asmer.fs.a-level.com.ua/${state.cart[key].good.images[0].url
+                }`}
+            />
+            <div className={classes.goodLink}>
+              {state.cart[key].good.name}
+            </div>
+            <div className={classes.goodPrice}>
+              Цена: {state.cart[key].good.price} грн
+            </div>
+            <div>
+             <div><button onClick={() => addOnClick()} className={classes.changeButton}>+</button><button onClick={() => {}} className={classes.changeButton}>-</button></div>
+            </div>
+            <div className={classes.goodPrice}>
+              Колво: {state.cart[key].count} шт. {state.cart[key].count * state.cart[key].good.price} грн
+
+            </div>
+          </div>
+          )}
+        </div>
+      </div>
+    )
+  } else {
+    return <div className={classes.loader}>loading...</div>
+  }
+}
+
+export default MainCart;

+ 61 - 0
src/components/Main/MainCart/MainCart.module.css

@@ -0,0 +1,61 @@
+
+.cardWrapper {
+    display: flex;
+    flex-wrap: wrap;
+    width: 100%;
+  }
+  
+  .catHeader {
+    width: 100%;
+  }
+  
+  .loader {
+    margin: auto auto;
+  }
+
+.divGood {
+    margin: 0 10px 10px 0;
+    padding: 20px;
+    width: 400px;
+    border: 2px solid gray;
+    display: flex;
+    flex-direction: column;
+    align-content: stretch;
+    text-align: center;
+  }
+  
+  .divGood:hover {
+    border-color: tomato;
+  }
+  
+  .goodLink {
+    flex: 1 1 auto;
+    text-align: center;
+    font-size: 22px;
+  }
+  
+  .goodPrice {
+    font-size: 20px;
+    font-weight: bold;
+    text-align: center;
+  }
+  
+  .goodButton{
+    height: 50px;
+    font-weight: bold;
+    margin: 10px 0 0 0;
+    border-radius: 30px;
+    background-color: white;
+    color: black;
+  }
+
+  .changeButton {
+    display: inline;
+    height: 50px;
+    width: 50px;
+    border-radius: 30px;
+  }
+  .changeButton:hover,
+  .goodButton:hover {
+    background-color: tomato;
+  }

+ 33 - 0
src/components/Main/MainCart/MainCartContainer.jsx

@@ -0,0 +1,33 @@
+import MainCart from "./MainCart";
+import { connect } from "react-redux";
+import { actionCartAdd, actionCartClear, actionCartDelete, actionCreateOrder } from "../../../redux/cartReducer";
+
+let mapStateToProps = (state) => {
+  return {
+
+    cart: state.cartReducer,
+    loggedIn: state.authReducer.token,
+    good: state.promiseReducer.goodById?.payload?.data?.GoodFindOne,
+
+  };
+};
+
+
+let mapDispatchToProps = (dispatch) => {
+  return {
+    actionCreateOrder: (_id,count) => {
+      dispatch(actionCreateOrder(_id,count));
+    },
+    actionCartAdd: (good, count = 1) => {
+      dispatch(actionCartAdd(good,count));
+    },
+    actionCartDelete: (good, count = 1) => {
+      dispatch(actionCartDelete(good, count));
+    },
+    actionCartClear: (good, count = 1) => {
+      dispatch(actionCartClear(good, count));
+    },
+  };
+};
+
+export default connect(mapStateToProps, mapDispatchToProps)(MainCart);

+ 20 - 0
src/components/Main/MainGoods/CatByIdItem/CatByIdItem.jsx

@@ -0,0 +1,20 @@
+import React from "react";
+import { NavLink } from "react-router-dom";
+import classes from "./CatByIdItem.module.css";
+
+
+const CatByIdItem = (props) => {
+  let path = `/good/${props.cardElements._id}`;
+  return (
+    <div className={classes.divGood}>
+      <img src={`http://shop-roles.asmer.fs.a-level.com.ua/${props.cardElements.images[0].url}`} />
+      <NavLink className={classes.goodLink} to={path}>{props.cardElements.name}</NavLink>
+      <div className={classes.goodPrice}>Цена: {props.cardElements.price} грн</div>
+      <NavLink className={classes.goodLink} to={path}><button className={classes.goodButton}> Купить</button></NavLink>
+    </div>
+  );
+}; 
+
+export default CatByIdItem;
+
+

+ 39 - 0
src/components/Main/MainGoods/CatByIdItem/CatByIdItem.module.css

@@ -0,0 +1,39 @@
+.divGood {
+  margin: 0 10px 10px 0;
+  padding: 20px;
+  width: 400px;
+  border: 2px solid gray;
+  display: flex;
+  flex-direction: column;
+  align-content: stretch;
+}
+
+.divGood:hover {
+  border-color: tomato;
+}
+
+.goodLink {
+  flex: 1 1 auto;
+  text-align: center;
+  font-size: 22px;
+}
+
+.goodPrice {
+  font-size: 20px;
+  font-weight: bold;
+  text-align: center;
+}
+
+.goodButton{
+  width: 300px;
+  height: 50px;
+  font-weight: bold;
+  margin: 10px 0 0 0;
+  border-radius: 30px;
+  background-color: white;
+  color: black;
+}
+
+.goodButton:hover {
+  background-color: tomato;
+}

+ 27 - 0
src/components/Main/MainGoods/MainGoods.jsx

@@ -0,0 +1,27 @@
+import React from "react";
+import { useEffect } from "react";
+import { useParams } from "react-router";
+import CatByIdItem from "./CatByIdItem/CatByIdItem";
+import classes from "./MainGoods.module.css";
+
+const MainGoods = (state) => {
+  
+  let { id } = useParams();
+  useEffect(() => state.drawCatById(id), [id]);
+  
+  if (state.catByIdGood?.goods) {
+    let catByIdq = state.catByIdGood.goods.map((el) => (
+      <CatByIdItem actionCartAdd={state.actionCartAdd} good={state.good} cardElements={el} />
+    ));
+
+    return (
+      <div>
+        <h1 className={classes.catHeader}>{state.catByIdGood.name}</h1>
+        <div className={classes.cardWrapper}>{catByIdq}</div>
+      </div>
+    );
+  } else {
+    return <div className={classes.loader}>loading...</div>;
+  }
+};
+export default MainGoods;

+ 13 - 0
src/components/Main/MainGoods/MainGoods.module.css

@@ -0,0 +1,13 @@
+.cardWrapper {
+  display: flex;
+  flex-wrap: wrap;
+  width: 100%;
+}
+
+.catHeader {
+  width: 100%;
+}
+
+.loader {
+  margin: auto auto;
+}

+ 28 - 0
src/components/Main/MainGoods/MainGoodsContainer.jsx

@@ -0,0 +1,28 @@
+import MainGoods from "./MainGoods";
+import { connect } from "react-redux";
+import { actionCategoryById } from "../../../redux/promiseReducer";
+import { actionCartAdd } from "../../../redux/cartReducer";
+
+
+let mapStateToProps = (state) => {
+  return {
+    catByIdGood: state.promiseReducer.catById?.payload?.data?.CategoryFindOne,
+    urlId: document.location.pathname.split("/")[2],
+    good: state.promiseReducer.goodById?.payload?.data?.GoodFindOne,
+
+  };
+};
+
+let mapDispatchToProps = (dispatch) => {
+  return {
+    drawCatById: (_id) => {
+      dispatch(actionCategoryById(_id));
+
+    },
+    actionCartAdd: (good) => {
+      dispatch(actionCartAdd(good));
+    },
+  };
+};
+
+export default connect(mapStateToProps, mapDispatchToProps)(MainGoods);

+ 58 - 0
src/components/Main/MainLogin/MainLogin.jsx

@@ -0,0 +1,58 @@
+import React from "react";
+import classes from "./MainLogin.module.css";
+import { Field, reduxForm } from "redux-form";
+import { Input } from "../../../common/FormControls/FormsControls";
+import { required } from "../../../common/FormControls/validators";
+import { Redirect } from "react-router";
+
+
+
+
+const MainLogin = (props) => {
+  let onSubmit = (formData) => {
+    props.auth(formData.login, formData.password);
+  };
+
+  if (props.loggedIn) {
+    return <Redirect to={"/"} />
+  }
+
+  return (
+    <div className={classes.divLoginHolder}>
+      <div className={classes.divLogin}>
+        <h1>Authorization</h1>
+        <div>
+          <MainLoginReduxForm onSubmit={onSubmit} />
+        </div>
+      </div>
+    </div>
+  );
+};
+
+const MainLoginForm = (props) => {
+  return (
+    <form onSubmit={props.handleSubmit}>
+      <div>
+        <div>Login</div>
+
+        <Field placeholder={"Login"} validate={[required]} name={"login"} component={Input} />
+      </div>
+      <div>
+        <div>Password</div>
+
+        <Field placeholder={"Password"} validate={[required]} type={"password"} name={"password"} component={Input} />
+      </div>
+      <div>
+        <button className={classes.authButton}>Login</button>
+      </div>
+    </form>
+  );
+}
+
+const MainLoginReduxForm = reduxForm({
+  // a unique name for the form
+  form: "login",
+})(MainLoginForm);
+
+
+export default MainLogin;

+ 29 - 0
src/components/Main/MainLogin/MainLogin.module.css

@@ -0,0 +1,29 @@
+.divLoginHolder {
+  margin: 0 auto;
+}
+
+.divLogin {
+  margin: 0 auto;
+  padding: 20px;
+  width: 400px;
+  display: flex;
+  flex-direction: column;
+  justify-content: center;
+  text-align: center;
+}
+
+.authButton {
+    width: 200px;
+  height: 50px;
+  font-weight: bold;
+  margin: 10px 0 0 0;
+  border-radius: 30px;
+  background-color: white;
+  color: black;
+}
+
+.authButton:hover {
+  background-color: tomato;
+}
+
+

+ 20 - 0
src/components/Main/MainLogin/MainLoginContainer.jsx

@@ -0,0 +1,20 @@
+import MainLogin from "./MainLogin";
+import { connect } from "react-redux";
+import { actionFullLogin } from "../../../redux/authReducer";
+
+let mapStateToProps = (state) => {
+  return {
+    loggedIn: state.authReducer.token
+  }
+};
+
+let mapDispatchToProps = (dispatch) => {
+  return {
+    auth: (login, password) => {
+      dispatch(actionFullLogin(login, password));
+    }, 
+    
+  };
+};
+
+export default connect(mapStateToProps, mapDispatchToProps)(MainLogin);

+ 36 - 0
src/components/Main/MainOneGood/MainOneGood.jsx

@@ -0,0 +1,36 @@
+import React, { useState } from "react";
+import { useEffect } from "react";
+import { useParams } from "react-router";
+import classes from "./MainOneGood.module.css";
+
+const MainOneGood = (state) => {
+  let [clicked, setClicked] = useState(false)
+  let [counter, setCount] = useState(0)
+
+
+  let { id } = useParams();
+  useEffect(() => state.drawGoodById(id), [id]);
+
+  const addOnClick = (good) => {
+    state.actionCartAdd(good)
+    setClicked(true)
+    setCount(counter + 1) 
+  }
+
+  if (state.good) {
+    
+    return (
+      <div className={classes.divGood}>
+        <img className={classes.goodImg} src={`http://shop-roles.asmer.fs.a-level.com.ua/${state.good.images[0].url}`}/>
+        <h1 className={classes.goodLink}>{state.good.name}</h1>
+        <div className={classes.goodLink}>{state.good.description}</div>
+        <div className={classes.goodPrice}>Цена: {state.good.price} грн</div>
+        <button onClick={() => addOnClick(state.good)} className={classes.goodButton} >Купить {clicked && <div>Added to cart! {counter}</div>}</button>
+        </div> 
+    );
+  } else {
+    return <div className={classes.loader}>loading...</div>;
+  }
+  
+};
+export default MainOneGood;

+ 47 - 0
src/components/Main/MainOneGood/MainOneGood.module.css

@@ -0,0 +1,47 @@
+.divGood {
+  margin: 0 10px 10px 0;
+  padding: 20px;
+  
+  display: flex;
+  flex-direction: column;
+  align-content: stretch;
+}
+
+.divGood:hover {
+  border-color: tomato;
+}
+
+.goodLink {
+  flex: 1 1 auto;
+  text-align: center;
+  font-size: 22px;
+}
+
+.goodPrice {
+  font-size: 20px;
+  font-weight: bold;
+  text-align: center;
+}
+
+.goodButton{
+  height: 50px;
+  font-weight: bold;
+  margin: 10px 0 0 0;
+  border-radius: 30px;
+  background-color: white;
+  color: black;
+}
+
+.goodButton:hover {
+  background-color: tomato;
+}
+
+.loader {
+  margin: auto auto;
+}
+
+.goodImg {
+  width: 60%;
+  margin: 0 auto;
+  
+}

+ 23 - 0
src/components/Main/MainOneGood/MainOneGoodContainer.jsx

@@ -0,0 +1,23 @@
+import MainOneGood from "./MainOneGood";
+import { connect } from "react-redux";
+import { actionGoodById } from "../../../redux/promiseReducer";
+import { actionCartAdd } from "../../../redux/cartReducer";
+
+let mapStateToProps = (state) => {
+  return {
+    good: state.promiseReducer.goodById?.payload?.data?.GoodFindOne,
+  };
+};
+
+let mapDispatchToProps = (dispatch) => {
+  return {
+    drawGoodById: (_id) => {
+      dispatch(actionGoodById(_id));
+    },
+    actionCartAdd: (good) => {
+      dispatch(actionCartAdd(good));
+    },
+  };
+};
+
+export default connect(mapStateToProps, mapDispatchToProps)(MainOneGood);

+ 42 - 0
src/components/Main/MainRegister/MainRegister.jsx

@@ -0,0 +1,42 @@
+import React, { useState } from "react";
+import { Redirect } from "react-router";
+import classes from "./MainRegister.module.css";
+
+const MainRegister = (state) => {
+  const [login, setLogin] = useState("");
+  const [password, setPassword] = useState("");
+
+  if (state.loggedIn) {
+    return <Redirect to={"/"} />
+  }
+
+  let onAuth = (login, password) => {
+    state.auth(login, password);
+  };
+
+  return (
+    <div className={classes.divLoginHolder}>
+      <div className={classes.divLogin}>
+        <h1>Registration</h1>
+        <div>
+          <div>
+            <div>Login</div>
+            <input onChange={(event) => setLogin(event.target.value)} />
+          </div>
+          <div>
+            <div>Password</div>
+            <input onChange={(event) => setPassword(event.target.value)} />
+          </div>
+          <button
+            className={classes.authButton}
+            onClick={() => onAuth(login, password)}
+          >
+            Register
+          </button>
+        </div>
+      </div>
+    </div>
+  );
+};
+
+export default MainRegister;

+ 30 - 0
src/components/Main/MainRegister/MainRegister.module.css

@@ -0,0 +1,30 @@
+.divLoginHolder {
+    margin: 0 auto;
+  }
+  
+  .divLogin {
+    margin: 0 auto;
+    padding: 20px;
+    width: 400px;
+    display: flex;
+    flex-direction: column;
+    justify-content: center;
+    text-align: center;
+  }
+  
+  .authButton {
+      width: 200px;
+    height: 50px;
+    font-weight: bold;
+    margin: 10px 0 0 0;
+    border-radius: 30px;
+    background-color: white;
+    color: black;
+  }
+  
+  .authButton:hover {
+    background-color: tomato;
+  }
+  
+  
+  

+ 20 - 0
src/components/Main/MainRegister/MainRegisterContainer.jsx

@@ -0,0 +1,20 @@
+import MainRegister from "./MainRegister";
+import { connect } from "react-redux";
+import { actionFullRegister } from "../../../redux/authReducer";
+
+let mapStateToProps = (state) => {
+  return {
+    loggedIn: state.authReducer.token
+  }
+};
+
+let mapDispatchToProps = (dispatch) => {
+  return {
+    auth: (login, password) => {
+      dispatch(actionFullRegister(login, password));
+    },
+    
+  };
+};
+
+export default connect(mapStateToProps, mapDispatchToProps)(MainRegister);

+ 11 - 0
src/index.css

@@ -0,0 +1,11 @@
+body {
+  
+  margin: 0;
+  -webkit-font-smoothing: antialiased;
+  -moz-osx-font-smoothing: grayscale;
+}
+
+code {
+  font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
+    monospace;
+}

+ 24 - 0
src/index.js

@@ -0,0 +1,24 @@
+import React from "react";
+import ReactDOM from "react-dom";
+import "./index.css";
+import App from "./App";
+import reportWebVitals from "./reportWebVitals";
+import { BrowserRouter } from "react-router-dom";
+import { Provider } from "react-redux";
+import store from "./redux/reduxStore";
+
+ReactDOM.render(
+  <React.StrictMode>
+    <BrowserRouter>
+      <Provider store={store}>
+        <App />
+      </Provider>
+    </BrowserRouter>
+  </React.StrictMode>,
+  document.getElementById("root")
+);
+
+// If you want to start measuring performance in your app, pass a function
+// to log results (for example: reportWebVitals(console.log))
+// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
+reportWebVitals();

+ 68 - 0
src/redux/authReducer.js

@@ -0,0 +1,68 @@
+import { actionPromise, shopGQL } from "./promiseReducer";
+
+function authReducer(state, action) {
+  if (state === undefined) {
+    if (localStorage.token) {
+      action.token = localStorage.token;
+      action.type = "LOGIN";
+    } else {
+      return {};
+    }
+  }
+  if (action.type === "LOGIN") {
+    localStorage.token = action.token;
+    let { 1: tokenAverage } = action.token.split(".");
+    let decodedToken = atob(tokenAverage);
+    let parsedToken = JSON.parse(decodedToken);
+    return { token: action.token, payload: parsedToken };
+  }
+  if (action.type === "LOGOUT") {
+    console.log("ЛОГАУТ");
+    localStorage.removeItem("token");
+    return {};
+  }
+  return state;
+}
+
+const actionGetToken = (login, password) =>
+  actionPromise(
+    "getToken",
+    shopGQL(
+      `query login($login:String, $password:String){
+                                                  login(login: $login, password: $password)
+                                              }`,
+      { login, password }
+    )
+  );
+
+const actionAuthLogin = (token) => ({ type: "LOGIN", token });
+export const actionLogout = () => ({ type: "LOGOUT" })
+
+export const actionFullLogin = (login, password) => async (dispatch) => {
+  let payload = await dispatch(actionGetToken(login, password));
+  if (payload.data.login) {
+    dispatch(actionAuthLogin(payload.data.login));
+  }
+};
+
+const actionRegister = (login, password) =>
+  actionPromise(
+    "register",
+    shopGQL(
+      `mutation reg($query:UserInput){
+                  UserUpsert(user:$query){
+                      _id login
+                  }
+                  }`,
+      { query: { login, password } }
+    )
+  );
+
+export const actionFullRegister = (login, password) => async (dispatch) => {
+  let payload = await dispatch(actionRegister(login, password));
+  if (payload.data.UserUpsert != null) {
+    await dispatch(actionFullLogin(login, password));
+  }
+};
+
+export default authReducer;

+ 64 - 0
src/redux/cartReducer.js

@@ -0,0 +1,64 @@
+import { actionPromise, shopGQL } from "./promiseReducer";
+
+function cardReducer(state = {}, { type, count = 1, good }) {
+  if (type === "CART_ADD") {
+    console.log("+1");
+    const _id = good._id;
+    return {
+      ...state,
+      [_id]: { count: (state[_id]?.count || 0) + count, good },
+    };
+  }
+  if (type === "CART_CLEAR") {
+    return {};
+  }
+  if (type === "CART_DELETE") {
+    const _id = good._id;
+    debugger
+    if (state[_id].count - 1 <= 0) {
+      delete state[_id];
+      return {};
+    } else {
+      return {
+        ...state,
+        [_id]: { count: state[_id]?.count - count, good },
+      };
+    }
+  }
+  return state;
+}
+
+export const actionCreateOrder = (_id, count) =>
+  actionPromise(
+    "createdOrder",
+    shopGQL(
+      `mutation createOrder($order:OrderInput){
+        OrderUpsert(order:$order){
+          _id
+        }
+      }`,
+      {
+        order: {
+          orderGoods: [{ count: count, good: { _id: _id } }],
+        },
+      }
+    )
+  );
+
+export const actionCartAdd = (good, count = 1) => ({
+  type: "CART_ADD",
+  count,
+  good,
+});
+export const actionCartDelete = (good, count) => ({
+  type: "CART_DELETE",
+  count,
+  good,
+});
+export const actionCartClear = (good, count = 1) => ({
+  type: "CART_CLEAR",
+  count,
+  good,
+});
+
+export default cardReducer;

+ 113 - 0
src/redux/promiseReducer.js

@@ -0,0 +1,113 @@
+const promiseReducer = function (
+  state = {},
+  { type, status, payload, error, name }
+) {
+  if (type === "PROMISE") {
+    return {
+      ...state,
+      [name]: { status, payload, error },
+    };
+  }
+  return state;
+};
+
+const actionPending = (name) => ({
+  type: "PROMISE",
+  status: "PENDING",
+  name,
+});
+const actionResolved = (name, payload) => ({
+  type: "PROMISE",
+  status: "RESOLVED",
+  name,
+  payload,
+});
+const actionRejected = (name, error) => ({
+  type: "PROMISE",
+  status: "REJECTED",
+  name,
+  error,
+});
+
+export const actionPromise = (name, promise) => async (dispatch) => {
+  dispatch(actionPending(name));
+  try {
+    let payload = await promise;
+    dispatch(actionResolved(name, payload));
+    return payload;
+  } catch (error) {
+    dispatch(actionRejected(name, error));
+  }
+};
+
+const getGQL =
+  (url) =>
+  (query, variables = {}) => {
+    let headers;
+    if (localStorage.token) {
+      headers = {
+        "Content-Type": "application/json",
+        Authorization: "Bearer " + localStorage.token,
+      };
+    } else {
+      headers = {
+        "Content-Type": "application/json",
+      };
+    }
+    return fetch(url, {
+      method: "POST",
+      headers,
+      body: JSON.stringify({ query, variables }),
+    }).then((res) => res.json());
+  };
+
+export let shopGQL = getGQL("http://shop-roles.asmer.fs.a-level.com.ua/graphql");
+
+export const actionRootCategories = () =>
+  actionPromise(
+    "rootCategories",
+    shopGQL(
+      `
+                  query cats($query:String){
+                    CategoryFind(query:$query){
+                      _id name
+                    }
+                  }
+              `,
+      { query: JSON.stringify([{ parent: null }]) }
+    )
+  );
+
+export const actionCategoryById = (_id) =>
+  actionPromise(
+    "catById",
+    shopGQL(
+      `query catById($query:String){
+                                                CategoryFindOne(query:$query){
+                                                  _id name goods{
+                                                    _id name price description images{
+                                                      url
+                                                    }
+                                                  }
+                                                }
+                                              }`,
+      { query: JSON.stringify([{ _id }]) }
+    )
+  );
+
+export const actionGoodById = (_id) =>
+  actionPromise(
+    "goodById",
+    shopGQL(
+      `query goodById($query:String){
+                                                GoodFindOne(query:$query){
+                                                  _id name price description images {
+                                                      url
+                                                  }
+                                                }
+                                              }`,
+      { query: JSON.stringify([{ _id }]) }
+    )
+  );
+
+export default promiseReducer;

+ 21 - 0
src/redux/reduxStore.js

@@ -0,0 +1,21 @@
+import { combineReducers } from "redux";
+import { createStore, applyMiddleware } from "redux";
+import thunkMiddleware from "redux-thunk";
+import authReducer from "./authReducer";
+import promiseReducer from "./promiseReducer";
+import cartReducer from "./cartReducer";
+import {reducer as formReducer} from 'redux-form'
+
+
+let reducers = combineReducers({
+  promiseReducer: promiseReducer,
+  authReducer: authReducer,
+  cartReducer: cartReducer,
+  form: formReducer,
+});
+
+const store = createStore(reducers, applyMiddleware(thunkMiddleware));
+
+window.store = store;
+
+export default store;

+ 13 - 0
src/reportWebVitals.js

@@ -0,0 +1,13 @@
+const reportWebVitals = onPerfEntry => {
+  if (onPerfEntry && onPerfEntry instanceof Function) {
+    import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
+      getCLS(onPerfEntry);
+      getFID(onPerfEntry);
+      getFCP(onPerfEntry);
+      getLCP(onPerfEntry);
+      getTTFB(onPerfEntry);
+    });
+  }
+};
+
+export default reportWebVitals;

+ 5 - 0
src/setupTests.js

@@ -0,0 +1,5 @@
+// jest-dom adds custom jest matchers for asserting on DOM nodes.
+// allows you to do things like:
+// expect(element).toHaveTextContent(/react/i)
+// learn more: https://github.com/testing-library/jest-dom
+import '@testing-library/jest-dom';