chore(plugin-multi-tenant): remove SELECT_ALL constant (#11660)
This commit is contained in:
@@ -6,8 +6,6 @@ import { useAuth } from '@payloadcms/ui'
|
||||
import { useRouter } from 'next/navigation.js'
|
||||
import React, { createContext } from 'react'
|
||||
|
||||
import { SELECT_ALL } from '../../constants.js'
|
||||
|
||||
type ContextType = {
|
||||
/**
|
||||
* Array of options to select from
|
||||
@@ -76,8 +74,8 @@ export const TenantSelectionProviderClient = ({
|
||||
({ id, refresh }) => {
|
||||
if (id === undefined) {
|
||||
if (tenantOptions.length > 1) {
|
||||
setSelectedTenantID(SELECT_ALL)
|
||||
setCookie(SELECT_ALL)
|
||||
setSelectedTenantID(undefined)
|
||||
deleteCookie()
|
||||
} else {
|
||||
setSelectedTenantID(tenantOptions[0]?.value)
|
||||
setCookie(String(tenantOptions[0]?.value))
|
||||
@@ -90,15 +88,11 @@ export const TenantSelectionProviderClient = ({
|
||||
router.refresh()
|
||||
}
|
||||
},
|
||||
[setSelectedTenantID, setCookie, router, preventRefreshOnChange, tenantOptions],
|
||||
[deleteCookie, preventRefreshOnChange, router, setCookie, setSelectedTenantID, tenantOptions],
|
||||
)
|
||||
|
||||
React.useEffect(() => {
|
||||
if (
|
||||
selectedTenantID &&
|
||||
selectedTenantID !== SELECT_ALL &&
|
||||
!tenantOptions.find((option) => option.value === selectedTenantID)
|
||||
) {
|
||||
if (selectedTenantID && !tenantOptions.find((option) => option.value === selectedTenantID)) {
|
||||
if (tenantOptions?.[0]?.value) {
|
||||
setTenant({ id: tenantOptions[0].value, refresh: true })
|
||||
} else {
|
||||
@@ -111,9 +105,13 @@ export const TenantSelectionProviderClient = ({
|
||||
if (userID && !tenantCookie) {
|
||||
// User is logged in, but does not have a tenant cookie, set it
|
||||
setSelectedTenantID(initialValue)
|
||||
setCookie(String(initialValue))
|
||||
if (initialValue) {
|
||||
setCookie(String(initialValue))
|
||||
} else {
|
||||
deleteCookie()
|
||||
}
|
||||
}
|
||||
}, [userID, tenantCookie, initialValue, setCookie, router])
|
||||
}, [userID, tenantCookie, initialValue, setCookie, deleteCookie, router])
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!userID && tenantCookie) {
|
||||
@@ -131,7 +129,7 @@ export const TenantSelectionProviderClient = ({
|
||||
data-selected-tenant-id={selectedTenantID}
|
||||
data-selected-tenant-title={selectedTenantLabel}
|
||||
>
|
||||
<Context.Provider
|
||||
<Context
|
||||
value={{
|
||||
options: tenantOptions,
|
||||
selectedTenantID,
|
||||
@@ -140,9 +138,9 @@ export const TenantSelectionProviderClient = ({
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</Context.Provider>
|
||||
</Context>
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
export const useTenantSelection = () => React.useContext(Context)
|
||||
export const useTenantSelection = () => React.use(Context)
|
||||
|
||||
@@ -2,7 +2,6 @@ import type { OptionObject, Payload, User } from 'payload'
|
||||
|
||||
import { cookies as getCookies } from 'next/headers.js'
|
||||
|
||||
import { SELECT_ALL } from '../../constants.js'
|
||||
import { findTenantOptions } from '../../queries/findTenantOptions.js'
|
||||
import { TenantSelectionProviderClient } from './index.client.js'
|
||||
|
||||
@@ -43,18 +42,24 @@ export const TenantSelectionProvider = async ({
|
||||
let tenantCookie = cookies.get('payload-tenant')?.value
|
||||
let initialValue = undefined
|
||||
|
||||
if (tenantOptions.length > 1 && tenantCookie === SELECT_ALL) {
|
||||
initialValue = SELECT_ALL
|
||||
} else {
|
||||
/**
|
||||
* Ensure the cookie is a valid tenant
|
||||
*/
|
||||
if (tenantCookie) {
|
||||
const matchingOption = tenantOptions.find((option) => String(option.value) === tenantCookie)
|
||||
if (matchingOption) {
|
||||
initialValue = matchingOption.value
|
||||
} else {
|
||||
tenantCookie = undefined
|
||||
initialValue = tenantOptions.length > 1 ? SELECT_ALL : tenantOptions[0]?.value
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the there was no cookie or the cookie was an invalid tenantID set intialValue
|
||||
*/
|
||||
if (!initialValue) {
|
||||
tenantCookie = undefined
|
||||
initialValue = tenantOptions.length > 1 ? undefined : tenantOptions[0]?.value
|
||||
}
|
||||
|
||||
return (
|
||||
<TenantSelectionProviderClient
|
||||
initialValue={initialValue}
|
||||
|
||||
Reference in New Issue
Block a user