Skip to content
Merged
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
11 changes: 11 additions & 0 deletions .changeset/37290-view-all-new-dependson-seed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@object-ui/app-shell': patch
'@object-ui/console': patch
---

fix(console): New on a list opened from a related list's "View all" seeds the relationship lookup's `dependsOn` fields, as the related tab's New does

Clause-②: no
Metadata-protocol: none

A related list's "View all" opens the child list under the parent record (the record trail). Its New opened the child's create form with the parent link alone, so a relationship lookup that declares `dependsOn` (an order `dependsOn` the account) drew gated ("Select Account first") with the parent hidden, or was emptied by a `lookupAutofill`. It now opens with the same seed the related tab's New writes (`relatedCreateSeedOf`): the parent pair first, then one `formLink` pair per `dependsOn` field, valued from the parent record. The trail holds the parent's id alone, so the page reads the parent record once, and only when the relationship lookup declares a `dependsOn`; a lookup without one, or a parent that cannot be read, opens with the parent link alone, as before. While the parent is read, New is disabled; a read that has not answered within 5 seconds opens the form with the parent link alone, and a read that answers after the page moved on (another filter, another page) opens nothing.
57 changes: 53 additions & 4 deletions ui/packages/app-shell/src/views/ObjectDataPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import * as React from 'react';
import { Link, useParams, useNavigate, useSearchParams, useLocation } from 'react-router-dom';
import { ListView, conditionCandidatesOf, conditionOptionsOf, useLookupValueNames } from '@object-ui/plugin-list';
import { relatedCreateSeedOf } from '@object-ui/plugin-detail/core';
import { useNavigationOverlay, useRowPredicate } from '@object-ui/react';
import {
Button,
Expand All @@ -55,7 +56,7 @@ import { formatMetadataError } from '@object-ui/data-objectstack';
import { useObjectTranslation, useObjectLabel } from '@object-ui/i18n';
import { usePermissions, useFieldPermissions } from '@object-ui/permissions';
import { useAuth, useWorkspaceAdminStatus } from '@object-ui/auth';
import { optionDisplayLabel, resolveAffordance, resolveFilterPlaceholders, type SchemaLike } from '@object-ui/core';
import { optionDisplayLabel, parentRelationshipFieldDef, resolveAffordance, resolveFilterPlaceholders, type SchemaLike } from '@object-ui/core';
import { normalizeFilterOperator, ViewFilterRuleSchema } from '@objectstack/spec/ui';
import type { ViewFilterRule } from '@objectstack/spec/ui';
import {
Expand Down Expand Up @@ -323,6 +324,13 @@ function addressPanelFilterConditionCount(searchParams: URLSearchParams): number
return Array.isArray(conditions) ? conditions.length : 0;
}

/**
* objectstack#37290 — how long New under a record trail waits for the parent
* read its `dependsOn` seed needs. Past it the form opens with the parent link
* alone, as it does when the read is refused or finds nothing.
*/
export const TRAIL_NEW_PARENT_READ_TIMEOUT_MS = 5000;

export function ObjectDataPage({ dataSource, objects }: any) {
const { appName, objectName } = useParams();
const { t, language } = useObjectTranslation();
Expand Down Expand Up @@ -907,6 +915,12 @@ export function ObjectDataPage({ dataSource, objects }: any) {
label: 'builtin:create:disabledWhen',
});
const createDisabled = createPredicates?.disabledWhen != null && createDisabledPred;
// objectstack#37290 — New under a record trail may read the parent first
// (the `dependsOn` seed). While it does, New is pending; a click's result is
// dropped when the page unmounted or the address moved on before it settled.
const [newPending, setNewPending] = React.useState(false);
const newClickRef = React.useRef(0);
React.useEffect(() => () => { newClickRef.current += 1; }, []);

if (!objectDef) {
return (
Expand Down Expand Up @@ -961,13 +975,48 @@ export function ObjectDataPage({ dataSource, objects }: any) {
const sp = new URLSearchParams(searchParams);
sp.set(RECORD_FORM_PARAM, 'new');
sp.set(RECORD_FORM_OBJECT_PARAM, objectDef.name);
sp.set(RECORD_FORM_LINK_PARAM, `${sourceField}:${source.i}`);
setSearchParams(sp);
sp.delete(RECORD_FORM_LINK_PARAM);
sp.append(RECORD_FORM_LINK_PARAM, `${sourceField}:${source.i}`);
// objectstack#37290 — and seeded as the related tab's New seeds it
// (objectstack#37246): each `dependsOn` field of the link takes the
// source record's value, or the form gates the lookup. The trail
// carries the record's id alone, so the record is read once, and
// only when the link declares a `dependsOn` to seed.
const dependsOn = (parentRelationshipFieldDef(objectDef.fields, sourceField) as { dependsOn?: unknown } | undefined)?.dependsOn;
const open = (parentRecord?: Record<string, unknown>) => {
for (const [field, value] of Object.entries(relatedCreateSeedOf(objectDef.fields, sourceField, parentRecord))) {
sp.append(RECORD_FORM_LINK_PARAM, `${field}:${value}`);
}
setSearchParams(sp);
};
if (Array.isArray(dependsOn) && dependsOn.length > 0 && typeof dataSource?.findOne === 'function') {
const click = ++newClickRef.current;
const at = `${location.pathname}${location.search}`;
setNewPending(true);
let timer: ReturnType<typeof setTimeout> | undefined;
const timedOut = new Promise<undefined>((resolve) => {
timer = setTimeout(() => resolve(undefined), TRAIL_NEW_PARENT_READ_TIMEOUT_MS);
});
const read = Promise.resolve()
.then(() => dataSource.findOne(source.o, source.i))
.catch(() => undefined);
void Promise.race([read, timedOut]).then((rec: unknown) => {
clearTimeout(timer);
if (click !== newClickRef.current) return;
setNewPending(false);
const now = locationRef.current;
if (`${now.pathname}${now.search}` !== at) return;
open(rec && typeof rec === 'object' ? (rec as Record<string, unknown>) : undefined);
});
return;
}
open(undefined);
return;
}
navigate('../new', { relative: 'path' });
}}
disabled={createDisabled}
disabled={createDisabled || newPending}
aria-busy={newPending || undefined}
className="shadow-none gap-1.5 h-8 sm:h-9"
data-testid="object-data-new-button"
>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* objectstack#37290 — New on a list opened from a related list's "View all"
* (the record trail, objectstack#35601) seeds the child as the related tab's
* New does (objectstack#37246, `relatedCreateSeedOf`): beside the parent pair,
* each `dependsOn` field of the link takes the source record's value. The
* trail holds the record's id alone, so the page reads the record once.
*
* Control: a link with no `dependsOn` writes the parent pair and reads nothing.
* A read refused, empty or past `TRAIL_NEW_PARENT_READ_TIMEOUT_MS` opens the
* form with the parent pair alone; New is pending while the read runs; a read
* that settles after the address moved on opens nothing.
*/

import * as React from 'react';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, cleanup, fireEvent, screen, waitFor, act } from '@testing-library/react';
import { MemoryRouter, Routes, Route, useLocation, useNavigate } from 'react-router-dom';
// The page reaches `@objectstack/lint` through a dynamic import; its first load is paid at collection, not inside a case.
import '@objectstack/lint';

vi.mock('@object-ui/permissions', async (importOriginal) => {
const actual = await importOriginal<typeof import('@object-ui/permissions')>();
return {
...actual,
usePermissions: () => ({
check: () => ({ allowed: true }),
checkField: () => true,
getFieldPermissions: () => [],
getRowFilter: () => undefined,
getObjectApiOperations: () => undefined,
roles: [],
isLoaded: false,
hasCapabilities: () => true,
can: () => true,
cannot: () => false,
}),
useFieldPermissions: () => ({ canRead: () => true, canWrite: () => true, permissions: [] }),
};
});

vi.mock('@object-ui/auth', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useAuth: () => ({ user: { id: 'u1', name: 'Ada' }, activeOrganization: null }),
useWorkspaceAdminStatus: () => ({ isAdmin: false, isResolved: true }),
createAuthenticatedFetch: () => vi.fn(),
}));

