Browse Source

HW11 done

Vadym Hlushko 2 years ago
parent
commit
417ee876fc
7 changed files with 2 additions and 278 deletions
  1. BIN
      hw11/chips.png
  2. BIN
      hw11/cigi.png
  3. 0 12
      hw11/index.html
  4. BIN
      hw11/kassa.png
  5. 0 191
      hw11/redux.js
  6. 2 1
      hw12/index.html
  7. 0 74
      hw12/main.js

BIN
hw11/chips.png


BIN
hw11/cigi.png


+ 0 - 12
hw11/index.html

@@ -1,12 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
-    <meta charset="UTF-8">
-    <meta name="viewport" content="width=device-width, initial-scale=1.0">
-    <title>Document</title>
-</head>
-<body>
-    <div id="content"></div>
-    <script src="redux.js"></script>
-</body>
-</html>

BIN
hw11/kassa.png


+ 0 - 191
hw11/redux.js

@@ -1,191 +0,0 @@
-function createStore(reducer){
-    let state = reducer(undefined, {})
-    let cbs   = []
-    return {
-        dispatch(action){
-            const newState = reducer(state, action)
-            if (newState !== state){
-                state = newState
-                for (let cb of cbs) 
-                    cb()
-            }
-        },
-        getState(){
-            return state
-        },
-        subscribe(cb){
-            cbs.push(cb)
-            return () => cbs = cbs.filter(c => c !== cb)
-        }
-    }
-}
-
-function createDom (state) {
-    let content = document.getElementById('content')
-    content.innerHTML = ''
-    let spanBeerAmount = document.createElement('span')
-    spanBeerAmount = `Цена - ${state.пиво.БАБЛО} грн | В наличии:${state.пиво.СКОКА} шт.`
-    let spanImgBeer = document.createElement('span')
-    let imgBeer = document.createElement('img')
-    imgBeer.src = "https://www.meme-arsenal.com/memes/a47737a30a10732b77d522f44f43ea3d.jpg"
-    spanImgBeer.append(imgBeer)
-    imgBeer.style.width = '200px'
-    content.append(spanImgBeer)
-    content.append(spanBeerAmount)
-    
-    let spanChipsAmount = document.createElement('span')
-    spanChipsAmount = `Цена = ${state.чипсы.БАБЛО} грн | В наличии: ${state.чипсы.СКОКА} пачек`
-    let spanImgChips = document.createElement('span')
-    spanImgChips.style.marginLeft = '50px'
-    let imgChips = document.createElement('img')
-    imgChips.src = "chips.png"
-    imgChips.style.width = '150px'
-    imgChips.style.marginRight = '30px'
-    spanImgChips.append(imgChips)
-    content.append(spanImgChips)
-    content.append(spanChipsAmount)
-    
-    let spanCigarettesAmount = document.createElement('span')
-    spanCigarettesAmount = `Цена: ${state.сиги.БАБЛО} грн | В наличии: ${state.сиги.СКОКА} пачек`
-    let spanImgCigarettes = document.createElement('span')
-    let imgCigarettes = document.createElement('img')
-    imgCigarettes.src = 'cigi.png'
-    imgCigarettes.style.width = '200px'
-    spanImgCigarettes.style.marginLeft = '10px'
-    spanImgCigarettes.append(imgCigarettes)
-    content.append(spanImgCigarettes)
-    content.append(spanCigarettesAmount)
-
-    
-    let cashbox = document.createElement('span')
-    let cashboxImg = document.createElement('img')
-    cashboxImg.src = "kassa.png"
-    cashboxImg.style.width = '200px'
-    cashboxImg.style.position = 'relative'
-    cashboxImg.style.top = '60px'
-    cashboxImg.style.left = '50px'
-    cashbox.style.position = 'relative' 
-    cashbox.style.top = '5px'
-    cashbox.style.left = '100px'
-    cashbox.innerHTML = `В кассе: ${state.касса} грн. `
-    content.append(cashboxImg)
-    content.append(cashbox)
-    
-    let btnCashbox = document.createElement('button')
-    btnCashbox.innerHTML = "Посмотреть сколько в кассе"
-    btnCashbox.style.marginLeft = '80px'
-    content.append(btnCashbox)
-
-    let btnHidden = document.createElement('button')
-    btnHidden.hidden = true
-    btnHidden.innerHTML = 'Скрыть'
-    btnHidden.style.position = 'relative'
-    btnHidden.style.top = '40px'
-    btnHidden.style.left = '20px'
-    content.append(btnHidden)
-    
-    cashbox.hidden = true
-    btnCashbox.hidden = false
-    
-    btnCashbox.onclick = () => {
-        alert ("Только для продавщицы!!!")
-        let password = prompt('Введите пароль для продавщицы')
-        if (password == 'qwerty'){
-        cashbox.hidden = false
-        btnCashbox.hidden = true
-        btnHidden.hidden = false}
-        else {
-            alert ("Куда лезешь?")
-        }
-    }
-
-    btnHidden.onclick = () => {
-        cashbox.hidden = true
-        btnCashbox.hidden = false
-        btnHidden.hidden = true
-    }
-    
-    let selectProducts = document.createElement('select')
-    content.append(selectProducts)
-    selectProducts.style.marginLeft = '300px'
-    let option = document.createElement('option')
-    option.innerHTML = 'Выберите продукт'
-    selectProducts.append(option) 
-    let inputAmount = document.createElement('input')
-    inputAmount.type = 'number'
-    content.append(inputAmount)
-    inputAmount.style.marginLeft = '20px'
-    inputAmount.placeholder = 'Количество товара'
-
-    let beer = document.createElement('option')
-    beer.innerHTML = 'пиво'
-    selectProducts.append(beer)
-
-    let chips = document.createElement('option')
-    chips.innerHTML = 'чипсы'
-    selectProducts.append(chips)
-
-    let cigarettes = document.createElement('option')
-    cigarettes.innerHTML = 'сиги'
-    selectProducts.append(cigarettes)
-    
-    let btn = document.createElement('button')
-    content.append(btn)
-    btn.innerHTML = 'Купить'
-    btn.style.marginLeft = '20px'
-    
-    btn.onclick = () => {
-        if(state[selectProducts.value].СКОКА >= inputAmount.value){
-        let amount = inputAmount.value
-        let selectNum = selectProducts.value
-        
-        store.dispatch(купиШото(amount,selectNum))
-        }
-        else {
-            alert('В наличие столько нету!')
-        }
-    }
-    return state
-}
-function reducer(state, {type, ШО, СКОКА,БАБЛО}){
-    if (!state){
-        return {
-            пиво: {СКОКА:100,БАБЛО:60},
-            чипсы: {СКОКА:100,БАБЛО:30},
-            сиги: {СКОКА:100 , БАБЛО:50},
-            касса: 0
-        }
-    }
-    if (type === 'КУПИТЬ'){
-        return {
-            ...state,
-            [ШО]: {...state[ШО], СКОКА: state[ШО].СКОКА - СКОКА },
-            касса: state[ШО].БАБЛО * СКОКА + state.касса
-        }
-    }
-
-    return state
-}
-
-const store = createStore(reducer)
-const unsubscribe2 = store.subscribe(() => console.log('подписчик 2', store.getState()))
-const shop = store.subscribe(() => createDom(store.getState()))
-const купиПиво = СКОКА => ({type: 'КУПИТЬ', ШО: "пиво", СКОКА})
-const купиЧипсы = СКОКА => ({type: 'КУПИТЬ', ШО: "чипсы", СКОКА})
-const купиСиги = СКОКА => ({type: 'КУПИТЬ', ШО: "сиги", СКОКА})
-const купиШото = (СКОКА,ШО) => ({type: 'КУПИТЬ', ШО, СКОКА})
-store.dispatch(купиПиво(0))
-store.dispatch(купиЧипсы(0))
-store.dispatch(купиСиги(0))
-
-
-
-
-
-
-
-  
-
-
-
-

