123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- // redux Домашнее задание.
- // задание 1 общие правила, задачи
- // настройка товаров, кассы и покупку товаров с помощью Redux
- import { createStore } from 'redux';
- const BUY = 'BUY';
- const initialState = {
- beer: {
- name: 'Пиво',
- quantity: 100,
- price: 50,
- },
- soda: {
- name: 'Газировка',
- quantity: 50,
- price: 30,
- },
- juice: {
- name: 'Сок',
- quantity: 20,
- price: 70,
- },
- cash: 0,
- };
- function buyActionCreator(product, quantity) {
- return {
- type: BUY,
- product,
- quantity,
- };
- }
- function reducer(state = initialState, action) {
- switch (action.type) {
- case BUY:
- const product = state[action.product];
- if (product.quantity < action.quantity) {
- return state;
- }
- return {
- ...state,
- [action.product]: {
- ...product,
- quantity: product.quantity - action.quantity,
- },
- cash: state.cash + product.price * action.quantity,
- };
- // Другие случаи...
- }
- }
- const store = createStore(reducer);
- const productSelect = document.getElementById('product-select');
- const quantityInput = document.getElementById('quantity-input');
- const buyButton = document.getElementById('buy-button');
- store.subscribe(() => {
- console.log(store.getState());
- });
- buyButton.addEventListener('click', () => {
- const product = productSelect.value;
- const quantity = quantityInput.value;
- store.dispatch(buyActionCreator(product, quantity));
- });
- {
- // Чтобы связать эти элементы с Redux-стором
- const productSelect = document.getElementById('product-select');
- const quantityInput = document.getElementById('quantity-input');
- const buyButton = document.getElementById('buy-button');
- buyButton.addEventListener('click', () => {
- const product = productSelect.value;
- const quantity = quantityInput.value;
- store.dispatch(buyActionCreator(product, quantity));
- });
- }
- {
- // проверку на уход в минус в редуктор
- function reducer(state = initialState, action) {
- switch (action.type) {
- case BUY:
- const product = state[action.product];
- if (product.quantity < action.quantity) {
- return state;
- }
- return {
- ...state,
- [action.product]: {
- ...product,
- quantity: product.quantity - action.quantity,
- },
- cash: state.cash + product.price * action.quantity,
- };
- // Другие случаи...
- }
- }
- }
- {
- // добавить в state цену каждого товара и поля кассы
- const initialState = {
- products: {
- beer: {
- quantity: 100,
- price: 10,
- },
- soda: {
- quantity: 50,
- price: 5,
- },
- juice: {
- quantity: 30,
- price: 15,
- },
- },
- cash: 0,
- };
- function reducer(state = initialState, action) {
- switch (action.type) {
- case BUY:
- const product = state.products[action.product];
- if (product.quantity < action.quantity) {
- return state;
- }
- return {
- ...state,
- products: {
- ...state.products,
- [action.product]: {
- ...product,
- quantity: product.quantity - action.quantity,
- },
- },
- cash: state.cash + product.price * action.quantity,
- };
- }
- }
- }
- {
- // отобразить состояние на экране
- const cashElement = document.getElementById('cash');
- store.subscribe(() => {
- const state = store.getState();
- cashElement.innerHTML = state.cash;
- });
- }
- {
- // обработать событие отправки формы
- const form = document.getElementById('buy-form');
- form.addEventListener('submit', (event) => {
- event.preventDefault();
- const product = document.getElementById('product').value;
- const quantity = parseInt(document.getElementById('quantity').value, 10);
- store.dispatch(buyActionCreator(product, quantity));
- });
- }
- {
- function buyActionCreator(product, quantity) {
- return {
- type: BUY,
- product,
- quantity,
- };
- }
- }
|