main.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. const getGQL = url =>
  2. async (query, variables) => {
  3. try {
  4. let obj = await fetch(url, {
  5. method: 'POST',
  6. headers: {
  7. Authorization: localStorage.authToken ? 'Bearer ' + localStorage.authToken : null,
  8. "Content-Type": "application/json"
  9. },
  10. body: JSON.stringify({ query, variables })
  11. })
  12. let a = await obj.json()
  13. for (const key in a) {
  14. for (const deepKey in a[key]) {
  15. return a[key][deepKey]
  16. }
  17. }
  18. }
  19. catch (error) {
  20. console.log('Что-то не так, Бро ' + error);
  21. }
  22. }
  23. const gql = getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql');
  24. function logIn() {
  25. const loginBox = document.createElement('div')
  26. loginBox.setAttribute('id', 'loginBox')
  27. const inputNick = document.createElement('input')
  28. const h2 = document.createElement('h2')
  29. h2.innerText = 'Log In'
  30. const inputPassword = document.createElement('input')
  31. const btnLogIn = document.createElement('button')
  32. const btnRegister = document.createElement('button')
  33. const btnX = document.createElement('button')
  34. btnX.innerText = 'X'
  35. btnX.classList.add('close')
  36. btnLogIn.innerText = 'Log In'
  37. btnRegister.innerText = 'Register'
  38. overlay.style.display = 'block'
  39. btnLogIn.onclick = async () => {
  40. let jwt = await gql(`
  41. query NameForMe1($login:String, $password:String){
  42. login(login:$login, password:$password)
  43. }
  44. `, {
  45. login: inputNick.value, password: inputPassword.value
  46. })
  47. console.log(jwt);
  48. if (jwt) {
  49. localStorage.setItem('authToken', jwt)
  50. overlay.style.display = 'none'
  51. loginBox.remove()
  52. const h1 = document.createElement('h2')
  53. h1.innerText = `Welcome ${inputNick.value}`
  54. title.append(h1)
  55. }
  56. }
  57. btnRegister.onclick = () => {
  58. loginBox.remove()
  59. registerF()
  60. }
  61. btnX.onclick = () => {
  62. loginBox.remove()
  63. overlay.style.display = 'none'
  64. }
  65. inputNick.type = 'text'
  66. inputPassword.type = 'password'
  67. loginBox.append(h2)
  68. loginBox.append(inputNick)
  69. loginBox.append(btnX)
  70. loginBox.append(inputPassword)
  71. loginBox.append(btnLogIn)
  72. loginBox.append(btnRegister)
  73. body.append(loginBox)
  74. }
  75. function registerF() {
  76. const loginBox = document.createElement('div')
  77. loginBox.setAttribute('id', 'loginBox')
  78. const inputNick = document.createElement('input')
  79. const h2 = document.createElement('h2')
  80. h2.innerText = 'Register'
  81. const inputPassword = document.createElement('input')
  82. const btnLogIn = document.createElement('button')
  83. const btnRegister = document.createElement('button')
  84. const btnX = document.createElement('button')
  85. btnX.innerText = 'X'
  86. btnX.classList.add('close')
  87. btnLogIn.innerText = 'Enter login'
  88. btnRegister.innerText = 'Register'
  89. overlay.style.display = 'block'
  90. btnRegister.onclick = async () => {
  91. let jwt = await gql(`
  92. mutation reg($login:String, $password:String){
  93. UserUpsert(user:{
  94. login:$login,
  95. password:$password,
  96. nick:$login}){
  97. _id login
  98. }
  99. }
  100. `, {
  101. login: inputNick.value, password: JinputPassword.value
  102. })
  103. console.log(jwt);
  104. }
  105. btnLogIn.onclick = () => {
  106. loginBox.remove()
  107. logIn()
  108. }
  109. btnX.onclick = () => {
  110. loginBox.remove()
  111. overlay.style.display = 'none'
  112. }
  113. inputNick.type = 'text'
  114. inputPassword.type = 'password'
  115. loginBox.append(h2)
  116. loginBox.append(inputNick)
  117. loginBox.append(inputPassword)
  118. loginBox.append(btnRegister)
  119. loginBox.append(btnLogIn)
  120. loginBox.append(btnX)
  121. body.append(loginBox)
  122. }
  123. userLogin.onclick = () => {
  124. overlay.style.display = 'block'
  125. logIn()
  126. }
  127. userRegister.onclick = () => {
  128. overlay.style.display = 'block'
  129. registerF()
  130. }
  131. overlay.onclick = () => {
  132. overlay.style.display = 'none'
  133. loginBox.remove()
  134. }
  135. menuBtn.onclick = async () => {
  136. let cat = await gql(`
  137. query Categoria {
  138. CategoryFind(query:"[{}]"){
  139. name _id goods{
  140. name images {
  141. url
  142. }
  143. }
  144. }
  145. }
  146. `, { login: 'Kabina', password: '12345' })
  147. cat.map(c => {
  148. if (c) {
  149. let li = document.createElement('li')
  150. let btnLink = document.createElement('button')
  151. btnLink.classList.add('menuBtn')
  152. btnLink.setAttribute('id', c._id)
  153. btnLink.innerText = `${c.name} к-во:${c.goods ? c.goods.length : '0'}`
  154. btnLink.onclick = () => {
  155. itemBox.firstChild ? itemBox.firstChild.remove() : null
  156. let items = document.createElement('items')
  157. if (c.goods !== null) {
  158. c.goods.map(i => {
  159. let item = document.createElement('item')
  160. let h3 = document.createElement('h3')
  161. h3.innerText = i.name
  162. item.append(h3)
  163. items.append(item)
  164. })
  165. itemBox.append(items)
  166. }
  167. }
  168. li.append(btnLink)
  169. menu.append(li)
  170. }
  171. })
  172. }