Skip to content

Commit

Permalink
fix(ui): avoid calling getTableState from join field on create (#9256)
Browse files Browse the repository at this point in the history
### 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
  • Loading branch information
r1tsuu authored Nov 17, 2024
1 parent d21fca9 commit ef2475d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
6 changes: 4 additions & 2 deletions packages/ui/src/elements/RelationshipTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const baseClass = 'relationship-table'

type RelationshipTableComponentProps = {
readonly allowCreate?: boolean
readonly disableTable?: boolean
readonly field: JoinFieldClient
readonly filterOptions?: Where
readonly initialData?: PaginatedDocs
Expand All @@ -50,6 +51,7 @@ type RelationshipTableComponentProps = {
export const RelationshipTable: React.FC<RelationshipTableComponentProps> = (props) => {
const {
allowCreate = true,
disableTable = false,
filterOptions,
initialData: initialDataFromProps,
initialDrawerData,
Expand Down Expand Up @@ -132,11 +134,11 @@ export const RelationshipTable: React.FC<RelationshipTableComponentProps> = (pro

useIgnoredEffect(
() => {
if (!Table || query) {
if (!disableTable && (!Table || query)) {
void renderTable()
}
},
[query],
[query, disableTable],
[Table, renderTable],
)

Expand Down
9 changes: 7 additions & 2 deletions packages/ui/src/fields/Join/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ const JoinFieldComponent: JoinFieldClientComponent = (props) => {
path,
})

const filterOptions: Where = useMemo(() => {
const filterOptions: null | Where = useMemo(() => {
if (!docID) {
return null
}

const where = {
[on]: {
in: [docID || ''],
equals: docID,
},
}

Expand All @@ -54,6 +58,7 @@ const JoinFieldComponent: JoinFieldClientComponent = (props) => {
{BeforeInput}
<RelationshipTable
allowCreate={typeof docID !== 'undefined' && allowCreate}
disableTable={filterOptions === null}
field={field as JoinFieldClient}
filterOptions={filterOptions}
initialData={docID && value ? value : ({ docs: [] } as PaginatedDocs)}
Expand Down
15 changes: 14 additions & 1 deletion test/joins/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { reorderColumns } from 'helpers/e2e/reorderColumns.js'
import * as path from 'path'
import { fileURLToPath } from 'url'

import { ensureCompilationIsDone, exactText, initPageConsoleErrorCatch } from '../helpers.js'
import {
ensureCompilationIsDone,
exactText,
initPageConsoleErrorCatch,
saveDocAndAssert,
} from '../helpers.js'
import { AdminUrlUtil } from '../helpers/adminUrlUtil.js'
import { navigateToDoc } from '../helpers/e2e/navigateToDoc.js'
import { initPayloadE2ENoConfig } from '../helpers/initPayloadE2ENoConfig.js'
Expand Down Expand Up @@ -57,6 +62,14 @@ test.describe('Admin Panel', () => {
expect(columns).toBe(3)
})

test('should render the create page and create doc with the join field', async () => {
await page.goto(categoriesURL.create)
const nameField = page.locator('#field-name')
await expect(nameField).toBeVisible()
await nameField.fill('test category')
await saveDocAndAssert(page)
})

test('should render collection type in first column of relationship table', async () => {
await navigateToDoc(page, categoriesURL)
const joinField = page.locator('.field-type.join').first()
Expand Down

0 comments on commit ef2475d

Please sign in to comment.