123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- function reducer(state, {
- type,
- ШО,
- СКОКА,
- ДЕНЕГ
- }) {
- if (!state) {
- return {
- пиво: {
- count: 100,
- price: 20
- },
- чипсы: {
- count: 60,
- price: 10
- },
- сиги: {
- count: 80,
- price: 40
- },
- касса: 0,
- ДЕНЕГ: 80
- }
- }
- if (type === 'КУПИТЬ') {
- if (state[ШО].count < СКОКА) {
- alert('NETU!')
- return state;
- } else if (state.ДЕНЕГ == 0) {
- alert('SHO BABOK NET?')
- return state;
- } else {
- console.log(СКОКА);
- if (state.ДЕНЕГ !== 0 && typeof state[ШО] === 'object' && СКОКА) {
- if (state[ШО].count < СКОКА) {
- alert('NETU!')
- return state;
- } else
- return {
- ...state,
- ДЕНЕГ: state.ДЕНЕГ - state[ШО].price,
- касса: state.касса + state[ШО].price,
- [ШО]: {
- ...state[ШО],
- count: state[ШО].count - СКОКА
- }
- }
- }
- return {
- ...state,
- [ШО]: state[ШО] - СКОКА,
- }
- }
- }
- return state
- }
- 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
- }
- }
- const store = createStore(reducer);
- let select = document.querySelector('#goods');
- function createSelectItems(selectId, store) {
- for (let key of Object.keys(store.getState())) {
- if (key === 'касса') {
- return;
- } else {
- let option = document.createElement('option');
- option.value = key;
- option.innerText = key;
- select.append(option);
- }
- }
- }
- createSelectItems('goods', store);
- btn.addEventListener('click', () => {
- store.dispatch({
- type: 'КУПИТЬ',
- 'ШО': select.value,
- 'СКОКА': input.value
- })
- });
- function drawTable(root, store) {
- root.innerHTML = ''
- for (let [item, pcs] of Object.entries(store.getState())) {
- let tr = document.createElement('tr');
- let td = document.createElement('td');
- let th = document.createElement('th');
- if (typeof pcs === 'object') {
- let th = document.createElement('th');
- let tr = document.createElement('td');
- th.innerText = item;
- tr.append(th);
- for (let [el, price] of Object.entries(pcs)) {
- let td = document.createElement('td');
- let td1 = document.createElement('td');
- td.innerText = el;
- td1.innerText = price;
- tr.append(td, td1);
- root.append(th, tr);
- }
- } else {
- th.innerText = item;
- td.innerText = pcs;
- tr.append(th, td);
- root.append(tr);
- }
- }
- }
- drawTable(table, store);
- store.subscribe(() => drawTable(table, store))
|