File tree Expand file tree Collapse file tree 3 files changed +28
-4
lines changed Expand file tree Collapse file tree 3 files changed +28
-4
lines changed Original file line number Diff line number Diff line change 1
1
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" ;
3
8
4
9
const CHANGE_CHARGER_INITIAL_STATE = {
5
10
totalAmount : 0 ,
@@ -15,7 +20,9 @@ class ChangeChargerModel {
15
20
#state;
16
21
17
22
constructor ( ) {
18
- this . #state = { ...CHANGE_CHARGER_INITIAL_STATE } ;
23
+ const initialState =
24
+ getLocalStorage ( "charger" ) || CHANGE_CHARGER_INITIAL_STATE ;
25
+ this . #state = { ...initialState } ;
19
26
this . #view = new ChangeChargerView ( ) ;
20
27
}
21
28
@@ -31,6 +38,9 @@ class ChangeChargerModel {
31
38
Object . keys ( this . #state. coinCounts ) . forEach ( ( key ) => {
32
39
this . #state. coinCounts [ key ] += coinCounts [ key ] ;
33
40
} ) ;
41
+
42
+ setLocalStorage ( "charger" , this . #state) ;
43
+
34
44
this . #view. update ( this . #state) ;
35
45
}
36
46
Original file line number Diff line number Diff line change 1
1
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" ;
3
7
4
8
const PRODUCT_MANAGER_INITIAL_STATE = {
5
9
products : [ ] ,
@@ -9,7 +13,9 @@ class ProductManagerModel {
9
13
#view;
10
14
11
15
constructor ( ) {
12
- this . #state = { ...PRODUCT_MANAGER_INITIAL_STATE } ;
16
+ const initialState =
17
+ getLocalStorage ( "manager" ) || PRODUCT_MANAGER_INITIAL_STATE ;
18
+ this . #state = { ...initialState } ;
13
19
this . #view = new ProductManagerView ( ) ;
14
20
}
15
21
@@ -23,6 +29,8 @@ class ProductManagerModel {
23
29
) ;
24
30
25
31
this . #state[ state ] = [ ...removedState , newState ] ;
32
+ setLocalStorage ( "manager" , this . #state) ;
33
+
26
34
this . #view. update ( this . #state[ state ] ) ;
27
35
}
28
36
Original file line number Diff line number Diff line change @@ -56,3 +56,9 @@ export const calculateCoinCount = (coin) => {
56
56
57
57
return result ;
58
58
} ;
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 ) ) ;
You can’t perform that action at this time.
0 commit comments