// Heavy children, all orthogonal to the New path under test and each dragging in
// a plugin bundle — the same posture as the sibling create-affordance test on
// this page.
vi.mock('@object-ui/plugin-list', async (importOriginal) => ({
...(await importOriginal<typeof import('@object-ui/plugin-list')>()),
ListView: () => null,
}));
vi.mock('./RecordDetailView', () => ({ RecordDetailView: () => null }));
vi.mock('./CreateViewDialog', () => ({ CreateViewDialog: () => null }));
vi.mock('./metadata-admin/useMetadata', () => ({ useMetadataClient: () => ({}) }));

import { ObjectDataPage, TRAIL_NEW_PARENT_READ_TIMEOUT_MS } from './ObjectDataPage';
import { ExpressionProvider } from '../providers/ExpressionProvider';
import { RECORD_TRAIL_PARAM } from '../urlParams';

const h = React.createElement;

const OBJECTS = [
{
name: 'contract',
label: 'Contract',
pluralLabel: 'Contracts',
managedBy: 'platform',
fields: {
id: { type: 'text', label: 'Id' },
account: { type: 'lookup', reference: 'account', label: 'Account' },
opportunity: { type: 'lookup', reference: 'opportunity', label: 'Opportunity', dependsOn: ['account'] },
},
},
{
name: 'task',
label: 'Task',
pluralLabel: 'Tasks',
managedBy: 'platform',
fields: {
id: { type: 'text', label: 'Id' },
opportunity: { type: 'lookup', reference: 'opportunity', label: 'Opportunity' },
},
},
{ name: 'opportunity', label: 'Opportunity', pluralLabel: 'Opportunities', managedBy: 'platform', fields: { name: { type: 'text', label: 'Name' }, account: { type: 'lookup', reference: 'account', label: 'Account' } } },
{ name: 'account', label: 'Account', pluralLabel: 'Accounts', managedBy: 'platform', fields: { name: { type: 'text', label: 'Name' } } },
];

