Alyona Brytvina 2 anni fa
parent
commit
376a579819
3 ha cambiato i file con 202 aggiunte e 0 eliminazioni
  1. 24 0
      HW11/index.html
  2. 153 0
      HW11/main.js
  3. 25 0
      HW11/style.css

+ 24 - 0
HW11/index.html

@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html lang="en" xmlns="http://www.w3.org/1999/html">
+<head>
+    <meta charset="UTF-8">
+    <title>HW11</title>
+    <link rel="stylesheet" href="style.css">
+</head>
+<body>
+<div id='shop'>
+    <label> Выберите товар для покупки
+    <select id='goods'>
+    </select>
+    </label>
+    <label>Сколько вы хотите купить?
+    <input type='number' id='quantity' placeholder="введите число"/>
+    </label>
+    <label>У вас на счету гривен
+    <input type="number" id="fieldForMoney">
+    </label>
+    <button id='buy'>Купить</button>
+</div>
+<script src="main.js"></script>
+</body>
+</html>

+ 153 - 0
HW11/main.js

@@ -0,0 +1,153 @@
+function createStore(reducer) {
+    let state = reducer(undefined, {});
+    let cbs = [];
+
+    const getState = () => state;
+
+    const subscribe = cb => (cbs.push(cb),
+        () => cbs = cbs.filter(c => c !== cb));
+
+    const dispatch = action => {
+        const newState = reducer(state, action);
+        if (newState !== state) {
+            state = newState;
+            for (let cb of cbs) cb();
+        }
+    };
+
+    return {
+        getState,
+        dispatch,
+        subscribe
+    };
+}
+
+function reducer(state, {type, ШО, СКОКА}) {
+    if (!state) {
+        return {
+            пиво: {
+                count: 100,
+                priceForOne: 6,
+            },
+            чипсы: {
+                count: 100,
+                priceForOne: 14,
+            },
+            сиги: {
+                count: 100,
+                priceForOne: 25,
+            },
+            касса: {
+                count: 0,
+            },
+            деньги: {
+                count: 0
+            }
+
+        };
+    }
+
+    if (type === 'КУПИТЬ') {
+
+        for (let key in {...state}) {
+            if (key === ШО && (СКОКА) > {...state}[key].count) {
+                return state;
+            }
+        }
+
+        const total =  +СКОКА * state[ШО].priceForOne;
+        if(state.деньги.count >= total){
+            return {
+                ...state,
+                касса: {count: state.касса.count + total},
+                деньги: {count: state.деньги.count - total},
+                [ШО]: {...state[ШО], count: state[ШО].count - +СКОКА},
+            };
+        }
+    }
+
+    if(type === 'МоиДеньги'){
+
+        return {
+            ...state,
+            деньги: {count: +СКОКА}
+        }
+    }
+
+    return state;
+}
+
+const store = createStore(reducer);
+
+for (let key in store.getState()) {
+    let goods = document.getElementById('goods');
+    let option = document.createElement('option');
+    option.innerHTML = key;
+    goods.appendChild(option);
+}
+
+
+const купи = (ШО, СКОКА) => ({type: 'КУПИТЬ', ШО, СКОКА});
+const моиДеньги = (СКОКА) => ({type: 'МоиДеньги', СКОКА});
+
+let buy = document.getElementById('buy');
+
+buy.onclick = () => {
+    let goods = document.getElementById('goods');
+    let quantity = document.getElementById('quantity');
+    store.dispatch(купи(goods.value, quantity.value));
+
+};
+
+fieldForMoney.oninput = (event) => {
+
+    store.dispatch(моиДеньги(event.target.value));
+}
+
+const unsubscribe = store.subscribe(() => console.log(store.getState()));
+
+setTimeout(unsubscribe, 1000);
+
+store.subscribe( () => {
+    fieldForMoney.value = store.getState().деньги.count;
+})
+function createTable() {
+    const shop = document.getElementById('shop');
+
+    let table = document.createElement('table');
+    let tr = document.createElement('tr');
+    let tr1 = document.createElement('tr');
+    let tr2 = document.createElement('tr');
+
+    for (let key in store.getState()) {
+
+        let th = document.createElement('th');
+        th.innerText = key;
+
+        let td1 = document.createElement('td');
+        td1.innerText = store.getState()[key].count;
+
+        let td2 = document.createElement('td');
+        td2.innerText = store.getState()[key].priceForOne || '';
+
+        tr2.appendChild(td2);
+        tr1.appendChild(td1);
+        tr.appendChild(th);
+
+        store.subscribe(() => {
+            td1.innerText = store.getState()[key].count;
+        });
+
+        td1.innerText = store.getState()[key].count;
+
+    }
+    table.appendChild(tr);
+    table.appendChild(tr1);
+    table.appendChild(tr2);
+    shop.append(table);
+
+}
+
+createTable();
+
+setTimeout(() => store.dispatch({type: 'купить', ШО: 'пиво', СКОКА: 3}), 1000);

+ 25 - 0
HW11/style.css

@@ -0,0 +1,25 @@
+* {
+    box-sizing: border-box;
+}
+
+button{
+    width: 100%;
+    font-size: 2em;
+}
+input, button, select{
+    width: 100%;
+    font-size: 2em;
+    margin: 10px;
+}
+
+table {
+    border: 1px;
+    border-collapse: collapse;
+    width: 300px;
+    height: 150px;
+}
+
+td,th {
+    border: 1px solid black;
+    text-align: center;
+}