Skip to content

Commit 015f492

Browse files
committed
feat: localStorage 기능 추가
1 parent c648c35 commit 015f492

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/js/model/changeCharger.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import ChangeChargerView from "../view/changeCharger.js";
2-
import { calculateCoinCount, isInitialState } from "../utils/utils.js";
2+
import {
3+
calculateCoinCount,
4+
getLocalStorage,
5+
isInitialState,
6+
setLocalStorage,
7+
} from "../utils/utils.js";
38

49
const CHANGE_CHARGER_INITIAL_STATE = {
510
totalAmount: 0,
@@ -15,7 +20,9 @@ class ChangeChargerModel {
1520
#state;
1621

1722
constructor() {
18-
this.#state = { ...CHANGE_CHARGER_INITIAL_STATE };
23+
const initialState =
24+
getLocalStorage("charger") || CHANGE_CHARGER_INITIAL_STATE;
25+
this.#state = { ...initialState };
1926
this.#view = new ChangeChargerView();
2027
}
2128

@@ -31,6 +38,9 @@ class ChangeChargerModel {
3138
Object.keys(this.#state.coinCounts).forEach((key) => {
3239
this.#state.coinCounts[key] += coinCounts[key];
3340
});
41+
42+
setLocalStorage("charger", this.#state);
43+
3444
this.#view.update(this.#state);
3545
}
3646

src/js/model/productManager.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import ProductManagerView from "../view/productManager.js";
2-
import { isInitialState } from "../utils/utils.js";
2+
import {
3+
getLocalStorage,
4+
isInitialState,
5+
setLocalStorage,
6+
} from "../utils/utils.js";
37

48
const PRODUCT_MANAGER_INITIAL_STATE = {
59
products: [],
@@ -9,7 +13,9 @@ class ProductManagerModel {
913
#view;
1014

1115
constructor() {
12-
this.#state = { ...PRODUCT_MANAGER_INITIAL_STATE };
16+
const initialState =
17+
getLocalStorage("manager") || PRODUCT_MANAGER_INITIAL_STATE;
18+
this.#state = { ...initialState };
1319
this.#view = new ProductManagerView();
1420
}
1521

@@ -23,6 +29,8 @@ class ProductManagerModel {
2329
);
2430

2531
this.#state[state] = [...removedState, newState];
32+
setLocalStorage("manager", this.#state);
33+
2634
this.#view.update(this.#state[state]);
2735
}
2836

src/js/utils/utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ export const calculateCoinCount = (coin) => {
5656

5757
return result;
5858
};
59+
60+
export const setLocalStorage = (key, data) => {
61+
localStorage.setItem(key, JSON.stringify(data));
62+
};
63+
64+
export const getLocalStorage = (key) => JSON.parse(localStorage.getItem(key));

0 commit comments

Comments
 (0)