redux.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. function createStore(reducer){
  2. let state = reducer(undefined, {})
  3. let cbs = []
  4. return {
  5. dispatch(action){
  6. const newState = reducer(state, action)
  7. if (newState !== state){
  8. state = newState
  9. for (let cb of cbs)
  10. cb()
  11. }
  12. },
  13. getState(){
  14. return state
  15. },
  16. subscribe(cb){
  17. cbs.push(cb)
  18. return () => cbs = cbs.filter(c => c !== cb)
  19. }
  20. }
  21. }
  22. function createDom (state) {
  23. let content = document.getElementById('content')
  24. content.innerHTML = ''
  25. let spanBeerAmount = document.createElement('span')
  26. spanBeerAmount = `Цена - 60 грн | В наличии:${state.пиво.СКОКА} шт.`
  27. let spanImgBeer = document.createElement('span')
  28. let imgBeer = document.createElement('img')
  29. imgBeer.src = "https://www.meme-arsenal.com/memes/a47737a30a10732b77d522f44f43ea3d.jpg"
  30. spanImgBeer.append(imgBeer)
  31. imgBeer.style.width = '200px'
  32. content.append(spanImgBeer)
  33. content.append(spanBeerAmount)
  34. let spanChipsAmount = document.createElement('span')
  35. spanChipsAmount = `Цена = 30 грн | В наличии: ${state.чипсы.СКОКА} пачек`
  36. let spanImgChips = document.createElement('span')
  37. spanImgChips.style.marginLeft = '50px'
  38. let imgChips = document.createElement('img')
  39. imgChips.src = "chips.png"
  40. imgChips.style.width = '150px'
  41. imgChips.style.marginRight = '30px'
  42. spanImgChips.append(imgChips)
  43. content.append(spanImgChips)
  44. content.append(spanChipsAmount)
  45. let spanCigarettesAmount = document.createElement('span')
  46. spanCigarettesAmount = `Цена:50 грн | В наличии: ${state.сиги.СКОКА} пачек`
  47. let spanImgCigarettes = document.createElement('span')
  48. let imgCigarettes = document.createElement('img')
  49. imgCigarettes.src = 'cigi.png'
  50. imgCigarettes.style.width = '200px'
  51. spanImgCigarettes.style.marginLeft = '10px'
  52. spanImgCigarettes.append(imgCigarettes)
  53. content.append(spanImgCigarettes)
  54. content.append(spanCigarettesAmount)
  55. let cashbox = document.createElement('span')
  56. let cashboxImg = document.createElement('img')
  57. cashboxImg.src = "kassa.png"
  58. cashboxImg.style.width = '200px'
  59. cashboxImg.style.position = 'relative'
  60. cashboxImg.style.top = '60px'
  61. cashboxImg.style.left = '50px'
  62. cashbox.style.position = 'relative'
  63. cashbox.style.top = '5px'
  64. cashbox.style.left = '100px'
  65. cashbox.innerHTML = `В кассе: ${state.касса} грн. `
  66. content.append(cashboxImg)
  67. content.append(cashbox)
  68. let selectProducts = document.createElement('select')
  69. content.append(selectProducts)
  70. selectProducts.style.marginLeft = '300px'
  71. let option = document.createElement('option')
  72. option.innerHTML = 'Выберите продукт'
  73. selectProducts.append(option)
  74. let inputAmount = document.createElement('input')
  75. inputAmount.type = 'number'
  76. content.append(inputAmount)
  77. inputAmount.style.marginLeft = '20px'
  78. inputAmount.placeholder = 'Количество товара'
  79. let beer = document.createElement('option')
  80. beer.innerHTML = 'пиво'
  81. selectProducts.append(beer)
  82. let chips = document.createElement('option')
  83. chips.innerHTML = 'чипсы'
  84. selectProducts.append(chips)
  85. let cigarettes = document.createElement('option')
  86. cigarettes.innerHTML = 'сиги'
  87. selectProducts.append(cigarettes)
  88. let btn = document.createElement('button')
  89. content.append(btn)
  90. btn.innerHTML = 'Купить'
  91. btn.style.marginLeft = '20px'
  92. btn.onclick = () => {
  93. if(state[selectProducts.value].СКОКА >= inputAmount.value){
  94. let amount = inputAmount.value
  95. let selectNum = selectProducts.value
  96. store.dispatch(купиШото(amount,selectNum))
  97. }
  98. else {
  99. alert('В наличие столько нету!')
  100. }
  101. }
  102. return state
  103. }
  104. function reducer(state, {type, ШО, СКОКА,БАБЛО}){
  105. if (!state){
  106. return {
  107. пиво: {СКОКА:100,БАБЛО:60},
  108. чипсы: {СКОКА:100,БАБЛО:30},
  109. сиги: {СКОКА:100 , БАБЛО:50},
  110. касса: 0
  111. }
  112. }
  113. if (type === 'КУПИТЬ'){
  114. return {
  115. ...state,
  116. [ШО]: {...state[ШО], СКОКА: state[ШО].СКОКА - СКОКА },
  117. касса: state[ШО].БАБЛО * СКОКА + state.касса
  118. }
  119. }
  120. return state
  121. }
  122. const store = createStore(reducer)
  123. const unsubscribe2 = store.subscribe(() => console.log('подписчик 2', store.getState()))
  124. const shop = store.subscribe(() => createDom(store.getState()))
  125. const купиПиво = СКОКА => ({type: 'КУПИТЬ', ШО: "пиво", СКОКА})
  126. const купиЧипсы = СКОКА => ({type: 'КУПИТЬ', ШО: "чипсы", СКОКА})
  127. const купиСиги = СКОКА => ({type: 'КУПИТЬ', ШО: "сиги", СКОКА})
  128. const купиШото = (СКОКА,ШО) => ({type: 'КУПИТЬ', ШО, СКОКА})
  129. store.dispatch(купиПиво(0))
  130. store.dispatch(купиЧипсы(0))
  131. store.dispatch(купиСиги(0))