Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catalogs view should filter by site #801 #804

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add if (lsPath.startsWith('rdmo.management.config.')) { after this line. This was an oversight by me. Otherwise rdmo.storeid and everything else in the store is picked up as well and stored again unter e.g. rdmo.management.config.rdmo.storeid.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ist erledigt. Schaust du bitte noch mal ob ich das if-Statement auch an der richtigen Stelle beende?
Line 57 nach store.dispatch(...

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