index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. function reducer(state, {type, ШО, СКОКА}){
  2. if (!state){
  3. return {
  4. пиво: {number : 300, price : 40},
  5. чипсы: {number : 50, price : 30},
  6. сиги: {number : 100, price : 70},
  7. кола: {number: 80, price: 20},
  8. шоколад: {number: 30, price: 25},
  9. бабло: {cashbox : 0, mycash : 2000}
  10. }
  11. }
  12. if (type === 'КУПИТЬ' && (state[ШО].number) >= 0 && state.бабло.mycash - state[ШО].price * СКОКА >= 0) {
  13. return {
  14. ...state,
  15. [ШО]: {...state[ШО], number: state[ШО].number - СКОКА},
  16. бабло: {
  17. mycash: state.бабло.mycash - state[ШО].price * СКОКА,
  18. cashbox: state.бабло.cashbox + state[ШО].price * СКОКА
  19. }
  20. }
  21. }
  22. return state
  23. }
  24. function createStore(reducer){
  25. let state = reducer(undefined, {})
  26. let cbs = []
  27. const getState = () => state
  28. const subscribe = cb => (cbs.push(cb),
  29. () => cbs = cbs.filter(c => c !== cb))
  30. const dispatch = action => {
  31. const newState = reducer(state, action)
  32. if (newState !== state){
  33. state = newState
  34. for (let cb of cbs) cb()
  35. }
  36. }
  37. return {
  38. getState,
  39. dispatch,
  40. subscribe
  41. }
  42. }
  43. const store = createStore(reducer)
  44. console.log(store.getState())
  45. let productN = document.getElementById("product");
  46. let numberN = document.getElementById("number");
  47. let buttonN = document.getElementById("button");
  48. let pivoN = document.getElementById("pivo").innerHTML= store.getState().пиво.number + ' шт'
  49. let chipsN = document.getElementById("chips").innerHTML= store.getState().чипсы.number + ' шт'
  50. let sigiN = document.getElementById("sigi").innerHTML= store.getState().сиги.number + ' шт'
  51. let colaN = document.getElementById("cola").innerHTML= store.getState().кола.number + ' шт'
  52. let chokN = document.getElementById("chok").innerHTML= store.getState().шоколад.number + ' шт'
  53. let pivoM = document.getElementById("pivoK").innerHTML= store.getState().пиво.price + ' грн'
  54. let chipsM = document.getElementById("chipsK").innerHTML= store.getState().чипсы.price + ' грн'
  55. let sigiM = document.getElementById("sigiK").innerHTML= store.getState().сиги.price + ' грн'
  56. let colaM = document.getElementById("colaK").innerHTML= store.getState().кола.price + ' грн'
  57. let chokM = document.getElementById("chokK").innerHTML= store.getState().шоколад.price + ' грн'
  58. let kassaN = document.getElementById("kassa").innerHTML= store.getState().бабло.cashbox + ' грн'
  59. let moneyN = document.getElementById("money").innerHTML= store.getState().бабло.mycash + ' грн'
  60. buttonN.onclick = () => {
  61. store.dispatch({type: 'КУПИТЬ', ШО: productN.value, СКОКА: numberN.value})
  62. let pivoN = document.getElementById("pivo").innerHTML= store.getState().пиво.number + ' шт'
  63. let chipsN = document.getElementById("chips").innerHTML= store.getState().чипсы.number + ' шт'
  64. let sigiN = document.getElementById("sigi").innerHTML= store.getState().сиги.number + ' шт'
  65. let colaN = document.getElementById("cola").innerHTML= store.getState().кола.number + ' шт'
  66. let chokN = document.getElementById("chok").innerHTML= store.getState().шоколад.number + ' шт'
  67. let kassaN = document.getElementById("kassa").innerHTML= store.getState().бабло.cashbox + ' грн'
  68. let moneyN = document.getElementById("money").innerHTML= store.getState().бабло.mycash + ' грн'
  69. }