123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- function reducer(state, {type, ШО, СКОКА}){
-
- if (!state){
- return {
- пиво: {number : 300, price : 40},
- чипсы: {number : 50, price : 30},
- сиги: {number : 100, price : 70},
- кола: {number: 80, price: 20},
- шоколад: {number: 30, price: 25},
- бабло: {cashbox : 0, mycash : 2000}
- }
- }
- if (type === 'КУПИТЬ' && (state[ШО].number) >= 0 && state.бабло.mycash - state[ШО].price * СКОКА >= 0) {
- return {
- ...state,
- [ШО]: {...state[ШО], number: state[ШО].number - СКОКА},
- бабло: {
- mycash: state.бабло.mycash - state[ШО].price * СКОКА,
- cashbox: state.бабло.cashbox + state[ШО].price * СКОКА
- }
- }
- }
- 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)
- console.log(store.getState())
- let productN = document.getElementById("product");
- let numberN = document.getElementById("number");
- let buttonN = document.getElementById("button");
- let pivoN = document.getElementById("pivo").innerHTML= store.getState().пиво.number + ' шт'
- let chipsN = document.getElementById("chips").innerHTML= store.getState().чипсы.number + ' шт'
- let sigiN = document.getElementById("sigi").innerHTML= store.getState().сиги.number + ' шт'
- let colaN = document.getElementById("cola").innerHTML= store.getState().кола.number + ' шт'
- let chokN = document.getElementById("chok").innerHTML= store.getState().шоколад.number + ' шт'
- let pivoM = document.getElementById("pivoK").innerHTML= store.getState().пиво.price + ' грн'
- let chipsM = document.getElementById("chipsK").innerHTML= store.getState().чипсы.price + ' грн'
- let sigiM = document.getElementById("sigiK").innerHTML= store.getState().сиги.price + ' грн'
- let colaM = document.getElementById("colaK").innerHTML= store.getState().кола.price + ' грн'
- let chokM = document.getElementById("chokK").innerHTML= store.getState().шоколад.price + ' грн'
- let kassaN = document.getElementById("kassa").innerHTML= store.getState().бабло.cashbox + ' грн'
- let moneyN = document.getElementById("money").innerHTML= store.getState().бабло.mycash + ' грн'
- buttonN.onclick = () => {
- store.dispatch({type: 'КУПИТЬ', ШО: productN.value, СКОКА: numberN.value})
- let pivoN = document.getElementById("pivo").innerHTML= store.getState().пиво.number + ' шт'
- let chipsN = document.getElementById("chips").innerHTML= store.getState().чипсы.number + ' шт'
- let sigiN = document.getElementById("sigi").innerHTML= store.getState().сиги.number + ' шт'
- let colaN = document.getElementById("cola").innerHTML= store.getState().кола.number + ' шт'
- let chokN = document.getElementById("chok").innerHTML= store.getState().шоколад.number + ' шт'
- let kassaN = document.getElementById("kassa").innerHTML= store.getState().бабло.cashbox + ' грн'
- let moneyN = document.getElementById("money").innerHTML= store.getState().бабло.mycash + ' грн'
- }
|