index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // redux Домашнее задание.
  2. // задание 1 общие правила, задачи
  3. // настройка товаров, кассы и покупку товаров с помощью Redux
  4. import { createStore } from 'redux';
  5. const BUY = 'BUY';
  6. const initialState = {
  7. beer: {
  8. name: 'Пиво',
  9. quantity: 100,
  10. price: 50,
  11. },
  12. soda: {
  13. name: 'Газировка',
  14. quantity: 50,
  15. price: 30,
  16. },
  17. juice: {
  18. name: 'Сок',
  19. quantity: 20,
  20. price: 70,
  21. },
  22. cash: 0,
  23. };
  24. function buyActionCreator(product, quantity) {
  25. return {
  26. type: BUY,
  27. product,
  28. quantity,
  29. };
  30. }
  31. function reducer(state = initialState, action) {
  32. switch (action.type) {
  33. case BUY:
  34. const product = state[action.product];
  35. if (product.quantity < action.quantity) {
  36. return state;
  37. }
  38. return {
  39. ...state,
  40. [action.product]: {
  41. ...product,
  42. quantity: product.quantity - action.quantity,
  43. },
  44. cash: state.cash + product.price * action.quantity,
  45. };
  46. // Другие случаи...
  47. }
  48. }
  49. const store = createStore(reducer);
  50. const productSelect = document.getElementById('product-select');
  51. const quantityInput = document.getElementById('quantity-input');
  52. const buyButton = document.getElementById('buy-button');
  53. store.subscribe(() => {
  54. console.log(store.getState());
  55. });
  56. buyButton.addEventListener('click', () => {
  57. const product = productSelect.value;
  58. const quantity = quantityInput.value;
  59. store.dispatch(buyActionCreator(product, quantity));
  60. });
  61. {
  62. // Чтобы связать эти элементы с Redux-стором
  63. const productSelect = document.getElementById('product-select');
  64. const quantityInput = document.getElementById('quantity-input');
  65. const buyButton = document.getElementById('buy-button');
  66. buyButton.addEventListener('click', () => {
  67. const product = productSelect.value;
  68. const quantity = quantityInput.value;
  69. store.dispatch(buyActionCreator(product, quantity));
  70. });
  71. }
  72. {
  73. // проверку на уход в минус в редуктор
  74. function reducer(state = initialState, action) {
  75. switch (action.type) {
  76. case BUY:
  77. const product = state[action.product];
  78. if (product.quantity < action.quantity) {
  79. return state;
  80. }
  81. return {
  82. ...state,
  83. [action.product]: {
  84. ...product,
  85. quantity: product.quantity - action.quantity,
  86. },
  87. cash: state.cash + product.price * action.quantity,
  88. };
  89. // Другие случаи...
  90. }
  91. }
  92. }
  93. {
  94. // добавить в state цену каждого товара и поля кассы
  95. const initialState = {
  96. products: {
  97. beer: {
  98. quantity: 100,
  99. price: 10,
  100. },
  101. soda: {
  102. quantity: 50,
  103. price: 5,
  104. },
  105. juice: {
  106. quantity: 30,
  107. price: 15,
  108. },
  109. },
  110. cash: 0,
  111. };
  112. function reducer(state = initialState, action) {
  113. switch (action.type) {
  114. case BUY:
  115. const product = state.products[action.product];
  116. if (product.quantity < action.quantity) {
  117. return state;
  118. }
  119. return {
  120. ...state,
  121. products: {
  122. ...state.products,
  123. [action.product]: {
  124. ...product,
  125. quantity: product.quantity - action.quantity,
  126. },
  127. },
  128. cash: state.cash + product.price * action.quantity,
  129. };
  130. }
  131. }
  132. }
  133. {
  134. // отобразить состояние на экране
  135. const cashElement = document.getElementById('cash');
  136. store.subscribe(() => {
  137. const state = store.getState();
  138. cashElement.innerHTML = state.cash;
  139. });
  140. }
  141. {
  142. // обработать событие отправки формы
  143. const form = document.getElementById('buy-form');
  144. form.addEventListener('submit', (event) => {
  145. event.preventDefault();
  146. const product = document.getElementById('product').value;
  147. const quantity = parseInt(document.getElementById('quantity').value, 10);
  148. store.dispatch(buyActionCreator(product, quantity));
  149. });
  150. }
  151. {
  152. function buyActionCreator(product, quantity) {
  153. return {
  154. type: BUY,
  155. product,
  156. quantity,
  157. };
  158. }
  159. }