|
@@ -1,13 +1,57 @@
|
|
|
import { store } from './redux/store';
|
|
|
+import { defaultState } from './redux/shop/reducer/';
|
|
|
|
|
|
-store.subscribe(() => console.log(store.getState()));
|
|
|
+const shopGoods = document.getElementsByClassName('goodsList')[0];
|
|
|
+const cashRegister = document.getElementsByClassName('cashRegister')[0];
|
|
|
|
|
|
-store.dispatch({ type: 'INCREMENT' });
|
|
|
+const reDrawShop = state => {
|
|
|
+ cashRegister.children[0].textContent = `Shop cash : "${state.cash} $"`;
|
|
|
+ cashRegister.children[1].textContent = `User cash : "${state.userCash} $"`;
|
|
|
+ for (const li in shopGoods.children) {
|
|
|
+ const htmlLi = shopGoods.children[li];
|
|
|
+ if (typeof htmlLi === 'object') {
|
|
|
+ const name = htmlLi.attributes.name.value;
|
|
|
+ const quantity = htmlLi.children[0];
|
|
|
+ const price = htmlLi.children[1];
|
|
|
+ quantity.textContent = `left : ${state[name].quantity} st ,`;
|
|
|
+ price.textContent = `price : ${state[name].price} $ .`;
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
|
|
|
-store.dispatch({ type: 'INCREMENT' });
|
|
|
+reDrawShop(defaultState);
|
|
|
|
|
|
-store.dispatch({ type: 'DECREMENT' });
|
|
|
+store.subscribe(() => reDrawShop(store.getState().shop));
|
|
|
|
|
|
-store.dispatch({ type: 'DECREMENT' });
|
|
|
+const shopForm = document.getElementById('shop-form');
|
|
|
|
|
|
-store.dispatch({ type: 'DECREMENT' });
|
|
|
+const handelShopForm = function (e) {
|
|
|
+ e.preventDefault();
|
|
|
+ const form = this.children;
|
|
|
+ const quantity = form[1].value;
|
|
|
+ const select = form[0].children;
|
|
|
+ for (const option in select) {
|
|
|
+ if (select[option].selected && quantity) {
|
|
|
+ store.dispatch({
|
|
|
+ type: select[option].value.toLowerCase(),
|
|
|
+ payload: quantity,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+shopForm.addEventListener('submit', handelShopForm);
|
|
|
+
|
|
|
+const handleCashHtml =
|
|
|
+ document.getElementsByClassName('handleCash')[0].children;
|
|
|
+
|
|
|
+const handleCash = function () {
|
|
|
+ if (this.value < 0) return;
|
|
|
+ store.dispatch({
|
|
|
+ type: this.name,
|
|
|
+ payload: this.value,
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+handleCashHtml[0].addEventListener('input', handleCash);
|
|
|
+handleCashHtml[1].addEventListener('input', handleCash);
|