-
Notifications
You must be signed in to change notification settings - Fork 6
/
store.ts
39 lines (38 loc) · 1.15 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { Module } from 'vuex'
import store from '@vue-storefront/core/store'
import RootState from '@vue-storefront/core/types/RootState'
import { isServer } from '@vue-storefront/core/helpers'
import filter from 'lodash-es/filter'
export const module: Module<any, RootState> = {
namespaced: true,
getters: {
currentStoreView: (state, getters, rootState) => {
return rootState.storeView
},
storeViews: () => {
const { config } = store.state
return filter(config.storeViews, prop => prop.hasOwnProperty('storeCode') && prop.disabled === false)
},
isCurrent: (state, getters) => {
if (isServer) {
return true
}
return navigator.language === getters.currentStoreView.i18n.defaultLocale
},
saved: () => {
if (isServer) {
return undefined
}
const saved = localStorage.getItem('savedLocale')
return saved || undefined
},
suggested: (state, getters) => {
if (getters.isCurrent) {
return getters.currentStoreView
}
return getters.storeViews.find(
storeView => navigator.language === storeView.i18n.defaultLocale
)
},
}
}