index.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Korzinap!!!!!!!!!!!!!!!</title>
  6. <style>
  7. .Jorik{
  8. border-bottom: 1px dotted green;
  9. }
  10. #content {
  11. display: flex;
  12. }
  13. #aside {
  14. width: 30%;
  15. }
  16. #aside > a {
  17. display: block;
  18. }
  19. .wrapper{
  20. border: 1px dotted green;
  21. }
  22. img{
  23. width:300px;
  24. }
  25. main{
  26. padding-left: 20px;
  27. }
  28. table{
  29. border:1px solid black;
  30. }
  31. td{
  32. text-align: center;
  33. border:1px solid black;
  34. }
  35. header {
  36. display: flex;
  37. justify-content: space-between;
  38. }
  39. #ownCab, .wrapper{
  40. display: flex;
  41. justify-content: space-between;
  42. }
  43. #ownCab{
  44. margin-left: 1em;
  45. }
  46. #ownCab a, button{
  47. margin-left: 1em;
  48. }
  49. .cart img{
  50. width: 1.5em;
  51. height: 1.5em;
  52. }
  53. a {
  54. text-decoration: none;
  55. color:blue;
  56. }
  57. a:hover{
  58. color: green;
  59. }
  60. </style>
  61. </head>
  62. <body>
  63. <header>
  64. <div id="formId"></div>
  65. <div class="wrapper">
  66. <div class="cart">
  67. <a href="#/cart">
  68. <span id="cartQuantity"></span>
  69. <img src="xz.png" />
  70. </a>
  71. </div>
  72. <div id="ownCab"></div>
  73. </div>
  74. </header>
  75. <div id='content'>
  76. <aside id='aside'>
  77. Категории
  78. </aside>
  79. <main id='main'>
  80. Контент
  81. </main>
  82. </div>
  83. <script>
  84. //----------------------Store---------------------------------------
  85. function createStore(reducer){
  86. let state = reducer(undefined, {})
  87. let cbs = []
  88. const getState = () => state
  89. const subscribe = cb => (cbs.push(cb),
  90. () => cbs = cbs.filter(c => c !== cb))
  91. const dispatch = action => {
  92. if (typeof action === 'function'){
  93. return action(dispatch, getState)
  94. }
  95. const newState = reducer(state, action)
  96. if (newState !== state){
  97. state = newState
  98. for (let cb of cbs) cb()
  99. }
  100. }
  101. return {
  102. getState,
  103. dispatch,
  104. subscribe
  105. }
  106. }
  107. //-----------------------------------------------------------------------------
  108. //написать jwtDecode = token =>({})
  109. const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOnsiaWQiOiI2MWE0ZTA1MmM3NTBjMTJiYTZiYTQwMjkiLCJsb2dpbiI6InZsYWRCcmF1bjQiLCJhY2wiOlsiNjFhNGUwNTJjNzUwYzEyYmE2YmE0MDI5IiwidXNlciJdfSwiaWF0IjoxNjM4NTM5NTUzfQ.oPRus9nGS1rg69eKu8rK-tMi4V-hN5HXE0NOzAc5K4k";
  110. //выкусить из токена серединку
  111. //сделать base64 декод (atob)
  112. //с результатом сделать JSON.parse
  113. const jwtDecode = token =>{
  114. try{
  115. let mid=token.split('.');
  116. let tok=mid[1];
  117. let tokenDecode=atob(tok);
  118. let finalTok=JSON.parse(tokenDecode);
  119. return finalTok;
  120. }catch(e){
  121. console.log(e);
  122. }
  123. }
  124. console.log(jwtDecode(token));
  125. //-------------------------------------------------------------------------------
  126. function authReducer(state, {type, token}) {
  127. if(!state) {
  128. if (localStorage.authToken) {
  129. type = 'AUTH_LOGIN'
  130. token = localStorage.authToken
  131. } else {
  132. return {}
  133. }
  134. }
  135. if (type === 'AUTH_LOGIN') {
  136. localStorage.authToken = token
  137. let payload = jwtDecode(token)
  138. if (typeof payload !== 'object') {
  139. return {}
  140. }
  141. return {token, payload}
  142. }
  143. if (type === 'AUTH_LOGOUT') {
  144. // debugger
  145. localStorage.removeItem('authToken')
  146. return {}
  147. }
  148. return state
  149. }
  150. // const store = createStore(authReducer)
  151. const actionAuthLogin = token => ({type: 'AUTH_LOGIN', token})
  152. const actionAuthLogout = () => ({type: 'AUTH_LOGOUT'})
  153. // login.onclick = () => store.dispatch(actionAuthLogin(token))
  154. // logout.onclick = () => store.dispatch(actionAuthLogout())
  155. //------------------------------------------------------------------------------
  156. function combineReducers(reducers) {
  157. return (state = {}, action) => {
  158. const newState = {}
  159. for (const [reducerName, reducer] of Object.entries(reducers)) {
  160. let newSubstate = reducer(state[reducerName], action)
  161. if (newSubstate !== state[reducerName]) {
  162. newState[reducerName] = newSubstate
  163. }
  164. }
  165. if (Object.entries(newState).length !== 0) {
  166. return {...state, ...newState}
  167. } else return state
  168. }
  169. }
  170. //-------------------------------------------------------------------------------
  171. function promiseReducer(state = {}, { type, name, status, payload, error }) {
  172. //{
  173. // login: {status, payload, error}
  174. // catById: {status, payload, error}
  175. //}
  176. if (type === 'PROMISE') {
  177. return {
  178. ...state,
  179. [name]: { status, payload, error }
  180. }
  181. }
  182. return state
  183. }
  184. const actionPending = name => ({ type: 'PROMISE', status: 'PENDING', name })
  185. const actionResolved = (name, payload) => ({ type: 'PROMISE', status: 'RESOLVED', name, payload })
  186. const actionRejected = (name, error) => ({ type: 'PROMISE', status: 'REJECTED', name, error })
  187. const actionPromise = (name, promise) =>
  188. async dispatch => {
  189. dispatch(actionPending(name)) // 1. {delay1000: {status: 'PENDING'}}
  190. try {
  191. let payload = await promise
  192. dispatch(actionResolved(name, payload))
  193. return payload
  194. }
  195. catch (error) {
  196. dispatch(actionRejected(name, error))
  197. }
  198. }
  199. // const store = createStore(promiseReducer)
  200. // store.subscribe(() => console.log(store.getState()))
  201. // const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  202. // store.dispatch(actionPromise('delay1000', delay(1000)))
  203. // store.dispatch(actionPromise('delay2000', delay(2000)))
  204. // store.dispatch(actionPromise('failedfetch', fetch('https://swapi.dev/api/people/1/')
  205. // .then(res => res.json())))
  206. //--------------------------------------------------------------
  207. function cartReducer(state = {}, {type, good = {}, count = 1}) {
  208. const {_id} = good
  209. if(!count) {
  210. return state
  211. }
  212. const types = {
  213. CART_ADD(){
  214. //берет старую позицию, и добавляет count к текущему количеству.
  215. //если позиции нет - то добавляет к 0 (т. е. в первый раз будет count)
  216. count = +count
  217. return {
  218. ...state,
  219. [_id]: {good, count:(state[_id] ? state[_id].count : 0) + count}
  220. }
  221. },
  222. CART_CHANGE(){
  223. //тупо меняет позицию
  224. return {
  225. ...state,
  226. [_id]: {good, count}
  227. }
  228. },
  229. CART_REMOVE(){
  230. //надо как-то создать объект без ключа
  231. let {[_id]: remove, ...rest} = state
  232. return rest
  233. },
  234. CART_CLEAR(){
  235. //самое простое
  236. return {}
  237. }
  238. }
  239. if(type in types) {
  240. return types[type]()
  241. }
  242. return state
  243. }
  244. const actionCartAdd = (good, count) => ({type: 'CART_ADD', good, count})
  245. const actionCartChange = (good, count) => ({type: 'CART_CHANGE', good, count})
  246. const actionCartRemove = (good) => ({type: 'CART_REMOVE', good})
  247. const actionCartClear = () => ({type: 'CART_CLEAR'})
  248. //-------------------------------------------------------------------------
  249. const combinedReducer = combineReducers({promise: promiseReducer, auth: authReducer, cart: cartReducer}) //тут еще
  250. const store = createStore(combinedReducer)
  251. console.log(store.getState())
  252. // store.dispatch(actionPromise('delay1000', delay('1000')))
  253. // store.dispatch(actionAuthLogin(token))
  254. //--------------------------------------------------------------------------------
  255. const getGQL = url =>
  256. (query, variables = {}) =>
  257. fetch(url, {
  258. method: 'POST',
  259. headers: {
  260. "Content-Type": "application/json",
  261. ...(localStorage.authToken ? { "Authorization": "Bearer " + localStorage.authToken } : {})
  262. },
  263. body: JSON.stringify({ query, variables })
  264. })
  265. .then(res => res.json())
  266. .then(data => {
  267. if (data.errors && !data.data)
  268. throw new Error(JSON.stringify(data.errors))
  269. return data.data[Object.keys(data.data)[0]]
  270. })
  271. const backURL = 'http://shop-roles.asmer.fs.a-level.com.ua'
  272. const gql = getGQL(backURL + '/graphql')
  273. //-------------------------Actions----------------------------------------
  274. const actionRootCats = () =>
  275. actionPromise('rootCats', gql(`query {
  276. CategoryFind(query: "[{\\"parent\\":null}]"){
  277. _id name
  278. }
  279. }`))
  280. const actionCatById = (_id) => //добавить подкатегории
  281. actionPromise('catById', gql(`query catById($q: String){
  282. CategoryFindOne(query: $q) {
  283. _id name goods{
  284. _id name price description images {
  285. url
  286. }
  287. }
  288. subCategories {
  289. name _id goods {
  290. _id name description
  291. }
  292. }
  293. }
  294. }`, { q: JSON.stringify([{ _id }]) }))
  295. const actionGoodById = (_id) =>
  296. actionPromise('goodById', gql(`query goodById($q: String){
  297. GoodFindOne(query: $q){
  298. _id name description price images{
  299. url
  300. }
  301. }
  302. }`, { q: JSON.stringify([{ _id }]) }))
  303. store.dispatch(actionRootCats())
  304. const actionOrder = () =>
  305. async (dispatch, getState) => {
  306. let {cart} = store.getState()
  307. const orderGoods = Object.entries(cart)
  308. .map(([_id, {good, count}]) => ({good: {_id}, count}))
  309. let result = await dispatch(actionPromise('order', gql(`
  310. mutation newOrder($order:OrderInput){
  311. OrderUpsert(order:$order)
  312. { _id total }
  313. }`, {order: {orderGoods}})))
  314. if(result?._id) {
  315. store.dispatch(actionCartClear())
  316. }
  317. }
  318. const actionMyOrders = () =>
  319. actionPromise('orderfind', gql(`query orderfind{
  320. OrderFind(query: "[{}]"){
  321. _id createdAt total orderGoods{
  322. _id createdAt price count
  323. good{
  324. _id description name images{
  325. _id url
  326. }
  327. }
  328. }
  329. }
  330. }`))
  331. const actionLogin = (login, password) =>
  332. actionPromise('login', gql(`query login($login: String, $password: String){
  333. login(login: $login, password: $password)
  334. }`, {login: login, password: password})
  335. )
  336. const actionFullLogin = (login, password) =>
  337. async dispatch => {
  338. let token = await dispatch(actionLogin(login, password))
  339. if(token) {
  340. dispatch(actionAuthLogin(token))
  341. }
  342. }
  343. const actionRegister = (login, password) =>
  344. actionPromise('register', gql(`mutation registration($login: String, $password:String) {
  345. UserUpsert(user: {login: $login,
  346. password:$password,
  347. nick: $login}){
  348. _id login
  349. }
  350. }`, {login: login, password: password})
  351. )
  352. const actionFullRegister = (login, password) =>
  353. async dispatch => {
  354. let log = await dispatch(actionRegister(login, password))
  355. if (log) {
  356. let token = await dispatch(actionLogin(login, password))
  357. if (token){
  358. dispatch(actionAuthLogin(token))
  359. }
  360. }
  361. }
  362. //--------------------------main Page--------------------------------------------
  363. store.subscribe(() => {
  364. const { promise } = store.getState()
  365. console.log('------------')
  366. console.log(promise)
  367. if (promise?.rootCats?.payload) {
  368. aside.innerHTML = ''
  369. for (const { _id, name } of promise?.rootCats?.payload) {
  370. const link = document.createElement('a')
  371. link.href = `#/category/${_id}`
  372. link.innerText = name
  373. aside.append(link)
  374. }
  375. }
  376. })
  377. store.subscribe(() => {
  378. const { promise } = store.getState()
  379. const [, route, _id] = location.hash.split('/')
  380. if (promise?.catById?.payload && route === 'category') {
  381. const { name } = promise.catById.payload
  382. main.innerHTML = `<h1 class="Jorik">${name}</h1>`
  383. if (promise.catById.payload?.subCategories) {
  384. for (let { _id, name } of promise.catById.payload.subCategories) {
  385. const podCat = document.createElement('a')
  386. podCat.href = `#/category/${_id}`
  387. podCat.innerText = name
  388. main.append(podCat)
  389. }
  390. }
  391. for (const good of promise.catById.payload.goods) {
  392. const {_id, name, price, images} = good
  393. const card = document.createElement('div')
  394. card.innerHTML = `<h2>${name}</h2>
  395. <img src="${backURL}/${images[0].url}"/>
  396. <div>
  397. <b>Стоимость:</b> <b><sub>${price}UAH</sub></b>
  398. <br><a href=#/good/${_id}>Страница товара</a>
  399. </div>`
  400. main.append(card)
  401. let btn = document.createElement('button')
  402. btn.innerText = 'Добавить в корзину'
  403. btn.style.marginLeft=50+'%'
  404. btn.onclick = () => store.dispatch(actionCartAdd(good, 1))
  405. card.append(btn)
  406. }
  407. }
  408. })
  409. //-----------------------Korzina---------------------------
  410. store.subscribe(() => {
  411. const {cart} = store.getState()
  412. console.log(cart)
  413. if (cart){
  414. let num = 0
  415. cartQuantity.innerText = ''
  416. for (let key in cart) {
  417. num+=cart[key].count
  418. }
  419. cartQuantity.innerText = num
  420. }
  421. })
  422. function showCart() {
  423. const { cart } = store.getState()
  424. console.log(cart)
  425. main.innerHTML = ``
  426. let num = 1
  427. let count = 0
  428. let total = 0
  429. let h = document.createElement('h2')
  430. h.innerText = 'Корзина';
  431. h.style.borderBottom = 1+'px solid black'
  432. main.append(h)
  433. const table = document.createElement('table')
  434. for (let key in cart) {
  435. let { good, count } = cart[key]
  436. console.log(good, count)
  437. let cartKey = document.createElement('tr')
  438. cartKey.innerHTML = `<td>${num++}</td><td><img src="${backURL}/${good.images[0].url}"/></td><td>${good.name}</td>
  439. <td>${good.price} UAH</td>`
  440. let addBtnTd = document.createElement('td')
  441. let addBtn = document.createElement('button')
  442. addBtn.innerText = '+'
  443. addBtnTd.append(addBtn)
  444. let chngTd = document.createElement('td')
  445. let chng = document.createElement('input')
  446. chng.type = 'number'
  447. chng.min = 0
  448. chng.max = 50
  449. chng.value = count
  450. chngTd.append(chng)
  451. let subBtnTd = document.createElement('td')
  452. let subBtn = document.createElement('button')
  453. subBtn.innerText = '-'
  454. subBtnTd.append(subBtn)
  455. let deleteBtnTd = document.createElement('td')
  456. let deleteBtn = document.createElement('button')
  457. deleteBtn.innerText = 'Удалить'
  458. deleteBtnTd.append(deleteBtn)
  459. chng.oninput = () => store.dispatch(actionCartChange(good, chng.value))
  460. deleteBtn.onclick = () => {
  461. cartKey.remove()
  462. store.dispatch(actionCartRemove(good))
  463. }
  464. addBtn.onclick = () => {
  465. store.dispatch(actionCartAdd(good, 1))
  466. chng.value++
  467. }
  468. subBtn.onclick = () => {
  469. store.dispatch(actionCartAdd(good, -1))
  470. chng.value--
  471. }
  472. cartKey.append(addBtnTd, chngTd, subBtnTd, deleteBtnTd)
  473. table.append(cartKey)
  474. main.append(table)
  475. count += parseInt(chng.value)
  476. total += good.price * chng.value
  477. }
  478. let clearBtn = document.createElement('button')
  479. clearBtn.innerText = "Очистить корзину";
  480. Object.entries(cart).length > 0 ? main.append(clearBtn) : null
  481. clearBtn.onclick = () => {
  482. store.dispatch(actionCartClear())
  483. table.remove()
  484. clearBtn.style.display = 'none';
  485. canOrder.style.display = 'none';
  486. }
  487. let canOrder = document.createElement('button')
  488. canOrder.innerText = "Оформить заказ"
  489. Object.entries(cart).length > 0 ? main.append(canOrder) : null
  490. if (localStorage.authToken) {
  491. canOrder.disabled = false
  492. } else {
  493. canOrder.disabled = true
  494. }
  495. canOrder.onclick = () => {
  496. store.dispatch(actionOrder())
  497. table.remove()
  498. clearBtn.style.display = 'none';
  499. canOrder.style.display = 'none';
  500. }
  501. }
  502. //-----------------opis tovara--------------------------
  503. store.subscribe(() => {
  504. const { promise } = store.getState()
  505. const [, route, _id] = location.hash.split('/')
  506. if (promise?.goodById?.payload && route === 'good' && location.href.includes(`#/good/${_id}`)) {
  507. main.innerHTML = ``
  508. let { _id, name, price, images, description } = promise.goodById.payload
  509. let goodItem = document.createElement('div')
  510. goodItem.className = 'good_item'
  511. goodItem.innerHTML = `<h2>${name}</h2>
  512. <img src="${backURL}/${images[0].url}"/>
  513. <div>
  514. <p><b>Стоимость:</b> <b><sub>${price} UAH</sub></b></strong></p>
  515. <p><b>Описание:</b> <sub>${description}</sub></p>
  516. </div>`
  517. main.append(goodItem)
  518. }
  519. })
  520. //--------------------------Orders-----------------------------
  521. store.subscribe(() => {
  522. const {promise} = store.getState()
  523. const [,route, _id] = location.hash.split('/')
  524. if (promise?.orderfind?.payload && route === 'dashboard'){
  525. main.innerHTML = ''
  526. let title = document.createElement('h2')
  527. title.innerText = 'Ваши заказы';
  528. title.style.textAlign='left'
  529. title.style.borderBottom=1+'px solid black'
  530. main.append(title)
  531. for (let order of promise.orderfind.payload) {
  532. let quantity = 0
  533. const {total, orderGoods} = order
  534. let table = document.createElement('table')
  535. let thead = document.createElement('thead')
  536. thead.innerHTML = `<th class="user-order">Дата заказа</th>
  537. <th>Наименование</th>
  538. <th>Количество</th>
  539. <th>Цена</th>`
  540. table.append(thead)
  541. for (let {createdAt, count, good, price} of orderGoods){
  542. quantity += count;
  543. let date = new Date(+createdAt).toLocaleDateString();
  544. let tr = document.createElement('tr')
  545. tr.innerHTML = `<td>${date}</td>
  546. <td><figure class='good-img'>
  547. <img src="${backURL}/${good.images[0].url}">
  548. <figcaption>${good.name}</figcaption>
  549. </figure></td>
  550. <td>${count}</td>
  551. <td>${price}</td>`
  552. table.append(tr)
  553. }
  554. let totalOrderAmount = document.createElement('tr')
  555. totalOrderAmount.innerHTML = `<th class="user-order">Всего товаров в заказе: ${quantity}</th>
  556. <th>Общая сумма заказа: ${total}</th>`
  557. table.append(totalOrderAmount)
  558. main.append(table)
  559. }
  560. }
  561. else if (!promise?.orderfind?.payload && route === 'dashboard'){
  562. main.innerHTML = ''
  563. let title = document.createElement('h2')
  564. title.textContent = 'Вы еще не сделали заказ';
  565. main.append(title)
  566. }
  567. })
  568. //----------------------Login-------------------------------
  569. store.subscribe(() => {
  570. const {auth} = store.getState()
  571. const {payload} = auth
  572. if (payload?.sub ) {
  573. ownCab.innerHTML = ''
  574. //ownCab.style.marginTop=10+'px'
  575. //ownCab.style.border=5+'px solid blue'
  576. //ownCab.style.width=17+'%'
  577. const {id, login} = payload.sub
  578. const userName = document.createElement('div')
  579. userName.innerHTML = `Hello, ${login}`
  580. const userOrders = document.createElement('a')
  581. //userOrders.style.marginRight=10+'px'
  582. userOrders.innerText = 'Your Orders'
  583. userOrders.href = `#/dashboard/`
  584. let logout = document.createElement('button')
  585. logout.textContent = 'Exit'
  586. logout.onclick = () => {
  587. formId.innerHTML = ''
  588. store.dispatch(actionAuthLogout());
  589. }
  590. ownCab.append(userName, userOrders, logout)
  591. } else {
  592. ownCab.innerHTML = ''
  593. }
  594. })
  595. store.subscribe(() => {
  596. const {auth} = store.getState()
  597. if (!auth?.payload){
  598. formId.innerHTML = '';
  599. let twoBtns=document.createElement('span')
  600. let loginBtn = document.createElement('a');
  601. let regBtn = document.createElement('a');
  602. twoBtns.append(loginBtn, regBtn)
  603. twoBtns.style.border=1+'px dotted green'
  604. formId.append(twoBtns)
  605. loginBtn.innerText = 'LogIn'
  606. regBtn.innerText = 'Registration'
  607. loginBtn.onclick = () => {
  608. loginBtn.style.display = 'none'
  609. regBtn.style.display= 'none'
  610. let form = document.createElement('form')
  611. let login = document.createElement('input')
  612. login.type = 'text'
  613. login.placeholder = 'Login'
  614. let password = document.createElement('input')
  615. password.placeholder = 'Password'
  616. password.type = 'password'
  617. let btn = document.createElement('a')
  618. btn.innerText = 'Enter'
  619. btn.onclick = () => {
  620. if (login.value !== '' && password.value !== '') {
  621. btn.href = `#/login/${login.value}*${password.value}`
  622. }
  623. formId.innerHTML = ''
  624. }
  625. form.append(login, password, btn)
  626. formId.append(form)
  627. }
  628. regBtn.onclick = () => {
  629. loginBtn.style.display = 'none'
  630. regBtn.style.display= 'none'
  631. let form = document.createElement('form')
  632. let login = document.createElement('input')
  633. login.type = 'text'
  634. login.placeholder = 'Login'
  635. let pass = document.createElement('input')
  636. pass.placeholder = 'Password'
  637. pass.type = 'password'
  638. let btn = document.createElement('a')
  639. btn.innerText = 'Registration'
  640. btn.onclick = () => {
  641. if (login.value !== '' && pass.value !== '') {
  642. btn.href = `#/register/${login.value}*${pass.value}`
  643. }
  644. formId.innerHTML = ''
  645. }
  646. form.append(login, password, button)
  647. formId.append(form)
  648. }
  649. }
  650. })
  651. //-------------------------------------------------------------
  652. window.onhashchange = () => {
  653. const [, route, _id] = location.hash.split('/')
  654. const routes = {
  655. category() {
  656. store.dispatch(actionCatById(_id))
  657. },
  658. good() {
  659. //задиспатчить actionGoodById
  660. store.dispatch(actionGoodById(_id))
  661. console.log('ТОВАРОСТРАНИЦА')
  662. },
  663. login(){
  664. let data = _id.split('*')
  665. store.dispatch(actionFullLogin(data[0], data[1]))
  666. console.log('ЛОГИН')
  667. },
  668. register(){
  669. let data = _id.split('*')
  670. store.dispatch(actionFullRegister(data[0], data[1]))
  671. console.log('РЕГА')
  672. },
  673. cart(){
  674. showCart()
  675. console.log('Сделать страницу с позициями, полями ввода колличества, картинками и кнопкой')
  676. },
  677. dashboard(){
  678. store.dispatch(actionMyOrders())
  679. console.log('Прочитать бывшие заказы')
  680. }
  681. }
  682. if (route in routes)
  683. routes[route]()
  684. }
  685. window.onhashchange()
  686. </script>
  687. </body>
  688. </html>