Skip to content

Commit

Permalink
Catalogs view should filter by site #801
Browse files Browse the repository at this point in the history
* set storeid cookie in management views.py
* clear localStorage in new session
* when multisite: filter by currentSite when catalogs page first loads
  • Loading branch information
CalamityC authored and MyPyDavid committed Nov 24, 2023
1 parent 5e63c75 commit 4b85c48
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
22 changes: 20 additions & 2 deletions rdmo/management/assets/js/store/configureStore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { applyMiddleware, createStore } from 'redux'
import Cookies from 'js-cookie'
import thunk from 'redux-thunk'
import isEmpty from 'lodash/isEmpty'
import isNil from 'lodash/isNil'

import { parseLocation } from '../utils/location'
Expand All @@ -12,6 +14,15 @@ import * as elementActions from '../actions/elementActions'
export default function configureStore() {
const middlewares = [thunk]

// empty localStorage in new session
const currentStoreId = Cookies.get('storeid')
const localStoreId = localStorage.getItem('rdmo.storeid')

if (isEmpty(localStoreId) || localStoreId !== currentStoreId) {
localStorage.clear()
localStorage.setItem('rdmo.storeid', currentStoreId)
}

if (process.env.NODE_ENV === 'development') {
const { logger } = require('redux-logger')
middlewares.push(logger)
Expand All @@ -28,7 +39,6 @@ export default function configureStore() {
// load: restore the config from the local storage
const updateConfigFromLocalStorage = () => {
const ls = {...localStorage}

Object.entries(ls).forEach(([lsPath, lsValue]) => {
const path = lsPath.replace('rdmo.management.config.', '')
let value
Expand All @@ -46,9 +56,11 @@ export default function configureStore() {
})
}

let currentSiteId
// load, popstate: fetch elements depending on the location
const fetchElementsFromLocation = () => {
const baseUrl = store.getState().config.baseUrl
currentSiteId = store.getState().config.currentSite?.id.toString() || ''
const pathname = window.location.pathname
let { elementType, elementId, elementAction } = parseLocation(baseUrl, pathname)

Expand All @@ -69,7 +81,13 @@ export default function configureStore() {
// this event is triggered when the page first loads
window.addEventListener('load', () => {
updateConfigFromLocalStorage()
fetchConfig().then(() => fetchElementsFromLocation())
fetchConfig().then(() => {
fetchElementsFromLocation()
if (!isEmpty(currentSiteId) && isEmpty(store.getState().config.filter) && store.getState().config.settings.multisite) {
store.dispatch(configActions.updateConfig('filter.sites', currentSiteId))
}
})

})

// this event is triggered when when the forward/back buttons are used
Expand Down
8 changes: 8 additions & 0 deletions rdmo/management/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import logging

from django.contrib.auth.mixins import LoginRequiredMixin
Expand All @@ -18,3 +19,10 @@ class ManagementView(LoginRequiredMixin, PermissionRedirectMixin, RulesPermissio
def has_permission(self):
# Use test_rule from rules for permissions check
return test_rule('management.can_view_management', self.request.user, self.request.site)

def render_to_response(self, context, **response_kwargs):
storeid = hashlib.sha256(self.request.session.session_key.encode()).hexdigest()

response = super().render_to_response(context, **response_kwargs)
response.set_cookie('storeid', storeid)
return response

0 comments on commit 4b85c48

Please sign in to comment.