main.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. function createStore(reducer){
  2. let state = reducer(undefined, {}) //стартовая инициализация состояния, запуск редьюсера со state === undefined
  3. let cbs = [] //массив подписчиков
  4. const getState = () => state //функция, возвращающая переменную из замыкания
  5. const subscribe = cb => (cbs.push(cb), //запоминаем подписчиков в массиве
  6. () => cbs = cbs.filter(c => c !== cb)) //возвращаем функцию unsubscribe, которая удаляет подписчика из списка
  7. const dispatch = action => {
  8. if (typeof action === 'function'){ //если action - не объект, а функция
  9. return action(dispatch, getState) //запускаем эту функцию и даем ей dispatch и getState для работы
  10. }
  11. const newState = reducer(state, action) //пробуем запустить редьюсер
  12. if (newState !== state){ //проверяем, смог ли редьюсер обработать action
  13. state = newState //если смог, то обновляем state
  14. for (let cb of cbs) cb(state) //и запускаем подписчиков
  15. }
  16. }
  17. return {
  18. getState, //добавление функции getState в результирующий объект
  19. dispatch,
  20. subscribe //добавление subscribe в объект
  21. }
  22. }
  23. //JWT
  24. const jwt = (token) =>{
  25. try {
  26. let payload = JSON.parse(atob(token.split(".")[1]))
  27. return payload
  28. }
  29. catch(error){
  30. return undefined
  31. }
  32. }
  33. const reducers = {
  34. promise: promiseReducer, //допилить много имен для многих промисов
  35. auth: localStoredReducer(authReducer,"auth"), //часть предыдущего ДЗ
  36. cart: localStoredReducer(cartReducer,"cart"), //часть предыдущего ДЗ
  37. }
  38. //promiseReducer
  39. const totalReducer = combineReducers(reducers)
  40. function promiseReducer(state={}, {type,name, status, payload, error}){
  41. if (type === 'PROMISE'){
  42. //имена добавить
  43. return {
  44. ...state,
  45. [name] : {status, payload, error}
  46. }
  47. }
  48. return state
  49. }
  50. //CombineReducer
  51. function combineReducers(reducers){
  52. function totalReducer(state={}, action){
  53. const newTotalState = {}
  54. for (const [reducerName, reducer] of Object.entries(reducers)){
  55. const newSubState = reducer(state[reducerName], action)
  56. if (newSubState !== state[reducerName]){
  57. newTotalState[reducerName] = newSubState
  58. }
  59. }
  60. if (Object.keys(newTotalState).length){
  61. return {...state, ...newTotalState}
  62. }
  63. return state
  64. }
  65. return totalReducer
  66. }
  67. function getGql (endpoint){
  68. return async function gql(query, variables={}) {
  69. let headers = {
  70. 'Content-Type': 'application/json;charset=utf-8',
  71. 'Accept': 'application/json',
  72. }
  73. if ("authToken" in localStorage) {
  74. headers.Authorization = "Bearer " + localStorage.authToken
  75. }
  76. let result = await fetch(endpoint, {
  77. method: 'POST',
  78. headers,
  79. body: JSON.stringify({
  80. query,
  81. variables
  82. })
  83. }).then(res => res.json())
  84. if (("errors" in result) && !("data" in result)) {
  85. throw new Error(JSON.stringify(result.errors))
  86. }
  87. result = Object.values(result.data)[0]
  88. return result
  89. }
  90. }
  91. gql = getGql('http://shop-roles.node.ed.asmer.org.ua/graphql/')
  92. //authReducer
  93. function authReducer (state={},{type,token}){
  94. if(type === "AUTH_LOGIN"){
  95. try{
  96. window.localStorage.setItem("authToken",token)
  97. return {
  98. token: token,
  99. payload : jwt(token)
  100. }
  101. }
  102. catch(e){
  103. console.log(e)
  104. }
  105. }
  106. if(type === "AUTH_LOGOUT"){
  107. window.localStorage.removeItem("authToken")
  108. return {}
  109. }
  110. return state
  111. }
  112. //cartReducer
  113. function cartReducer (state={},{type,good,count=1}){
  114. if(type === 'CART_ADD'){
  115. return{
  116. ...state,
  117. [good._id]: {
  118. good,
  119. count : +count
  120. }
  121. }
  122. }
  123. if(type === "CART_SUB"){
  124. if(state[good._id].count-count <= 0){
  125. delete state[good._id]
  126. }
  127. else {
  128. return {
  129. ...state,
  130. [good._id] : {
  131. good,
  132. count: state[good._id].count - count,
  133. }
  134. }
  135. }
  136. }
  137. if(type === "CART_DEL"){
  138. delete state[good._id]
  139. return {
  140. ...state
  141. }
  142. }
  143. if(type==="CART_SET"){
  144. if(state[good._id].count <= 0){
  145. delete state[good.id]
  146. }
  147. else {
  148. return {
  149. ...state,
  150. [good._id] : {
  151. good,
  152. count
  153. }
  154. }
  155. }
  156. }
  157. if(type === "CART_CLEAR"){
  158. return {}
  159. }
  160. return state
  161. }
  162. //имена добавить
  163. const actionPromise = (name,promise) =>
  164. async dispatch => {
  165. dispatch(actionPending(name)) //сигнализируем redux, что промис начался
  166. try{
  167. const payload = await promise //ожидаем промиса
  168. dispatch(actionFulfilled(name,payload)) //сигнализируем redux, что промис успешно выполнен
  169. return payload //в месте запуска store.dispatch с этим thunk можно так же получить результат промиса
  170. }
  171. catch (error){
  172. dispatch(actionRejected(name,error)) //в случае ошибки - сигнализируем redux, что промис несложился
  173. }
  174. }
  175. //ActionPromise
  176. const actionPending = (name) => ({type: 'PROMISE',name, status: 'PENDING'})
  177. const actionFulfilled = (name,payload) => ({type: 'PROMISE',name, status: 'FULFILLED', payload})
  178. const actionRejected = (name,error) => ({type: 'PROMISE',name, status: 'REJECTED', error})
  179. //ActionLogin
  180. const actionAuthLogin = token => ({type: 'AUTH_LOGIN', token})
  181. const actionAuthLogout = () => ({type: 'AUTH_LOGOUT'})
  182. //ActionCart
  183. const actionCartAdd = (good, count=1) => ({type: 'CART_ADD', count, good})
  184. const actionCartSub = (good, count=1) => ({type: 'CART_SUB', count, good})
  185. const actionCartDel = (good) => ({type: 'CART_DEL', good})
  186. const actionCartSet = (good, count=1) => ({type: 'CART_SET', count, good})
  187. const actionCartClear = () => ({type: 'CART_CLEAR'})
  188. //LocalStoredReducer
  189. function localStoredReducer(originalReducer, localStorageKey){
  190. function wrapper(state, action){
  191. if(!state){
  192. try{
  193. return JSON.parse(localStorage[localStorageKey])
  194. }
  195. catch {}
  196. }
  197. let resp = originalReducer(state,action)
  198. localStorage[localStorageKey] = JSON.stringify(resp)
  199. return resp
  200. }
  201. return wrapper
  202. }
  203. const store = createStore(totalReducer)
  204. store.subscribe(() => console.log(store.getState()))
  205. //const store = createStore(localStoredReducer(cartReducer, 'cart'))
  206. //store = createStore(totalReducer) //не забудьте combineReducers если он у вас уже есть
  207. //store.subscribe(() => console.log(store.getState()))
  208. let queryFindRoots = `query findRoots($q:String) {
  209. CategoryFind(query: $q){
  210. _id name parent{
  211. _id name
  212. }
  213. }
  214. }`
  215. let variablesFindRoots = {
  216. q:JSON.stringify([{parent:null}])
  217. }
  218. //gql(queryFindRoots , variablesFindRoots ).then(console.log)
  219. const actionFindRoots = () => actionPromise("findRoots",gql(queryFindRoots, variablesFindRoots))
  220. store.dispatch(actionFindRoots())
  221. let queryCatFindOne = `query categoryFindOne ($q : String) {
  222. CategoryFindOne(query: $q){
  223. _id name parent{
  224. _id name
  225. }
  226. goods{
  227. _id name description price
  228. images{
  229. url
  230. }
  231. }
  232. subCategories{
  233. _id name
  234. }
  235. }
  236. }
  237. `
  238. const actionCatFindOne = (_id) => actionPromise("CatFindOne",gql(queryCatFindOne,{
  239. q:JSON.stringify([{ _id }])
  240. } ))
  241. let queryGoodFindOne = `query goodfindOne ($q : String) {
  242. GoodFindOne(query: $q){
  243. _id name price description
  244. images{
  245. url
  246. }
  247. }
  248. }`
  249. const actionGoodFindOne = (_id) => actionPromise("GoodFindOne",gql(queryGoodFindOne, {
  250. q:JSON.stringify([{ _id }])
  251. }))
  252. //Запрос на регістрацію
  253. let mutationRegistr = `mutation registration($login:String, $password: String) {
  254. UserUpsert(user : {login:$login, password: $password}){
  255. _id createdAt login
  256. }
  257. }`
  258. const actionRegistr = (login,password) => actionPromise("Registr",gql( mutationRegistr, {"login" : login , "password" : password}))
  259. //Зaпрос на користувача
  260. let queryLogin = `query login($login:String,$password:String){
  261. login(login:$login,password:$password)
  262. }`
  263. const actionLogin = (login,password) => actionPromise("Login",gql(queryLogin, {login,password}))
  264. //Історія замовлень
  265. let queryHistoryOrder = `query historyOrder($order : String){
  266. OrderFind(query: $order){
  267. _id createdAt total owner {
  268. _id
  269. createdAt
  270. login
  271. nick
  272. } orderGoods{
  273. _id count good {
  274. _id
  275. createdAt
  276. name
  277. description
  278. price
  279. } total
  280. }
  281. }
  282. }
  283. `
  284. const HistoryOrder = () => actionPromise("HistoryOrder",gql(queryHistoryOrder, {
  285. order:JSON.stringify([{}])
  286. }
  287. ))
  288. //Заказ
  289. const newOrder = (orderGoods) =>
  290. actionPromise("NewOrder",gql(
  291. `mutation NewOrder($order: OrderInput) {
  292. OrderUpsert(order: $order) {
  293. _id
  294. orderGoods {
  295. _id
  296. price
  297. count
  298. total
  299. good {
  300. name
  301. _id
  302. price
  303. images {
  304. url
  305. }
  306. }
  307. }
  308. }
  309. }`,
  310. { order: { orderGoods } }
  311. )
  312. )
  313. // Лічильник в корзині
  314. store.subscribe (()=>{
  315. let counter = 0
  316. for(let {count} of Object.values(store.getState().cart)){
  317. counter += count
  318. }
  319. cartIcon.innerHTML = counter;
  320. })
  321. //Анон чи Логін
  322. store.subscribe(()=>{
  323. loginName.innerHTML = store.getState().auth.payload?.sub?.login || "anon"
  324. })
  325. //Якщо вже отримав токен в минулий раз
  326. store.subscribe(()=>{
  327. let result = store.getState().auth?.token
  328. if(typeof(result)==="string"){
  329. afterLoginization()
  330. }
  331. })
  332. //URL for img
  333. const url = 'http://shop-roles.node.ed.asmer.org.ua/'
  334. store.subscribe(()=>{
  335. let {status,payload,error} = store.getState().promise?.findRoots
  336. if(status === "PENDING"){
  337. aside.innerHTML = ""
  338. }
  339. if(status === "FULFILLED"){
  340. aside.innerHTML = ""
  341. for(let {_id,name} of payload){
  342. let div = document.createElement('div')
  343. div.innerHTML = `<a href='#/category/${_id}'>${name}</a>`
  344. div.classList.add('asideLink')
  345. document.getElementById('aside').append(div)
  346. }
  347. }
  348. })
  349. //Категорії
  350. store.subscribe(()=>{
  351. let {status,payload,error} = store.getState().promise?.CatFindOne || {}
  352. const [,route, _id] = location.hash.split('/')
  353. if(route !== "category"){
  354. return
  355. }
  356. if(status === "PENDING"){
  357. mainCard.innerHTML = `<img src = 'https://i.pinimg.com/originals/71/3a/32/713a3272124cc57ba9e9fb7f59e9ab3b.gif'>`
  358. }
  359. if(status === "FULFILLED"){
  360. const {name,subCategories,goods} = payload
  361. mainCard.innerHTML = ""
  362. if(payload && route === "category"){
  363. mainCard.innerHTML += `<h1>${name} </h1>`
  364. if(payload.subCategories !== null){
  365. for(let {_id,name} of subCategories){
  366. mainCard.innerHTML += `<a href="#/category/${_id}>${name}</a>`
  367. console.log(name)
  368. }
  369. }
  370. for(const {_id,name,images} of goods){
  371. for (let img of images){
  372. mainCard.innerHTML += `<img src ="${url + img.url}" style="width:10%;"> `
  373. }
  374. mainCard.innerHTML += `<a href='#/good/${_id}'>${name}</a></br>`
  375. }
  376. }
  377. }
  378. })
  379. //Товар
  380. store.subscribe(()=>{
  381. let {status,payload,error} = store.getState().promise?.GoodFindOne || {}
  382. const [,route, _id] = location.hash.split('/')
  383. if(route !== "good"){
  384. return
  385. }
  386. if(status === "PENDING"){
  387. mainCard.innerHTML = `<img src = 'https://i.pinimg.com/originals/71/3a/32/713a3272124cc57ba9e9fb7f59e9ab3b.gif'>`
  388. }
  389. if(status === "FULFILLED"){
  390. mainCard.innerHTML = ""
  391. if(payload && route === "good"){
  392. let {_id,name,description,images,price} = payload
  393. mainCard.innerHTML = ""
  394. let divMain = document.createElement('div')
  395. divMain.id = "divMain"
  396. divMain.innerHTML += `<h1>${name} </h1>`
  397. divMain.innerHTML += `<h3>${description}</h3>`
  398. for(let img of images){
  399. divMain.innerHTML +=`<img src ="${url + img.url}"> `
  400. }
  401. divMain.innerHTML += `<h3>${price} $</h3>`
  402. divMain.innerHTML +=`<button id="buy">Buy</button>`
  403. mainCard.append(divMain)
  404. buy.addEventListener("click",function(){
  405. store.dispatch(actionCartAdd({_id, name, price: price,img:images}))
  406. })
  407. }
  408. }
  409. })
  410. //store = createStore(cartReducer);
  411. //store.dispatch(actionCartAdd({ _id: 'пиво', price: 50 }))
  412. //store.dispatch(actionCartAdd({ _id: 'чипсы', price: 75 }));
  413. //store.dispatch(actionCartAdd({ _id: 'пиво', price: 50 }, 5));
  414. //store.dispatch(actionCartSet({ _id: 'чипсы', price: 75 }, 2));
  415. //store.dispatch(actionCartSub({ _id: 'пиво', price: 50 }, 4));
  416. //Авторизація
  417. document.getElementById("Autorization").onclick = () => location.href = `#/login`;
  418. const actionFullLogin = (login, password) =>
  419. async (dispatch) => {
  420. const token = await dispatch(actionLogin(login, password))
  421. console.log(token)
  422. if(typeof(token) === "string"){
  423. dispatch(actionAuthLogin(token))
  424. mainCard.innerHTML = `Hello,${login}.`
  425. afterLoginization()
  426. }
  427. else {
  428. mainCard.innerHTML = `<h1>Bad attempt</h1>
  429. <button id="reloadAuth">Try again</button>`
  430. document.getElementById("reloadAuth").onclick = function(){
  431. location.reload()
  432. }
  433. }
  434. //проверьте что token - строка и отдайте его в actionAuthLogin
  435. }
  436. //Регістрація
  437. document.getElementById("Registration").onclick = () => location.href = `#/register`
  438. const actionFullRegistr = (login, password) =>
  439. async (dispatch) => {
  440. let userRegistr = await dispatch(actionRegistr(login,password))
  441. if(userRegistr){
  442. dispatch(actionFullLogin(login,password))
  443. }
  444. else {
  445. mainCard.innerHTML = `<h1>Bad attempt</h1>
  446. <button id="reloadReg">Try again</button>`
  447. document.getElementById("reloadReg").onclick = function(){
  448. location.reload()
  449. }
  450. }
  451. }
  452. //При логінізації
  453. function afterLoginization (){
  454. document.getElementById("Registration").hidden = true
  455. document.getElementById("Autorization").hidden = true
  456. document.getElementById("logOut").hidden = false
  457. document.getElementById("history").hidden = false
  458. logOut.onclick = ()=>{
  459. store.dispatch(actionAuthLogout())
  460. location.reload()
  461. }
  462. mainCard.innerHTML = `<h3>I hope you are doing great.Let's buy smth!!!</h3>`
  463. }
  464. //Історія
  465. document.getElementById("history").onclick = ()=> {location.href = `#/history`}
  466. store.subscribe(()=>{
  467. const [,route, _id] = location.hash.split('/')
  468. if(route !== "history"){
  469. return
  470. }
  471. let {status,payload,error} = store.getState().promise?.HistoryOrder || {}
  472. if(status === "PENDING"){
  473. mainCard.innerHTML = `<img src = 'https://i.pinimg.com/originals/71/3a/32/713a3272124cc57ba9e9fb7f59e9ab3b.gif'>`
  474. }
  475. if(status === "FULFILLED"){
  476. mainCard.innerHTML = ""
  477. if(payload && route === "history"){
  478. let i=1
  479. let j = 0
  480. // let arr = []
  481. // let [createdAt,total ] = payload
  482. //console.log(createdAt,total)
  483. for( let {_id,createdAt,total,orderGoods} of payload){
  484. let data = new Date(+createdAt).toString()
  485. let time = data.split(" ").slice(1,5).join(" ")
  486. // arr.push(+createdAt)
  487. // arr.sort((a,b)=>{
  488. // return b - a
  489. // })
  490. mainCard.innerHTML += `<div class = "history" style = "border: 1px solid purple">
  491. <h3>Order №${i} at ${time}</h3>
  492. <h3>Total: ${total}$</h3>
  493. </div>`
  494. i++
  495. }
  496. //console.log(arr)
  497. }
  498. }
  499. })
  500. //Створення заказу
  501. const actionNewOrder = () =>
  502. async (dispatch,getState) => {
  503. let {cart} = getState()
  504. const orderGoods = Object.entries(cart).map(([_id,{count}])=>({
  505. good : { _id},
  506. count,
  507. }))
  508. console.log(orderGoods)
  509. let result = await dispatch(newOrder(orderGoods))
  510. if(result?._id){
  511. dispatch(actionCartClear())
  512. }
  513. }
  514. //Корзина
  515. store.subscribe(()=>{
  516. document.getElementById("cartIcon").onclick = function inforOfCart(){
  517. location.href = `#/cart`
  518. mainCard.innerHTML = ``
  519. let storeCartInfo = store.getState().cart
  520. let h1 = document.createElement('h1')
  521. h1.innerText = 'Cart'
  522. let sum = 0
  523. for(let i=0;i<Object.keys(storeCartInfo).length;i++){
  524. let div = document.createElement('div')
  525. div.id = `${i}`
  526. div.className = "Cart"
  527. div.style = "border: 1px solid purple;"
  528. mainCard.append(div)
  529. let userOrder = document.getElementById(i)
  530. let nameOrder = Object.keys(storeCartInfo)[i]
  531. let name = storeCartInfo[nameOrder].good.name
  532. userOrder.innerHTML +=`<h3>${name}</h3>`
  533. for(const img of storeCartInfo[nameOrder].good.img){
  534. userOrder.innerHTML +=`<img src="${url + img.url}" alt="imageGood">`
  535. }
  536. userOrder.innerHTML +=`<h3>Cost: ${storeCartInfo[nameOrder].good.price}$</h3>`
  537. let buttonMinus = document.createElement("button")
  538. buttonMinus.innerText = "-"
  539. userOrder.append(buttonMinus)
  540. let input = document.createElement('input')
  541. input.type = "number"
  542. input.value = store.getState().cart[nameOrder].count
  543. userOrder.append(input)
  544. let buttonPlus = document.createElement("button")
  545. buttonPlus.innerText = "+"
  546. userOrder.append(buttonPlus)
  547. let buttonDel = document.createElement('button')
  548. buttonDel.innerText = "Delete"
  549. userOrder.append(buttonDel)
  550. buttonMinus.onclick = function(){
  551. input.value--
  552. store.dispatch(actionCartSub({
  553. _id:nameOrder,name:name,price:storeCartInfo[nameOrder].good.price,
  554. img:storeCartInfo[nameOrder].good.img}))
  555. inforOfCart()
  556. }
  557. buttonPlus.onclick = function(){
  558. input.value++
  559. store.dispatch(actionCartAdd(
  560. {_id:nameOrder,name:name,price:storeCartInfo[nameOrder].good.price,
  561. img:storeCartInfo[nameOrder].good.img},
  562. input.value))
  563. inforOfCart()
  564. }
  565. input.oninput = function(){
  566. store.dispatch(actionCartSet({_id:nameOrder,name:name,price:storeCartInfo[nameOrder].good.price},+input.value))
  567. inforOfCart()
  568. }
  569. buttonDel.onclick = function(){
  570. store.dispatch(actionCartDel({_id:nameOrder}))
  571. inforOfCart()
  572. }
  573. sum += storeCartInfo[nameOrder].good.price * storeCartInfo[nameOrder].count
  574. }
  575. let divResult = document.createElement("div")
  576. divResult.className = "cartResult"
  577. divResult.innerHTML += `Sum: ${sum} $`
  578. mainCard.prepend(h1,divResult)
  579. let btnCreatOrd = document.createElement("button")
  580. btnCreatOrd.id = "btnCreateOrd"
  581. btnCreatOrd.innerText = "Create Order"
  582. divResult.append(btnCreatOrd)
  583. btnCreatOrd.onclick = function (){
  584. store.dispatch(actionNewOrder())
  585. store.dispatch(actionCartClear())
  586. inforOfCart()
  587. }
  588. if(Object.entries(storeCartInfo).length === 0){
  589. mainCard.innerHTML += `<img src = "https://static.thenounproject.com/png/576143-200.png">`
  590. mainCard.innerHTML +=`<h3>The cart is clear</h3>`
  591. document.getElementById("btnCreateOrd").disabled = true
  592. }
  593. }
  594. })
  595. //LoginForm
  596. window.onhashchange = () => {
  597. const [,route, _id] = location.hash.split('/')
  598. const routes = {
  599. category() {
  600. store.dispatch(actionCatFindOne(_id))
  601. },
  602. good(){
  603. // тут был store.dispatch goodById
  604. store.dispatch(actionGoodFindOne(_id))
  605. console.log('good', _id)
  606. },
  607. login(){
  608. mainCard.innerHTML = `<form>
  609. <div class="auto">
  610. <h1>Autorization</h1>
  611. <input id="inputLogin" type="text" placeholder="login">
  612. </br>
  613. <input id="inputPassword" type="password" placeholder="password">
  614. </br>
  615. <button id="loginButton">Log in</button>
  616. </div>
  617. </form>`
  618. // let aut = new formLogPas(mainCard,true)
  619. document.getElementById("loginButton").onclick = function() {
  620. store.dispatch(actionFullLogin(inputLogin.value, inputPassword.value))
  621. }
  622. //нарисовать форму логина, которая по нажатию кнопки Login делает store.dispatch(actionFullLogin(login, password))
  623. },
  624. register(){
  625. mainCard.innerHTML = `<form>
  626. <div class="registration">
  627. <h1>Registration</h1>
  628. <input id="loginReg" type="text" placeholder="login">
  629. </br>
  630. <input id="passwordReg" type="password" placeholder="password">
  631. </br>
  632. <button id="regButton">Log in</button>
  633. </div>
  634. </form>`
  635. //let reg = new formLogPas(mainCard,true)
  636. document.getElementById("regButton").onclick =() => {
  637. store.dispatch(actionFullRegistr(loginReg.value, passwordReg.value))
  638. }
  639. //нарисовать форму регистрации, которая по нажатию кнопки Login делает store.dispatch(actionFullRegister(login, password))
  640. },
  641. history(){
  642. store.dispatch(HistoryOrder())
  643. },
  644. cart(){},
  645. }
  646. if (route in routes){
  647. routes[route]()
  648. }
  649. }
  650. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  651. window.onhashchange()
  652. //const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOnsiaWQiOiI2M2QyODUyNGRhODExZTMzNWJmY2ZlNTYiLCJsb2dpbiI6InZhczI0MyIsImFjbCI6WyI2M2QyODUyNGRhODExZTMzNWJmY2ZlNTYiLCJ1c2VyIl19LCJpYXQiOjE2NzQ3NDExNDF9.dVileTOVOJF1o6CUKgASIlTW7vdqDNLM3tycKXTIQHA"