1
1
import { applyMiddleware , createStore } from 'redux'
2
+ import Cookies from 'js-cookie'
2
3
import thunk from 'redux-thunk'
4
+ import isEmpty from 'lodash/isEmpty'
3
5
import isNil from 'lodash/isNil'
4
6
5
7
import { parseLocation } from '../utils/location'
@@ -12,6 +14,15 @@ import * as elementActions from '../actions/elementActions'
12
14
export default function configureStore ( ) {
13
15
const middlewares = [ thunk ]
14
16
17
+ // empty localStorage in new session
18
+ const currentStoreId = Cookies . get ( 'storeid' )
19
+ const localStoreId = localStorage . getItem ( 'rdmo.storeid' )
20
+
21
+ if ( isEmpty ( localStoreId ) || localStoreId !== currentStoreId ) {
22
+ localStorage . clear ( )
23
+ localStorage . setItem ( 'rdmo.storeid' , currentStoreId )
24
+ }
25
+
15
26
if ( process . env . NODE_ENV === 'development' ) {
16
27
const { logger } = require ( 'redux-logger' )
17
28
middlewares . push ( logger )
@@ -28,7 +39,6 @@ export default function configureStore() {
28
39
// load: restore the config from the local storage
29
40
const updateConfigFromLocalStorage = ( ) => {
30
41
const ls = { ...localStorage }
31
-
32
42
Object . entries ( ls ) . forEach ( ( [ lsPath , lsValue ] ) => {
33
43
const path = lsPath . replace ( 'rdmo.management.config.' , '' )
34
44
let value
@@ -46,9 +56,11 @@ export default function configureStore() {
46
56
} )
47
57
}
48
58
59
+ let currentSiteId
49
60
// load, popstate: fetch elements depending on the location
50
61
const fetchElementsFromLocation = ( ) => {
51
62
const baseUrl = store . getState ( ) . config . baseUrl
63
+ currentSiteId = store . getState ( ) . config . currentSite ?. id . toString ( ) || ''
52
64
const pathname = window . location . pathname
53
65
let { elementType, elementId, elementAction } = parseLocation ( baseUrl , pathname )
54
66
@@ -69,7 +81,13 @@ export default function configureStore() {
69
81
// this event is triggered when the page first loads
70
82
window . addEventListener ( 'load' , ( ) => {
71
83
updateConfigFromLocalStorage ( )
72
- fetchConfig ( ) . then ( ( ) => fetchElementsFromLocation ( ) )
84
+ fetchConfig ( ) . then ( ( ) => {
85
+ fetchElementsFromLocation ( )
86
+ if ( ! isEmpty ( currentSiteId ) && isEmpty ( store . getState ( ) . config . filter ) && store . getState ( ) . config . settings . multisite ) {
87
+ store . dispatch ( configActions . updateConfig ( 'filter.sites' , currentSiteId ) )
88
+ }
89
+ } )
90
+
73
91
} )
74
92
75
93
// this event is triggered when when the forward/back buttons are used
0 commit comments