+ 2 - 1
hw12/index.html

@@ -6,6 +6,7 @@
     <title>Document</title>
 </head>
 <body>
-    <script src="main.js"></script>
+    <div id="content"></div>
+    <script src="redux.js"></script>
 </body>
 </html>

+ 0 - 74
hw12/main.js

@@ -1,74 +0,0 @@
-//fetch basic + improved
-fetch('https://swapi.dev/api/people/1/')
-             .then(res => res.json())
-             .then(luke => swapiTable(document.body, luke))
-
- function swapiTable(parent, data) {
-     let table = document.createElement("table")
-     table.border = 1
-     table.style.borderColor = 'purple'
-     parent.append(table)
-     for (let key in data) {
-       let tr = document.createElement("tr")
-       table.append(tr)
-
-       let th = document.createElement("th")
-       tr.append(th)
-       th.append(key)
-
-       let td = document.createElement("td")
-       tr.append(td) 
-       let btn = function(parent, link) { 
-         button = document.createElement("button")
-         button.append(link)
-         button.style.background = "#8B008B"
-         button.style.color = 'white'
-         parent.append(button)
-         button.onclick = () => {
-             fetch(link)
-             .then(res => res.json())
-             .then(luke => swapiTable(document.body, luke))
-           }
-         }
-       if (data[key] instanceof Array) {
-         for (let arr of data[key]) {
-           if (arr.startsWith("https://swapi.dev/api/")) {
-               btn(td, arr)
-           } else td.append(data[key])
-             }
-       }else { if (data[key].startsWith("https://swapi.dev/api/")) {
-               btn(td, data[key])
-             }else td.append(data[key])
-                 }  
-             }       
-}
-//myfetch
-function myfetch(url){
-    return new Promise(function (resolve, reject){
-        const xhr = new XMLHttpRequest()
-        xhr.open('GET', url)
-        xhr.onload = () => {
-            if (xhr.status != 200) { 
-                reject(`Ошибка: ${xhr.statusText}`)
-                return
-            } 
-            resolve(JSON.parse(xhr.response))
-        }
-        xhr.onerror = function() {
-            reject("Error")
-        }
-        xhr.send()
-    })
-}
-
-myfetch('https://swapi.dev/api/people/1/')
-  .then(luke => console.log(luke))
-
-  //race
-  const delay = new Promise((resolve, reject) => {
-    setTimeout(resolve, Math.random() * 100, 'delay');
-  });
-
-  Promise.race([delay , myfetch('https://swapi.dev/api/people/1/')]).then((value) => {
-      console.log(value)
-  })