File tree 2 files changed +40
-3
lines changed
2 files changed +40
-3
lines changed Original file line number Diff line number Diff line change
1
+ import { merge } from 'lodash'
2
+
3
+ export default class Storage {
4
+
5
+ constructor ( key ) {
6
+
7
+ this . key = key ;
8
+
9
+ if ( ! window . localStorage ) {
10
+ console . error ( 'localStorage not available' ) ;
11
+ }
12
+ }
13
+
14
+ plugin = store => {
15
+
16
+ store . replaceState ( merge (
17
+ store . state ,
18
+ this . load ( )
19
+ ) ) ;
20
+
21
+ this . save ( store . state ) ;
22
+
23
+ store . subscribe ( ( mutation , state ) => {
24
+ this . save ( state ) ;
25
+ } )
26
+ } ;
27
+
28
+ load ( ) {
29
+ return JSON . parse ( window . localStorage . getItem ( this . key ) || '{}' ) ;
30
+ }
31
+
32
+ save ( data ) {
33
+ window . localStorage . setItem ( this . key , JSON . stringify ( data ) ) ;
34
+ }
35
+ }
Original file line number Diff line number Diff line change 1
1
import Vue from 'vue' ;
2
2
import Vuex from 'vuex' ;
3
3
4
- import Ticket from "@/models/ticket " ;
4
+ import Storage from "@/services/storage " ;
5
5
6
6
Vue . use ( Vuex ) ;
7
7
8
8
export default new Vuex . Store ( {
9
9
state : {
10
- tickets : [
11
- ] ,
10
+ tickets : [ ] ,
12
11
} ,
12
+ plugins : [
13
+ new Storage ( 'ticket-calc' ) . plugin
14
+ ] ,
13
15
getters : {
14
16
tickets : state => state . tickets ,
15
17
} ,
You can’t perform that action at this time.
0 commit comments