Files
baserow/e2e-tests/fixtures/database/view.ts
Davide Silvestri 89184e9b27 fix: stop editing rows hidden by restricted view filters (#6217)
* fix: stop editing rows hidden by restricted view filters

Cancel edits still queued for a row that the backend reported as hidden,
including pasted values, in grid, gallery, timeline, kanban and calendar,
so they are neither sent nor put back on screen. Close the row modal when
its row gets hidden in a restricted view; other views keep it open.

* test: cover builders and ignore broadcast order in restricted view tests

* test: add e2e tests for restricted view row visibility

Cover creating, editing, pasting and dragging rows in a restricted view
while the HTTP response is held after the server commits, the row modal
closing only in restricted views, and view default values.

* fix: hide rows created with the grid's floating add button when filtered out

createNewRowConfirmed used the single-row endpoint, which doesn't return
hidden_row_ids, so a restricted editor kept a row the view's filters
exclude. Create it through the batch endpoint and report hidden rows like
the other create paths.

* test: expect the batch endpoint for the grid's floating add button

* fix: cancel queued kanban edits when a drag hides the card
2026-10-08 09:23:06 +02:00

279 lines
7.0 KiB
TypeScript

import { getClient } from "../../client";
import { User } from "../user";
import { Table } from "./table";
import { Field } from "./field";
export class View {
constructor(
public id: number,
public name: string,
public type: string,
public slug: string,
public table: Table,
) {}
}
/**
* Creates a form view. With `isPublic` it is shared so `/form/{slug}` is
* reachable without authentication, the way the e2e tests open it.
*/
export async function createFormView(
user: User,
table: Table,
{
isPublic = true,
name = "Form",
}: { isPublic?: boolean; name?: string } = {},
): Promise<View> {
const response: any = await getClient(user).post(
`database/views/table/${table.id}/`,
{ name, type: "form" },
);
let data = response.data;
if (isPublic && !data.public) {
const patched: any = await getClient(user).patch(
`database/views/${data.id}/`,
{ public: true },
);
data = patched.data;
}
return new View(data.id, data.name, data.type, data.slug, table);
}
/**
* One condition controlling a field's visibility: `field` is the field it
* reads, `type` is a view filter type like "not_empty", and `value` is the
* comparison value (blank for empty/not_empty).
*/
export type FormFieldCondition = {
field: number;
type: string;
value?: string;
};
export type FormFieldOptions = {
enabled?: boolean;
required?: boolean;
order?: number;
name?: string;
description?: string;
show_when_matching_conditions?: boolean;
condition_type?: "AND" | "OR";
conditions?: FormFieldCondition[];
};
/**
* Updates the form field options keyed by field id, like the
* `PATCH database/views/{id}/field-options/` request the frontend sends.
*
* Each condition needs an `id`; the backend reads an unknown id as a new
* condition, so we assign temporary ids here and callers can omit them.
*/
export async function updateFormFieldOptions(
user: User,
view: View,
fieldOptions: Record<number, FormFieldOptions>,
): Promise<void> {
let tempConditionId = 0;
const payload: Record<number, any> = {};
for (const [fieldId, options] of Object.entries(fieldOptions)) {
payload[Number(fieldId)] = {
...options,
...(options.conditions
? {
conditions: options.conditions.map((condition) => ({
id: (tempConditionId -= 1),
value: "",
...condition,
})),
}
: {}),
};
}
await getClient(user).patch(`database/views/${view.id}/field-options/`, {
field_options: payload,
});
}
/**
* Configure a column's "Summarize" aggregation on a grid view, the same
* field-options PATCH the footer/group-header picker sends.
*/
export async function setViewFieldAggregation(
user: User,
view: View,
field: Field,
aggregationType: string,
aggregationRawType: string,
): Promise<void> {
await getClient(user).patch(`database/views/${view.id}/field-options/`, {
field_options: {
[field.id]: {
aggregation_type: aggregationType,
aggregation_raw_type: aggregationRawType,
},
},
});
}
export async function getDefaultGridView(
user: User,
table: Table,
): Promise<View> {
const response: any = await getClient(user).get(
`database/views/table/${table.id}/`,
);
const gridView = response.data.find((view: any) => view.type === "grid");
return new View(
gridView.id,
gridView.name,
gridView.type,
gridView.slug,
table,
);
}
export async function createGridView(
user: User,
table: Table,
{ name = "Grid" }: { name?: string } = {},
): Promise<View> {
const response: any = await getClient(user).post(
`database/views/table/${table.id}/`,
{ name, type: "grid" },
);
const data = response.data;
const view = new View(data.id, data.name, data.type, data.slug, table);
await ensureViewFieldOptions(user, view);
return view;
}
export async function createGalleryView(
user: User,
table: Table,
{ name = "Gallery" }: { name?: string } = {},
): Promise<View> {
const response: any = await getClient(user).post(
`database/views/table/${table.id}/`,
{ name, type: "gallery" },
);
const data = response.data;
const view = new View(data.id, data.name, data.type, data.slug, table);
await ensureViewFieldOptions(user, view);
return view;
}
export async function ensureViewFieldOptions(
user: User,
view: View,
): Promise<void> {
await getClient(user).get(`database/views/${view.id}/field-options/`);
}
export async function createViewFilter(
user: User,
view: View,
field: Field,
type: string,
value: string,
): Promise<void> {
await getClient(user).post(`database/views/${view.id}/filters/`, {
field: field.id,
type,
value,
});
}
export async function createViewSort(
user: User,
view: View,
field: Field,
order: "ASC" | "DESC" = "ASC",
): Promise<void> {
await getClient(user).post(`database/views/${view.id}/sortings/`, {
field: field.id,
order,
});
}
export async function createViewGroupBy(
user: User,
view: View,
field: Field,
order: "ASC" | "DESC" = "ASC",
): Promise<void> {
await getClient(user).post(`database/views/${view.id}/group_bys/`, {
field: field.id,
order,
});
}
export interface ViewDecorationPayload {
type: string;
value_provider_type?: string;
value_provider_conf?: Record<string, unknown>;
order?: number;
}
export async function patchView(
user: User,
view: View,
values: Record<string, unknown>,
): Promise<void> {
await getClient(user).patch(`database/views/${view.id}/`, values);
}
export async function createViewDecoration(
user: User,
view: View,
values: ViewDecorationPayload,
): Promise<void> {
await getClient(user).post(`database/views/${view.id}/decorations/`, values);
}
/** Creates a restricted view: editors only see and receive the rows its filters match. */
export async function createRestrictedView(
user: User,
table: Table,
{
name,
type = "grid",
settings = {},
}: { name: string; type?: string; settings?: Record<string, unknown> },
): Promise<View> {
const response: any = await getClient(user).post(
`database/views/table/${table.id}/`,
{ name, type, ownership_type: "restricted", ...settings },
);
const data = response.data;
const view = new View(data.id, data.name, data.type, data.slug, table);
await ensureViewFieldOptions(user, view);
return view;
}
export async function updateViewFieldOptions(
user: User,
view: View,
fieldOptions: Record<number, Record<string, unknown>>,
): Promise<void> {
await getClient(user).patch(`database/views/${view.id}/field-options/`, {
field_options: fieldOptions,
});
}
/** Sets the values new rows created in the view start with. */
export async function setViewDefaultValues(
user: User,
view: View,
defaults: { field: Field; value: unknown }[],
): Promise<void> {
await getClient(user).patch(
`database/views/${view.id}/default-values/`,
defaults.map(({ field, value }) => ({
field: field.id,
enabled: true,
value,
})),
);
}