Skip to content
Open
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
17 changes: 17 additions & 0 deletions frontend/common/hooks/useSelectedOrganisation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useSelector } from 'react-redux'
import { useGetOrganisationsQuery } from 'common/services/useOrganisation'
import { StoreStateType } from 'common/store'

const useSelectedOrganisation = () => {
const selectedId = useSelector(
(state: StoreStateType) => state.selectedOrganisation?.id,
)
const { data: organisationsData } = useGetOrganisationsQuery({})
const organisation = organisationsData?.results?.find(
(org) => org.id === selectedId,
)

return organisation
}

export default useSelectedOrganisation
22 changes: 22 additions & 0 deletions frontend/common/selectedOrganisationSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit'

type SelectedOrganisationState = {
id: number | undefined
}

const initialState: SelectedOrganisationState = {
id: undefined,
}

const selectedOrganisationSlice = createSlice({
initialState,
name: 'selectedOrganisation',
reducers: {
setSelectedOrganisationId(state, action: PayloadAction<number>) {
state.id = action.payload
},
},
})

export const { setSelectedOrganisationId } = selectedOrganisationSlice.actions
export default selectedOrganisationSlice.reducer
2 changes: 2 additions & 0 deletions frontend/common/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import {
import storage from 'redux-persist/lib/storage'
import { Persistor } from 'redux-persist/es/types'
import { service } from './service'
import selectedOrganisationReducer from './selectedOrganisationSlice'
// END OF IMPORTS
const createStore = () => {
const reducer = combineReducers({
[service.reducerPath]: service.reducer,
selectedOrganisation: selectedOrganisationReducer,
// END OF REDUCERS
})

Expand Down
5 changes: 5 additions & 0 deletions frontend/common/stores/account-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import dataRelay from 'data-relay'
import { sortBy } from 'lodash'
import Project from 'common/project'
import { getStore } from 'common/store'
import { setSelectedOrganisationId } from 'common/selectedOrganisationSlice'
import { service } from 'common/service'
import { getBuildVersion } from 'common/services/useBuildVersion'
import { createOnboardingSupportOptIn } from 'common/services/useOnboardingSupportOptIn'
Expand Down Expand Up @@ -309,6 +310,7 @@ const controller = {
selectOrganisation: (id) => {
API.setCookie('organisation', `${id}`)
store.organisation = find(store.model.organisations, { id })
getStore().dispatch(setSelectedOrganisationId(id))
store.changed()
},

Expand Down Expand Up @@ -344,6 +346,9 @@ const controller = {
AppActions.getOrganisation(orgId)
}
}
if (store.organisation?.id) {
getStore().dispatch(setSelectedOrganisationId(store.organisation.id))
}
}

AsyncStorage.setItem('user', JSON.stringify(store.model))
Expand Down
3 changes: 1 addition & 2 deletions frontend/web/components/BreadcrumbSeparator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import AccountStore from 'common/stores/account-store'
import { useGetProjectsQuery } from 'common/services/useProject'
import { useGetOrganisationsQuery } from 'common/services/useOrganisation'
import { useHistory } from 'react-router-dom'
import OrganisationStore from 'common/stores/organisation-store'
import AppActions from 'common/dispatcher/app-actions'
import Utils from 'common/utils/utils'
import { getStore } from 'common/store'
Expand Down Expand Up @@ -177,7 +176,7 @@ const BreadcrumbSeparator: FC<BreadcrumbSeparatorType> = ({
}
AccountStore.on('change', onChangeAccountStore)
return () => {
OrganisationStore.off('change', onChangeAccountStore)
AccountStore.off('change', onChangeAccountStore)
}
//eslint-disable-next-line
}, [])
Expand Down
14 changes: 3 additions & 11 deletions frontend/web/components/navigation/SelectOrgAndProject.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { FC } from 'react'
import AccountStore from 'common/stores/account-store'
import { Link, NavLink } from 'react-router-dom'
import BreadcrumbSeparator from 'components/BreadcrumbSeparator'
import classNames from 'classnames'
import Utils from 'common/utils/utils'
import { Project } from 'common/types/responses'
import { useGetOrganisationQuery } from 'common/services/useOrganisation'
import useSelectedOrganisation from 'common/hooks/useSelectedOrganisation'
import { appLevelPaths } from './constants'

type SelectOrgAndProjectType = {
Expand All @@ -18,12 +17,7 @@ const SelectOrgAndProject: FC<SelectOrgAndProjectType> = ({
projectId,
}) => {
const isAppLevelPage = appLevelPaths.includes(document.location.pathname)

const organisationId = AccountStore.getOrganisation()?.id
const { data: organisation } = useGetOrganisationQuery(
{ id: organisationId as number },
{ skip: !organisationId },
)
const organisation = useSelectedOrganisation()

return (
<Row className='gap-2'>
Expand Down Expand Up @@ -57,9 +51,7 @@ const SelectOrgAndProject: FC<SelectOrgAndProjectType> = ({
})}
to={Utils.getOrganisationHomePage()}
>
<div>
{organisation?.name || AccountStore.getOrganisation()?.name}
</div>
<div>{organisation?.name}</div>
</NavLink>
</BreadcrumbSeparator>
</div>
Expand Down
Loading