Skip to content

Commit ef2475d

Browse files
authored
fix(ui): avoid calling getTableState from join field on create (#9256)
### What? Fixes the issue when visiting the create view with the Join Field and using postgres adapter ``` invalid input syntax for type integer: "NaN" ``` This happens because we don't have an ID yet and we send to the database: `WHERE id = NaN` ### How? Avoids calling `getTableState` inside of `RelationshipTable` if there's no ID yet, as it will always lead to the same empty result. While we _could_ avoid error directly in the database adapter, I don't think we should do that render request Fixes #9193
1 parent d21fca9 commit ef2475d

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

packages/ui/src/elements/RelationshipTable/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const baseClass = 'relationship-table'
3939

4040
type RelationshipTableComponentProps = {
4141
readonly allowCreate?: boolean
42+
readonly disableTable?: boolean
4243
readonly field: JoinFieldClient
4344
readonly filterOptions?: Where
4445
readonly initialData?: PaginatedDocs
@@ -50,6 +51,7 @@ type RelationshipTableComponentProps = {
5051
export const RelationshipTable: React.FC<RelationshipTableComponentProps> = (props) => {
5152
const {
5253
allowCreate = true,
54+
disableTable = false,
5355
filterOptions,
5456
initialData: initialDataFromProps,
5557
initialDrawerData,
@@ -132,11 +134,11 @@ export const RelationshipTable: React.FC<RelationshipTableComponentProps> = (pro
132134

133135
useIgnoredEffect(
134136
() => {
135-
if (!Table || query) {
137+
if (!disableTable && (!Table || query)) {
136138
void renderTable()
137139
}
138140
},
139-
[query],
141+
[query, disableTable],
140142
[Table, renderTable],
141143
)
142144

packages/ui/src/fields/Join/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ const JoinFieldComponent: JoinFieldClientComponent = (props) => {
3333
path,
3434
})
3535

36-
const filterOptions: Where = useMemo(() => {
36+
const filterOptions: null | Where = useMemo(() => {
37+
if (!docID) {
38+
return null
39+
}
40+
3741
const where = {
3842
[on]: {
39-
in: [docID || ''],
43+
equals: docID,
4044
},
4145
}
4246

@@ -54,6 +58,7 @@ const JoinFieldComponent: JoinFieldClientComponent = (props) => {
5458
{BeforeInput}
5559
<RelationshipTable
5660
allowCreate={typeof docID !== 'undefined' && allowCreate}
61+
disableTable={filterOptions === null}
5762
field={field as JoinFieldClient}
5863
filterOptions={filterOptions}
5964
initialData={docID && value ? value : ({ docs: [] } as PaginatedDocs)}

test/joins/e2e.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { reorderColumns } from 'helpers/e2e/reorderColumns.js'
55
import * as path from 'path'
66
import { fileURLToPath } from 'url'
77

8-
import { ensureCompilationIsDone, exactText, initPageConsoleErrorCatch } from '../helpers.js'
8+
import {
9+
ensureCompilationIsDone,
10+
exactText,
11+
initPageConsoleErrorCatch,
12+
saveDocAndAssert,
13+
} from '../helpers.js'
914
import { AdminUrlUtil } from '../helpers/adminUrlUtil.js'
1015
import { navigateToDoc } from '../helpers/e2e/navigateToDoc.js'
1116
import { initPayloadE2ENoConfig } from '../helpers/initPayloadE2ENoConfig.js'
@@ -57,6 +62,14 @@ test.describe('Admin Panel', () => {
5762
expect(columns).toBe(3)
5863
})
5964

65+
test('should render the create page and create doc with the join field', async () => {
66+
await page.goto(categoriesURL.create)
67+
const nameField = page.locator('#field-name')
68+
await expect(nameField).toBeVisible()
69+
await nameField.fill('test category')
70+
await saveDocAndAssert(page)
71+
})
72+
6073
test('should render collection type in first column of relationship table', async () => {
6174
await navigateToDoc(page, categoriesURL)
6275
const joinField = page.locator('.field-type.join').first()

0 commit comments

Comments
 (0)