Skip to content

Commit d059ffb

Browse files
committed
refactor: vendinMachine > submitChargerForm 메소드 dom 의존성 제거
1 parent bbee807 commit d059ffb

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

src/js/controller/vendingMachine.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { ERROR_MESSAGE } from "../utils/constants.js";
77
import {
88
getLocalStorage,
99
setLocalStorage,
10-
validateChargerInput,
1110
validateManagerInputs,
1211
} from "../utils/utils.js";
1312

@@ -52,14 +51,7 @@ class VendingMachineController {
5251

5352
submitChargerForm(e) {
5453
e.preventDefault();
55-
const chargingInput = $(".charger-input");
56-
try {
57-
validateChargerInput(chargingInput.value);
58-
this.#currentModel.setState("totalAmount", Number(chargingInput.value));
59-
} catch (err) {
60-
alert(err.message);
61-
chargingInput.focus();
62-
}
54+
this.#currentModel.setState("totalAmount", e);
6355
}
6456

6557
submitManagerForm(e) {

src/js/model/changeCharger.js

+35-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import {
44
getLocalStorage,
55
isInitialState,
66
setLocalStorage,
7+
validateChargerInput,
78
} from "../utils/utils.js";
9+
import { ERROR_MESSAGE } from "../utils/constants.js";
10+
import { ValidationError } from "../utils/error.js";
811

912
const CHANGE_CHARGER_INITIAL_STATE = {
1013
totalAmount: 0,
@@ -30,18 +33,44 @@ class ChangeChargerModel {
3033
return this.#state;
3134
}
3235

33-
setState(state, newState) {
34-
this.#state[state] += newState;
35-
36-
const coinCounts = calculateCoinCount(newState);
36+
setCoinCounts(inputAmount) {
37+
const coinCounts = calculateCoinCount(inputAmount);
3738

3839
Object.keys(this.#state.coinCounts).forEach((key) => {
3940
this.#state.coinCounts[key] += coinCounts[key];
4041
});
42+
}
43+
44+
setTotalAmount(inputAmount) {
45+
this.#state.totalAmount += inputAmount;
46+
}
47+
48+
updateTotalAmount(chargerInput) {
49+
const inputAmount = Number(chargerInput.value);
50+
try {
51+
validateChargerInput(inputAmount);
4152

42-
setLocalStorage("charger", this.#state);
53+
this.setTotalAmount(inputAmount);
54+
this.setCoinCounts(inputAmount);
55+
this.#view.update(this.#state);
4356

44-
this.#view.update(this.#state);
57+
setLocalStorage("charger", this.#state);
58+
} catch (err) {
59+
if (err.message === ERROR_MESSAGE.INVALID_STATE) return;
60+
61+
alert(err.message);
62+
chargerInput.focus();
63+
}
64+
}
65+
66+
setState(state, e) {
67+
switch (state) {
68+
case "totalAmount":
69+
this.updateTotalAmount(e.target["charger-input"]);
70+
break;
71+
default:
72+
throw new ValidationError(ERROR_MESSAGE.INVALID_STATE);
73+
}
4574
}
4675

4776
initialize() {

src/js/utils/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ export const ERROR_MESSAGE = {
1111
EMPTY_VALUE: "빈 값이 입력되었습니다.",
1212
INVALID_AMOUNT: `${MINIMUM_PRODUCT_PRICE}원 이상 입력 가능합니다.`,
1313
INVALID_UNIT: `${PRODUCT_PRICE_UNIT}원 단위로 입력 가능합니다.`,
14+
INVALID_STATE: "잘못된 state가 입력되었습니다",
1415
};

src/js/utils/templates.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const COIN_CHARGING_TEMPLATE = `<div id="coin-charging-container">
7474
<input
7575
type="number"
7676
class="charger-input"
77-
name="product-quantity"
77+
name="charger-input"
7878
placeholder="자판기가 보유할 금액"
7979
min="0"
8080
/>

0 commit comments

Comments
 (0)