script.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. function reducer(state, {
  2. type,
  3. ШО,
  4. СКОКА,
  5. ДЕНЕГ
  6. }) {
  7. if (!state) {
  8. return {
  9. пиво: {
  10. count: 100,
  11. price: 20
  12. },
  13. чипсы: {
  14. count: 60,
  15. price: 10
  16. },
  17. сиги: {
  18. count: 80,
  19. price: 40
  20. },
  21. касса: 0,
  22. ДЕНЕГ: 80
  23. }
  24. }
  25. if (type === 'КУПИТЬ') {
  26. if (state[ШО].count < СКОКА) {
  27. alert('NETU!')
  28. return state;
  29. } else if (state.ДЕНЕГ == 0) {
  30. alert('SHO BABOK NET?')
  31. return state;
  32. } else {
  33. console.log(СКОКА);
  34. if (state.ДЕНЕГ !== 0 && typeof state[ШО] === 'object' && СКОКА) {
  35. if (state[ШО].count < СКОКА) {
  36. alert('NETU!')
  37. return state;
  38. } else
  39. return {
  40. ...state,
  41. ДЕНЕГ: state.ДЕНЕГ - state[ШО].price,
  42. касса: state.касса + state[ШО].price,
  43. [ШО]: {
  44. ...state[ШО],
  45. count: state[ШО].count - СКОКА
  46. }
  47. }
  48. }
  49. return {
  50. ...state,
  51. [ШО]: state[ШО] - СКОКА,
  52. }
  53. }
  54. }
  55. return state
  56. }
  57. function createStore(reducer) {
  58. let state = reducer(undefined, {})
  59. let cbs = []
  60. const getState = () => state
  61. const subscribe = cb => (cbs.push(cb),
  62. () => cbs = cbs.filter(c => c !== cb))
  63. const dispatch = action => {
  64. const newState = reducer(state, action)
  65. if (newState !== state) {
  66. state = newState
  67. for (let cb of cbs) cb()
  68. }
  69. }
  70. return {
  71. getState,
  72. dispatch,
  73. subscribe
  74. }
  75. }
  76. const store = createStore(reducer);
  77. let select = document.querySelector('#goods');
  78. function createSelectItems(selectId, store) {
  79. for (let key of Object.keys(store.getState())) {
  80. if (key === 'касса') {
  81. return;
  82. } else {
  83. let option = document.createElement('option');
  84. option.value = key;
  85. option.innerText = key;
  86. select.append(option);
  87. }
  88. }
  89. }
  90. createSelectItems('goods', store);
  91. btn.addEventListener('click', () => {
  92. store.dispatch({
  93. type: 'КУПИТЬ',
  94. 'ШО': select.value,
  95. 'СКОКА': input.value
  96. })
  97. });
  98. function drawTable(root, store) {
  99. root.innerHTML = ''
  100. for (let [item, pcs] of Object.entries(store.getState())) {
  101. let tr = document.createElement('tr');
  102. let td = document.createElement('td');
  103. let th = document.createElement('th');
  104. if (typeof pcs === 'object') {
  105. let th = document.createElement('th');
  106. let tr = document.createElement('td');
  107. th.innerText = item;
  108. tr.append(th);
  109. for (let [el, price] of Object.entries(pcs)) {
  110. let td = document.createElement('td');
  111. let td1 = document.createElement('td');
  112. td.innerText = el;
  113. td1.innerText = price;
  114. tr.append(td, td1);
  115. root.append(th, tr);
  116. }
  117. } else {
  118. th.innerText = item;
  119. td.innerText = pcs;
  120. tr.append(th, td);
  121. root.append(tr);
  122. }
  123. }
  124. }
  125. drawTable(table, store);
  126. store.subscribe(() => drawTable(table, store))