Skip to content

Commit 39d783a

Browse files
chore(plugin-multi-tenant): remove SELECT_ALL constant (#11660)
1 parent c4fd27d commit 39d783a

File tree

11 files changed

+700
-719
lines changed

11 files changed

+700
-719
lines changed

examples/multi-tenant/pnpm-lock.yaml

Lines changed: 649 additions & 654 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/plugin-multi-tenant/src/components/TenantField/index.client.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { RelationshipFieldClientProps } from 'payload'
55
import { RelationshipField, useField } from '@payloadcms/ui'
66
import React from 'react'
77

8-
import { SELECT_ALL } from '../../constants.js'
98
import { useTenantSelection } from '../../providers/TenantSelectionProvider/index.client.js'
109
import './index.scss'
1110

@@ -30,14 +29,11 @@ export const TenantField = (args: Props) => {
3029
setTenant({ id: value, refresh: unique })
3130
} else {
3231
// in the document view, the tenant field should always have a value
33-
const defaultValue =
34-
!selectedTenantID || selectedTenantID === SELECT_ALL
35-
? options[0]?.value
36-
: selectedTenantID
32+
const defaultValue = selectedTenantID || options[0]?.value
3733
setTenant({ id: defaultValue, refresh: unique })
3834
}
3935
hasSetValueRef.current = true
40-
} else if ((!value || value !== selectedTenantID) && selectedTenantID !== SELECT_ALL) {
36+
} else if (!value || value !== selectedTenantID) {
4137
// Update the field on the document value when the tenant is changed
4238
setValue(selectedTenantID)
4339
}

packages/plugin-multi-tenant/src/components/TenantSelector/index.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import './index.scss'
77
import { SelectInput, useTranslation } from '@payloadcms/ui'
88
import React from 'react'
99

10-
import { SELECT_ALL } from '../../constants.js'
1110
import { useTenantSelection } from '../../providers/TenantSelectionProvider/index.client.js'
1211

