@@ -5,19 +5,27 @@ import {
5
5
RootStoreInterface ,
6
6
RootStoreModel ,
7
7
StoreModuleNames ,
8
- MutationType
8
+ MutationType ,
9
+ // GEN-IMPORTS-STATE-INTERFACE
10
+ ItemsStateInterface ,
11
+ LocalesStateInterface
9
12
} from '@/models/store'
10
13
14
+
11
15
import { initialRootState } from './initialState'
12
16
13
17
import { LocalStorageKeys } from '@/models/local-storage/LocalStorageKeys'
14
18
15
- // GEN-IMPORTS
16
19
// import each Vuex module
20
+ // GEN-IMPORTS-STATE
17
21
import { itemsState } from '@/store/items'
18
22
import { localesState } from '@/store/locales'
19
23
20
- // Vuex store options to build our modularized namespaced store
24
+ /**
25
+ * @name storeOptions
26
+ * @description
27
+ * Vuex store options to build our modularized namespaced store
28
+ */
21
29
const storeOptions : StoreOptions < RootStateInterface > = {
22
30
state : initialRootState ,
23
31
@@ -32,14 +40,68 @@ const storeOptions: StoreOptions<RootStateInterface> = {
32
40
// Vuex Root store instance
33
41
export const store : RootStoreModel < RootStateInterface > = < any > createStore ( storeOptions )
34
42
43
+ /**
44
+ * @name dispatchModuleAction
45
+ * @description
46
+ * Private helper to dispatch an action to a Vuex module from one place
47
+ * So we keep the string interpolation for `${moduleName}/${actionName}` in one place only
48
+ * and be able to dispatch action with less code in a strongly-type way
49
+ * @param moduleName
50
+ * @param actionName
51
+ * @param params
52
+ */
53
+ function dispatchModuleAction < T > ( moduleName : string , actionName : string , params ?: T ) : void {
54
+ store . dispatch ( `${ moduleName } /${ actionName } ` , params )
55
+ }
56
+
57
+ // create individual module-store wrappers by app domain
58
+ // where each wrapper returns the corresponding module state from the Vuex Store
59
+ // and has an action<T> that allows us to dispatch the action with less
60
+ // code and in astrongly-typed way
61
+ /**
62
+ * @name itemsStore
63
+ * @description
64
+ * The items store wrapper that returns the itemsState and exposes a generic action<T> method
65
+ */
66
+ const itemsStore = {
67
+ get state ( ) : ItemsStateInterface {
68
+ return store . state . itemsState
69
+ } ,
70
+ action < T > ( actionName : string , params ?: T ) : void {
71
+ dispatchModuleAction ( StoreModuleNames . itemsState , actionName , params )
72
+ }
73
+ }
74
+
75
+ /**
76
+ * @name localesStore
77
+ * @description
78
+ * The locales store wrapper that retuns the localesState and exposes a generic action<T> method
79
+ */
80
+ const localesStore = {
81
+ get state ( ) : LocalesStateInterface {
82
+ return store . state . localesState
83
+ } ,
84
+ action < T > ( actionName : string , params ?: T ) : void {
85
+ dispatchModuleAction ( StoreModuleNames . localesState , actionName , params )
86
+ }
87
+ }
88
+
89
+ // export our wrappers using the composition API convention (i.e. useXYZ)
90
+ export const useItemsStore = ( ) => {
91
+ return itemsStore
92
+ }
93
+ export const useLocalesStore = ( ) => {
94
+ return localesStore
95
+ }
96
+
35
97
// for the current locale,
36
98
// try using the last user's preferred locale
37
99
// if available from localStorage
38
100
const userPreferredLocaleId : string = localStorage . getItem ( LocalStorageKeys . locale ) || ''
39
101
if ( userPreferredLocaleId . length > 0 ) {
40
102
// change locale selected
41
- store . dispatch (
42
- ` ${ StoreModuleNames . localesState } / ${ MutationType . locales . selectLocale } ` ,
103
+ localesStore . action (
104
+ MutationType . locales . selectLocale ,
43
105
userPreferredLocaleId
44
106
)
45
107
}
0 commit comments