HW13.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // create Store
  2. function createStore(reducer) {
  3. let state = reducer(undefined, {})
  4. let cbs = []
  5. const getState = () => state
  6. const subscribe = cb => (cbs.push(cb),
  7. () => cbs = cbs.filter(c => c !== cb))
  8. const dispatch = action => {
  9. const newState = reducer(state, action)
  10. if (newState !== state) {
  11. state = newState
  12. for (let cb of cbs) cb()
  13. }
  14. }
  15. return {
  16. getState,
  17. dispatch,
  18. subscribe
  19. }
  20. }
  21. // create Reduser
  22. function reducer(state, { type, ШО, СКОКА, БАБЛО }) {
  23. if (!state) {
  24. return {
  25. пиво: {
  26. amount: 110,
  27. price: 20
  28. },
  29. чипсы: {
  30. amount: 120,
  31. price: 30
  32. },
  33. сиги: {
  34. amount: 130,
  35. price: 50
  36. },
  37. касса: 200
  38. }
  39. }
  40. if (type === 'КУПИТЬ' && СКОКА <= state[ШО].amount && БАБЛО >= (СКОКА * state[ШО].price)) {
  41. return {
  42. ...state,
  43. [ШО]: {
  44. amount: state[ШО].amount - СКОКА,
  45. price: state[ШО].price
  46. },
  47. касса: state.касса + БАБЛО
  48. }
  49. }
  50. return state
  51. }
  52. const store = createStore(reducer)
  53. // create divStore
  54. const divStore = document.createElement('div')
  55. divStore.style.cssText = `
  56. min-width: 500px;
  57. width: 50%;
  58. border: 1px solid #A9A9A9;
  59. border-radius: 8px;
  60. box-shadow: rgb(0 0 0 / 50%) 5px 5px 5px 0px;
  61. padding: 20px;`
  62. document.body.append(divStore)
  63. // create Table of Store
  64. let table, col, row
  65. const createTable = () => {
  66. table = document.createElement('table')
  67. divStore.append(table)
  68. // create names of parametrs
  69. const params = []
  70. for (const child of Object.values(store.getState())) {
  71. for (const key of Object.keys(child)) {
  72. if (!params.includes(key)) {
  73. params.push(key);
  74. }
  75. }
  76. }
  77. // create Header of table
  78. row = document.createElement('tr')
  79. table.append(row)
  80. // create first cell in Header
  81. col = document.createElement('td')
  82. row.append(col)
  83. // create header names
  84. for (const key of params) {
  85. col = document.createElement('td')
  86. col.innerText = `${key}`
  87. col.style.cssText = `
  88. font-size: 1.5em;
  89. padding: 5px 10px;
  90. margin: 0 10px;
  91. text-align: center;
  92. background-color: #D9D9D9;`
  93. row.append(col)
  94. }
  95. // create table body
  96. for (const [key, values] of Object.entries(store.getState())) {
  97. if (key !== 'касса') {
  98. // рисуем строку заголовков
  99. row = document.createElement('tr')
  100. table.append(row)
  101. col = document.createElement('td')
  102. col.innerText = `${key.toUpperCase()}`
  103. col.style.cssText = `
  104. min-width: 150px;
  105. font-size: 2em;
  106. padding: 5px 10px;
  107. text-align: left`
  108. row.append(col)
  109. // рисуем строки значений для товаров
  110. for (const name of params) {
  111. col = document.createElement('td')
  112. col.innerText = `${(Object.keys(values)).includes(name) ? values[name] : ''}`
  113. col.style.cssText = `
  114. font-size: 1.5em;
  115. text-align: center`
  116. row.append(col)
  117. }
  118. } else {
  119. row = document.createElement('tr')
  120. table.append(row)
  121. col = document.createElement('td')
  122. col.innerText = `${key.toUpperCase()}`
  123. col.style.cssText = `
  124. min-width: 150px;
  125. font-size: 2em;
  126. padding: 5px 10px;
  127. text-align: left`
  128. row.append(col)
  129. col = document.createElement('td')
  130. col.innerText = `${values}`
  131. col.style.cssText = `
  132. font-size: 2em;
  133. text-align: center;`
  134. row.append(col)
  135. }
  136. }
  137. }
  138. createTable()
  139. // start test in console.log
  140. store.subscribe(() => console.log(store.getState()))
  141. // create divWindow
  142. const divWindow = document.createElement('div')
  143. divWindow.style.cssText = `
  144. width: 50%;
  145. padding: 10px;
  146. margin-top: 30px;
  147. text-align: ;
  148. display: flex;
  149. align-items: center;
  150. justify-content: space-between;`
  151. document.body.append(divWindow)
  152. // create inputAmount
  153. const inputAmount = document.createElement('input')
  154. inputAmount.type = 'number'
  155. inputAmount.min = 0
  156. inputAmount.placeholder = 'Количество товара'
  157. divWindow.append(inputAmount)
  158. // create selectProduct
  159. const selectProduct = document.createElement('select')
  160. divWindow.append(selectProduct)
  161. for (let key of Object.keys(store.getState())) {
  162. if (key !== 'касса') {
  163. let optionInput = document.createElement('option')
  164. optionInput.innerText = `${key}`
  165. selectProduct.append(optionInput)
  166. }
  167. }
  168. // ceate inputMoney
  169. const inputMoney = document.createElement('input')
  170. inputMoney.type = 'number'
  171. inputMoney.min = 0
  172. inputMoney.placeholder = 'Сумма, которую отдаете'
  173. divWindow.append(inputMoney)
  174. // create buyButton
  175. const buyButton = document.createElement('button')
  176. buyButton.innerText = 'Купить'
  177. divWindow.append(buyButton)
  178. // mistake alert
  179. buyButton.onclick = () => {
  180. if (+inputAmount.value > store.getState()[selectProduct.value].amount) {
  181. alert(`Количество товара, который вы хотите купить, больше, чем наличие на складе! На складе ${store.getState()[selectProduct.value].amount} единиц ${selectProduct.value}`)
  182. } else if (+inputMoney.value < (+inputAmount.value * store.getState()[selectProduct.value].price)) {
  183. alert(`Вы даете ${inputMoney.value}. А нужно ${+inputAmount.value * store.getState()[selectProduct.value].price}. Добавьте ${(+inputAmount.value * store.getState()[selectProduct.value].price) - +inputMoney.value}`)
  184. }
  185. }
  186. // start buyButton
  187. buyButton.addEventListener('click', () => store.dispatch({ type: 'КУПИТЬ', ШО: selectProduct.value, СКОКА: +inputAmount.value, БАБЛО: +inputMoney.value }))
  188. // Relaod table with new datas
  189. store.subscribe(() => {
  190. table.remove(),
  191. createTable()
  192. })