const TRAIL = encodeURIComponent(JSON.stringify([{ o: 'opportunity', i: 'O-1', t: 'Big deal' }]));

const readParent = async (object: string, id: string) =>
(object === 'opportunity' && id === 'O-1' ? { id: 'O-1', name: 'Big deal', account: 'A-1' } : null);

function makeDataSource(findOne: (object: string, id: string) => Promise<unknown> = readParent) {
return {
find: vi.fn(async () => ({ data: [], total: 0 })),
findOne: vi.fn(findOne),
create: vi.fn(async () => ({})),
update: vi.fn(async () => ({})),
delete: vi.fn(async () => ({})),
} as any;
}

function Probe() {
const location = useLocation();
return h('output', { 'data-testid': 'probe' }, `${location.pathname}${location.search}`);
}

/** Moves the address on (a filter change made while New's read is pending). */
function Mover() {
const navigate = useNavigate();
const location = useLocation();
return h('button', { 'data-testid': 'move', onClick: () => navigate(`${location.pathname}${location.search}&moved=1`) }, 'move');
}

function renderAt(objectName: string, dataSource: any) {
return render(
h(ExpressionProvider, {
user: { id: 'u1', name: 'Ada', profile: 'admin' },
children: h(
MemoryRouter,
{ initialEntries: [`/apps/demo/${objectName}/data?filter[opportunity]=O-1&${RECORD_TRAIL_PARAM}=${TRAIL}`] },
h(Routes, null, h(Route, {
path: '/apps/:appName/:objectName/data',
element: h(React.Fragment, null, h(ObjectDataPage, { dataSource, objects: OBJECTS }), h(Probe), h(Mover)),
})),
),
}),
);
}

const params = () => new URLSearchParams((screen.getByTestId('probe').textContent ?? '').split('?')[1] ?? '');
const newButton = () => document.querySelector('[data-testid="object-data-new-button"]') as HTMLButtonElement;
const clickNew = () => fireEvent.click(newButton());

