123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- const getGQL = url =>
- async (query, variables) => {
- try {
- let obj = await fetch(url, {
- method: 'POST',
- headers: {
- Authorization: localStorage.authToken ? 'Bearer ' + localStorage.authToken : null,
- "Content-Type": "application/json"
- },
- body: JSON.stringify({ query, variables })
- })
- let a = await obj.json()
- for (const key in a) {
- for (const deepKey in a[key]) {
- return a[key][deepKey]
- }
- }
- }
- catch (error) {
- console.log('Что-то не так, Бро ' + error);
- }
- }
- const gql = getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql');
- function logIn() {
- const loginBox = document.createElement('div')
- loginBox.setAttribute('id', 'loginBox')
- const inputNick = document.createElement('input')
- const h2 = document.createElement('h2')
- h2.innerText = 'Log In'
- const inputPassword = document.createElement('input')
- const btnLogIn = document.createElement('button')
- const btnRegister = document.createElement('button')
- const btnX = document.createElement('button')
- btnX.innerText = 'X'
- btnX.classList.add('close')
- btnLogIn.innerText = 'Log In'
- btnRegister.innerText = 'Register'
- overlay.style.display = 'block'
- btnLogIn.onclick = async () => {
- let jwt = await gql(`
- query NameForMe1($login:String, $password:String){
- login(login:$login, password:$password)
- }
- `, {
- login: inputNick.value, password: inputPassword.value
- })
- console.log(jwt);
- if (jwt) {
- localStorage.setItem('authToken', jwt)
- overlay.style.display = 'none'
- loginBox.remove()
- const h1 = document.createElement('h2')
- h1.innerText = `Welcome ${inputNick.value}`
- title.append(h1)
- }
- }
- btnRegister.onclick = () => {
- loginBox.remove()
- registerF()
- }
- btnX.onclick = () => {
- loginBox.remove()
- overlay.style.display = 'none'
- }
- inputNick.type = 'text'
- inputPassword.type = 'password'
- loginBox.append(h2)
- loginBox.append(inputNick)
- loginBox.append(btnX)
- loginBox.append(inputPassword)
- loginBox.append(btnLogIn)
- loginBox.append(btnRegister)
- body.append(loginBox)
- }
- function registerF() {
- const loginBox = document.createElement('div')
- loginBox.setAttribute('id', 'loginBox')
- const inputNick = document.createElement('input')
- const h2 = document.createElement('h2')
- h2.innerText = 'Register'
- const inputPassword = document.createElement('input')
- const btnLogIn = document.createElement('button')
- const btnRegister = document.createElement('button')
- const btnX = document.createElement('button')
- btnX.innerText = 'X'
- btnX.classList.add('close')
- btnLogIn.innerText = 'Enter login'
- btnRegister.innerText = 'Register'
- overlay.style.display = 'block'
- btnRegister.onclick = async () => {
- let jwt = await gql(`
- mutation reg($login:String, $password:String){
- UserUpsert(user:{
- login:$login,
- password:$password,
- nick:$login}){
- _id login
- }
- }
- `, {
- login: inputNick.value, password: JinputPassword.value
- })
- console.log(jwt);
- }
- btnLogIn.onclick = () => {
- loginBox.remove()
- logIn()
- }
- btnX.onclick = () => {
- loginBox.remove()
- overlay.style.display = 'none'
- }
- inputNick.type = 'text'
- inputPassword.type = 'password'
- loginBox.append(h2)
- loginBox.append(inputNick)
- loginBox.append(inputPassword)
- loginBox.append(btnRegister)
- loginBox.append(btnLogIn)
- loginBox.append(btnX)
- body.append(loginBox)
- }
- userLogin.onclick = () => {
- overlay.style.display = 'block'
- logIn()
- }
- userRegister.onclick = () => {
- overlay.style.display = 'block'
- registerF()
- }
- overlay.onclick = () => {
- overlay.style.display = 'none'
- loginBox.remove()
- }
- menuBtn.onclick = async () => {
- let cat = await gql(`
- query Categoria {
- CategoryFind(query:"[{}]"){
- name _id goods{
- name images {
- url
- }
- }
- }
- }
- `, { login: 'Kabina', password: '12345' })
- cat.map(c => {
- if (c) {
- let li = document.createElement('li')
- let btnLink = document.createElement('button')
- btnLink.classList.add('menuBtn')
- btnLink.setAttribute('id', c._id)
- btnLink.innerText = `${c.name} к-во:${c.goods ? c.goods.length : '0'}`
- btnLink.onclick = () => {
- itemBox.firstChild ? itemBox.firstChild.remove() : null
- let items = document.createElement('items')
- if (c.goods !== null) {
- c.goods.map(i => {
- let item = document.createElement('item')
- let h3 = document.createElement('h3')
- h3.innerText = i.name
- item.append(h3)
- items.append(item)
- })
- itemBox.append(items)
- }
- }
- li.append(btnLink)
- menu.append(li)
- }
- })
- }
|