1312
export const TenantSelector = ({ label, viewType }: { label: string; viewType?: ViewTypes }) => {
@@ -38,13 +37,7 @@ export const TenantSelector = ({ label, viewType }: { label: string; viewType?:
3837
onChange={handleChange}
3938
options={options}
4039
path="setTenant"
41-
value={
42-
selectedTenantID
43-
? selectedTenantID === SELECT_ALL
44-
? undefined
45-
: (selectedTenantID as string)
46-
: undefined
47-
}
40+
value={selectedTenantID as string | undefined}
4841
/>
4942
</div>
5043
)

packages/plugin-multi-tenant/src/constants.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/plugin-multi-tenant/src/list-filters/filterDocumentsBySelectedTenant.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { PayloadRequest, Where } from 'payload'
22

3-
import { SELECT_ALL } from '../constants.js'
43
import { getCollectionIDType } from '../utilities/getCollectionIDType.js'
54
import { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'
65

@@ -20,13 +19,13 @@ export const filterDocumentsBySelectedTenant = ({
2019
})
2120
const selectedTenant = getTenantFromCookie(req.headers, idType)
2221

23-
if (selectedTenant === SELECT_ALL) {
24-
return {}
22+
if (selectedTenant) {
23+
return {
24+
[tenantFieldName]: {
25+
equals: selectedTenant,
26+
},
27+
}
2528
}
2629

27-
return {
28-
[tenantFieldName]: {
29-
equals: selectedTenant,
30-
},
31-
}
30+
return {}
3231
}

packages/plugin-multi-tenant/src/list-filters/filterTenantsBySelectedTenant.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { PayloadRequest, Where } from 'payload'
22

3-
import { SELECT_ALL } from '../constants.js'
43
import { getCollectionIDType } from '../utilities/getCollectionIDType.js'
54
import { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'
65

@@ -18,13 +17,13 @@ export const filterTenantsBySelectedTenant = ({
1817
})
1918
const selectedTenant = getTenantFromCookie(req.headers, idType)
2019

21-
if (selectedTenant === SELECT_ALL) {
22-
return {}
20+
if (selectedTenant) {
21+
return {
22+
id: {
23+
equals: selectedTenant,
24+
},
25+
}
2326
}
2427

25-
return {
26-
id: {
27-
equals: selectedTenant,
28-
},
29-
}
28+
return {}
3029
}

packages/plugin-multi-tenant/src/list-filters/filterUsersBySelectedTenant.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { PayloadRequest, Where } from 'payload'
22

3-
import { SELECT_ALL } from '../constants.js'
43
import { getCollectionIDType } from '../utilities/getCollectionIDType.js'
54
import { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'
65

@@ -25,13 +24,13 @@ export const filterUsersBySelectedTenant = ({
2524
})
2625
const selectedTenant = getTenantFromCookie(req.headers, idType)
2726

28-
if (selectedTenant === SELECT_ALL) {
29-
return {}
27+
if (selectedTenant) {
28+
return {
29+
[`${tenantsArrayFieldName}.${tenantsArrayTenantFieldName}`]: {
30+
in: [selectedTenant],
31+
},
32+
}
3033
}
3134

32-
return {
33-
[`${tenantsArrayFieldName}.${tenantsArrayTenantFieldName}`]: {
34-
in: [selectedTenant],
35-
},
36-
}
35+
return {}
3736
}

packages/plugin-multi-tenant/src/providers/TenantSelectionProvider/index.client.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { useAuth } from '@payloadcms/ui'
66
import { useRouter } from 'next/navigation.js'
77
import React, { createContext } from 'react'
88

9-
import { SELECT_ALL } from '../../constants.js'
10-
119
type ContextType = {
1210
/**
1311
* Array of options to select from
@@ -76,8 +74,8 @@ export const TenantSelectionProviderClient = ({
7674
({ id, refresh }) => {
7775
if (id === undefined) {
7876
if (tenantOptions.length > 1) {
79-
setSelectedTenantID(SELECT_ALL)
80-
setCookie(SELECT_ALL)
77+
setSelectedTenantID(undefined)
78+
deleteCookie()
8179
} else {
8280
setSelectedTenantID(tenantOptions[0]?.value)
8381
setCookie(String(tenantOptions[0]?.value))
@@ -90,15 +88,11 @@ export const TenantSelectionProviderClient = ({
9088
router.refresh()
9189
}
9290
},
93-
[setSelectedTenantID, setCookie, router, preventRefreshOnChange, tenantOptions],
91+
[deleteCookie, preventRefreshOnChange, router, setCookie, setSelectedTenantID, tenantOptions],
9492
)
9593

9694
React.useEffect(() => {
97-
if (
98-
selectedTenantID &&
99-
selectedTenantID !== SELECT_ALL &&
100-
!tenantOptions.find((option) => option.value === selectedTenantID)
101-
) {
95+
if (selectedTenantID && !tenantOptions.find((option) => option.value === selectedTenantID)) {
10296
if (tenantOptions?.[0]?.value) {
10397
setTenant({ id: tenantOptions[0].value, refresh: true })
10498
} else {
@@ -111,9 +105,13 @@ export const TenantSelectionProviderClient = ({
111105
if (userID && !tenantCookie) {
112106
// User is logged in, but does not have a tenant cookie, set it
113107
setSelectedTenantID(initialValue)
114-
setCookie(String(initialValue))
108+
if (initialValue) {
109+
setCookie(String(initialValue))
110+
} else {
111+
deleteCookie()
112+
}
115113
}
116-
}, [userID, tenantCookie, initialValue, setCookie, router])
114+
}, [userID, tenantCookie, initialValue, setCookie, deleteCookie, router])
117115

118116
React.useEffect(() => {
119117
if (!userID && tenantCookie) {
@@ -131,7 +129,7 @@ export const TenantSelectionProviderClient = ({
131129
data-selected-tenant-id={selectedTenantID}
132130
data-selected-tenant-title={selectedTenantLabel}
133131
>
134-
<Context.Provider
132+
<Context
135133
value={{
136134
options: tenantOptions,
137135
selectedTenantID,
@@ -140,9 +138,9 @@ export const TenantSelectionProviderClient = ({
140138
}}
141139
>
142140
{children}
143-
</Context.Provider>
141+
</Context>
144142
</span>
145143
)
146144
}
147145

148-
export const useTenantSelection = () => React.useContext(Context)
146+
export const useTenantSelection = () => React.use(Context)

packages/plugin-multi-tenant/src/providers/TenantSelectionProvider/index.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { OptionObject, Payload, User } from 'payload'
22

33
import { cookies as getCookies } from 'next/headers.js'
44

5-
import { SELECT_ALL } from '../../constants.js'
65
import { findTenantOptions } from '../../queries/findTenantOptions.js'
76
import { TenantSelectionProviderClient } from './index.client.js'
87

@@ -43,18 +42,24 @@ export const TenantSelectionProvider = async ({
4342
let tenantCookie = cookies.get('payload-tenant')?.value
4443
let initialValue = undefined
4544

46-
if (tenantOptions.length > 1 && tenantCookie === SELECT_ALL) {
47-
initialValue = SELECT_ALL
48-
} else {
45+
/**
46+
* Ensure the cookie is a valid tenant
47+
*/
48+
if (tenantCookie) {
4949
const matchingOption = tenantOptions.find((option) => String(option.value) === tenantCookie)
5050
if (matchingOption) {
5151
initialValue = matchingOption.value
52-
} else {
53-
tenantCookie = undefined
54-
initialValue = tenantOptions.length > 1 ? SELECT_ALL : tenantOptions[0]?.value
5552
}
5653
}
5754

55+
/**
56+
* If the there was no cookie or the cookie was an invalid tenantID set intialValue
57+
*/
58+
if (!initialValue) {
59+
tenantCookie = undefined
60+
initialValue = tenantOptions.length > 1 ? undefined : tenantOptions[0]?.value
61+
}
62+
5863
return (
5964
<TenantSelectionProviderClient
6065
initialValue={initialValue}

packages/plugin-multi-tenant/src/utilities/getGlobalViewRedirect.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Payload, User, ViewTypes } from 'payload'
22

3-
import { SELECT_ALL } from '../constants.js'
43
import { findTenantOptions } from '../queries/findTenantOptions.js'
54
import { getCollectionIDType } from './getCollectionIDType.js'
65
import { getTenantFromCookie } from './getTenantFromCookie.js'
@@ -34,7 +33,7 @@ export async function getGlobalViewRedirect({
3433
let tenant = getTenantFromCookie(headers, idType)
3534
let redirectRoute
3635

37-
if (!tenant || tenant === SELECT_ALL) {
36+
if (!tenant) {
3837
const tenantsQuery = await findTenantOptions({
3938
limit: 1,
4039
payload,

0 commit comments

Comments
 (0)