beforeEach(() => {
cleanup();
vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({ data: [] }), {
status: 200,
headers: { 'content-type': 'application/json' },
})));
});

afterEach(() => {
vi.useRealTimers();
vi.unstubAllGlobals();
vi.clearAllMocks();
});

describe('New from a record trail seeds the dependsOn fields (objectstack#37290)', () => {
it('opens the child form with the parent pair, then the dependsOn value read from the source record', async () => {
const ds = makeDataSource();
renderAt('contract', ds);
clickNew();
await waitFor(() => expect(params().get('form')).toBe('new'));
expect(params().get('formObject')).toBe('contract');
expect(params().getAll('formLink')).toEqual(['opportunity:O-1', 'account:A-1']);
expect(ds.findOne).toHaveBeenCalledTimes(1);
expect(ds.findOne).toHaveBeenCalledWith('opportunity', 'O-1');
});

it('CONTROL — a link with no dependsOn writes the parent pair alone and reads nothing', async () => {
const ds = makeDataSource();
renderAt('task', ds);
clickNew();
await waitFor(() => expect(params().get('form')).toBe('new'));
expect(params().getAll('formLink')).toEqual(['opportunity:O-1']);
expect(ds.findOne).not.toHaveBeenCalledWith('opportunity', 'O-1');
});

it('a refused parent read opens the form with the parent pair alone', async () => {
const ds = makeDataSource(async () => { throw Object.assign(new Error('forbidden'), { status: 403 }); });
renderAt('contract', ds);
clickNew();
await waitFor(() => expect(params().get('form')).toBe('new'));
expect(params().getAll('formLink')).toEqual(['opportunity:O-1']);
expect(ds.findOne).toHaveBeenCalledWith('opportunity', 'O-1');
expect(newButton().disabled).toBe(false);
});

it('a parent read that finds nothing opens the form with the parent pair alone', async () => {
const ds = makeDataSource(async () => null);
renderAt('contract', ds);
clickNew();
await waitFor(() => expect(params().get('form')).toBe('new'));
expect(params().getAll('formLink')).toEqual(['opportunity:O-1']);
expect(ds.findOne).toHaveBeenCalledWith('opportunity', 'O-1');
});

it('New is pending while the read runs, and a read that never settles opens the parent pair alone after the bound', async () => {
vi.useFakeTimers({ shouldAdvanceTime: true });
const ds = makeDataSource(() => new Promise(() => {}));
renderAt('contract', ds);
clickNew();
await waitFor(() => expect(newButton().disabled).toBe(true));
expect(newButton().getAttribute('aria-busy')).toBe('true');
expect(params().get('form')).toBeNull();
await vi.advanceTimersByTimeAsync(TRAIL_NEW_PARENT_READ_TIMEOUT_MS);
await waitFor(() => expect(params().get('form')).toBe('new'));
expect(params().getAll('formLink')).toEqual(['opportunity:O-1']);
expect(newButton().disabled).toBe(false);
});

it('a read that settles after the address moved on opens nothing and keeps the new address', async () => {
let settle: (rec: unknown) => void = () => {};
const ds = makeDataSource(() => new Promise((resolve) => { settle = resolve; }));
renderAt('contract', ds);
clickNew();
await waitFor(() => expect(newButton().disabled).toBe(true));
fireEvent.click(screen.getByTestId('move'));
await waitFor(() => expect(params().get('moved')).toBe('1'));
expect(newButton().disabled, 'still pending after the move (same mount)').toBe(true);
settle({ id: 'O-1', account: 'A-1' });
await waitFor(() => expect(newButton().disabled).toBe(false));
// A stale open would write the address after the pending flag clears: let it land before reading.
await act(async () => { await new Promise((resolve) => setTimeout(resolve, 50)); });
expect(params().get('form')).toBeNull();
expect(params().getAll('formLink')).toEqual([]);
expect(params().get('moved')).toBe('1');
});
});
Loading