App.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import React, {useState, useEffect } from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4. import {Provider, connect} from 'react-redux';
  5. import {actionSearch } from './actions';
  6. import store from './reducers'
  7. //import { CounterView, Counter, ConnectedBIGTABLO, ConnectedCounter, ConnectedBigButton } from './components'
  8. import {Router, Route, Link, Switch, Redirect} from 'react-router-dom';
  9. import createHistory from "history/createBrowserHistory";
  10. import {actionCategories, actionLogin, actionCategory} from './actions'
  11. const history = createHistory()
  12. const PageMain = () =>
  13. <>
  14. <h1>Главная магазина</h1>
  15. </>
  16. const LoginForm = ({onLogin}) => {
  17. const [login, setLogin] = useState("")
  18. const [password, setPassword] = useState("")
  19. return (
  20. <div>
  21. <input type='text' value={login} onChange={e => setLogin(e.target.value)} />
  22. <input type='password' value={password} onChange={e => setPassword(e.target.value)} />
  23. <button onClick={() => onLogin(login, password)}>Login</button>
  24. </div>
  25. )
  26. }
  27. const CLoginForm = connect(null, {onLogin: actionLogin})(LoginForm)
  28. store.dispatch(actionCategories())
  29. const CategoryMenuItem = ({category:{_id, name}={_id: 'NOID', name: "NO CATEGORY"}}) =>
  30. <li>
  31. <Link to={`/category/${_id}`}>{name}</Link>
  32. </li>
  33. const CategoryMenu = ({categories=
  34. [
  35. { "_id": "5dc45acf5df9d670df48cc48", "name": "TV's" },
  36. { "_id": "5dc49f4d5df9d670df48cc64", "name": "Airconditions" },
  37. { "_id": "5dc458985df9d670df48cc47", "name": "Smartphones" },
  38. ]
  39. }) =>
  40. <aside style={{float: 'left'}}>
  41. <ul>
  42. {categories &&
  43. categories.map(category => <CategoryMenuItem category={category}/>)}
  44. </ul>
  45. </aside>
  46. const CCategoryMenu = connect(state => ({categories: state.promise.categories &&
  47. state.promise.categories.payload &&
  48. state.promise.categories.payload.CategoryFind}))(CategoryMenu)
  49. const defaultGoods = [
  50. {
  51. "_id": "5dcaac1fe87d153c543bcef4",
  52. "name": "Гречневая лапша с овощами и курицей"
  53. },
  54. {
  55. "_id": "5dcabb776d09c45440d14cf0",
  56. "name": "Пшеничная лапша с овощами и курицей"
  57. },
  58. {
  59. "_id": "5dcabc636d09c45440d14cf1",
  60. "name": "Пшеничная лапша с овощами и свининой"
  61. }]
  62. const GoodCard = ({_id, name}) =>
  63. <li>
  64. <h2>{name}</h2>
  65. </li>
  66. const GoodList = ({goods=defaultGoods}) =>
  67. <ul>
  68. {goods && goods.map(good => <GoodCard key={good._id} {...good}/>)}
  69. </ul>
  70. const CGoodList = connect(
  71. state => ({goods: state.promise.category &&
  72. state.promise.category.payload &&
  73. state.promise.category.payload.CategoryFindOne &&
  74. state.promise.category.payload.CategoryFindOne.goods})
  75. )(GoodList)
  76. const PageCategory = ({match:{params:{_id}}, getData}) => {
  77. useEffect(() => (getData(_id), undefined), [_id])
  78. return (
  79. <>
  80. <h1>КАТЕГОРИЯ {_id}</h1>
  81. <CGoodList />
  82. </>
  83. )
  84. }
  85. const CPageCategory = connect(null, {getData: actionCategory})(PageCategory)
  86. export default () => {
  87. return (
  88. <Provider store={store}>
  89. <Router history={history}>
  90. <CCategoryMenu/>
  91. <main>
  92. <Route path='/' component={PageMain} exact />
  93. <Route path='/category/:_id' component={CPageCategory} exact />
  94. </main>
  95. </Router>
  96. </Provider>
  97. )
  98. }