@marktiderman/genesis-ui-native 3.13.0 → 3.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marktiderman/genesis-ui-native",
3
- "version": "3.13.0",
3
+ "version": "3.14.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -83,7 +83,7 @@
83
83
  "@expo/ui": ">=58.0.0",
84
84
  "@gorhom/bottom-sheet": ">=5.0.0",
85
85
  "@hookform/resolvers": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0",
86
- "@marktiderman/genesis-core": "^2.17.0",
86
+ "@marktiderman/genesis-core": "^2.18.0",
87
87
  "@marktiderman/genesis-design-system": "^1.13.0",
88
88
  "@react-native-community/datetimepicker": ">=8.0.0",
89
89
  "@shopify/flash-list": ">=2.0.0",
@@ -116,7 +116,11 @@ function useHeaderActions<T extends Row>({ view: recordView, row, onAction, can,
116
116
  const viewer = useViewer();
117
117
  const actions = recordView.headerFor(row as T & DataRow, {
118
118
  viewer,
119
- runnable: Boolean(onAction) || (Boolean(provider.runAction) && capabilities.data?.actions !== false),
119
+ // Not runnable until the provider has answered (an action run before then, or after a failed
120
+ // answer, would go without its version).
121
+ runnable:
122
+ Boolean(onAction) ||
123
+ (Boolean(provider.runAction) && (!provider.capabilities || capabilities.isSuccess) && capabilities.data?.actions !== false),
120
124
  ...(!onAction && described.data?.actions ? { served: described.data.actions.map((a) => a.name) } : {}),
121
125
  ...(can ? { can: (name: string) => can(name, row) } : {}),
122
126
  }).actions;
@@ -129,7 +133,16 @@ function useHeaderActions<T extends Row>({ view: recordView, row, onAction, can,
129
133
  setPending(true);
130
134
  try {
131
135
  if (onAction) await onAction(a.name, row);
132
- else if (provider.runAction) await provider.runAction(view.resource, { id: recordView.recordId(row as T & DataRow), action: a.name });
136
+ else if (provider.runAction) {
137
+ // At the version the record was read at, so a record changed since is a conflict.
138
+ const field = capabilities.data?.version?.field;
139
+ const value = field ? (row as Record<string, unknown>)[field] : undefined;
140
+ await provider.runAction(view.resource, {
141
+ id: recordView.recordId(row as T & DataRow),
142
+ action: a.name,
143
+ ...(value == null ? {} : { expectedVersion: String(value) }),
144
+ });
145
+ }
133
146
  else throw new DataWriteError("error", fill(words.noRunner, { name: a.label }));
134
147
  await queryClient.invalidateQueries({ queryKey: [...resourceKeys(view.resource).all] });
135
148
  } catch (error) {
@@ -21,7 +21,7 @@ import { SafeAreaView } from "react-native-safe-area-context";
21
21
  import type { BaseRecord, DataRow } from "@marktiderman/genesis-core";
22
22
  import { personTimeZone } from "@marktiderman/genesis-core/dates";
23
23
  import { resourceKeys, useDataProvider, useViewer } from "@marktiderman/genesis-core";
24
- import { scopeClock, useListState, useOne, useOptionalViewerScope } from "@marktiderman/genesis-core/hooks";
24
+ import { scopeClock, useCapabilities, useListState, useOne, useOptionalViewerScope } from "@marktiderman/genesis-core/hooks";
25
25
  import { useQueryClient } from "@tanstack/react-query";
26
26
  import type { GalleryItem } from "@marktiderman/genesis-core/gallery";
27
27
  import type { RowPicture, SavedViewStore, ViewGallery } from "@marktiderman/genesis-core/resource-view";
@@ -542,15 +542,29 @@ function Screen<T extends Row = Row>(props: NativeResourceViewProps<T>) {
542
542
  const provider = useDataProvider();
543
543
  const queryClient = useQueryClient();
544
544
  const viewer = useViewer();
545
+ // The provider runs this view's actions when it has a runner and doesn't say it serves none for
546
+ // this resource (`capabilities().actions === false`).
547
+ const askCaps = !onAction && !!provider.runAction && !!provider.capabilities;
548
+ const actionCaps = useCapabilities(view.resource, { enabled: askCaps });
545
549
  const runAction = useCallback(
546
550
  async (name: string, row: T) => {
547
551
  if (onAction) await onAction(name, row);
548
- else if (provider.runAction) await provider.runAction(view.resource, { id: recordKey(view as AnyResourceView<Row>, row), action: name });
552
+ else if (provider.runAction) {
553
+ // At the version the row was read at, so a row changed since is a conflict.
554
+ const field = actionCaps.data?.version?.field;
555
+ const value = field ? (row as Record<string, unknown>)[field] : undefined;
556
+ await provider.runAction(view.resource, {
557
+ id: recordKey(view as AnyResourceView<Row>, row),
558
+ action: name,
559
+ ...(value == null ? {} : { expectedVersion: String(value) }),
560
+ });
561
+ }
549
562
  await queryClient.invalidateQueries({ queryKey: resourceKeys(view.resource).all });
550
563
  },
551
- [onAction, provider, view, queryClient],
564
+ [onAction, provider, view, queryClient, actionCaps.data],
552
565
  );
553
- const actionsOn = !!onAction || !!provider.runAction;
566
+ // Not on until the provider has said (an action shown before then could run without its version).
567
+ const actionsOn = !!onAction || (!!provider.runAction && (!askCaps || actionCaps.isSuccess) && actionCaps.data?.actions !== false);
554
568
  const cancelWord = useNativeResourceViewMessages().common.cancel;
555
569
  const confirmAction = useCallback(
556
570
  (name: string, row: T) => {
@@ -843,7 +857,7 @@ function Screen<T extends Row = Row>(props: NativeResourceViewProps<T>) {
843
857
  ...(r.time ? { timestamp: r.time } : {}),
844
858
  ...(r.from || r.badge ? { meta: [r.from, r.badge?.label].filter(Boolean).join(" · ") } : {}),
845
859
  onPress: () => {
846
- if (r.unread && markRead && view.canAction(markRead, row as never, viewer) === true) void runAction(markRead, row).catch(() => undefined);
860
+ if (r.unread && markRead && actionsOn && view.canAction(markRead, row as never, viewer) === true) void runAction(markRead, row).catch(() => undefined);
847
861
  press(row);
848
862
  },
849
863
  // The view's swipe actions (edit, delete, its own), as the list's rows have them.
@@ -134,6 +134,20 @@ describe("NativeRecordView", () => {
134
134
  await waitFor(() => expect(onAction).toHaveBeenCalledWith("archive", { ...brand, is_default: false }));
135
135
  });
136
136
 
137
+ it("runs an action at the version the record was read at", async () => {
138
+ // @feat:declared-data-access
139
+ const sheet = vi.spyOn(ActionSheetIOS, "showActionSheetWithOptions").mockImplementation(() => {});
140
+ const p = provider({ capabilities: vi.fn(async () => ({ search: true, sort: true, filterOperators: [], bulkWrites: false, versionedWrites: true, compositeKeys: false, actions: true, version: { field: "name", kind: "timestamp" as const } })), });
141
+ render(<NativeRecordViewMenu view={page} row={brand} />, { wrapper: wrap(p) });
142
+ await waitFor(() => expect(p.capabilities).toHaveBeenCalled());
143
+ // Let the capabilities answer land before the click, as a person's would.
144
+ await new Promise((resolve) => setTimeout(resolve, 0));
145
+ fireEvent.click(screen.getByTestId("native-resource-workspace-menu"));
146
+ sheet.mock.calls[0]![1](1);
147
+ await waitFor(() => expect(p.runAction).toHaveBeenCalledWith("brands", { id: "b1", action: "publish", expectedVersion: "Northwind" }));
148
+ sheet.mockRestore();
149
+ });
150
+
137
151
  it("Android: the menu is a sheet of rows, never an alert with too many buttons", async () => {
138
152
  const os = Platform.OS;
139
153
  Platform.OS = "android";
@@ -180,6 +194,16 @@ describe("NativeRecordView", () => {
180
194
  await waitFor(() => expect(p.runAction).toHaveBeenCalledWith("brands", { id: "b1", action: "activate" }));
181
195
  });
182
196
 
197
+ it("offers no provider action until the provider's capabilities arrive", async () => {
198
+ // @feat:declared-data-access
199
+ let answer: (c: Awaited<ReturnType<NonNullable<DataProvider["capabilities"]>>>) => void = () => {};
200
+ const p = provider({ capabilities: vi.fn(() => new Promise((resolve) => (answer = resolve))) as DataProvider["capabilities"] });
201
+ render(<NativeRecordViewMenu view={page} row={brand} testID="late" />, { wrapper: wrap(p) });
202
+ expect(screen.queryByTestId("late")).toBeNull();
203
+ answer({ search: true, sort: true, filterOperators: [], bulkWrites: false, versionedWrites: false, compositeKeys: false, actions: true });
204
+ expect(await screen.findByTestId("late")).toBeTruthy();
205
+ });
206
+
183
207
  it("hides actions when nothing can run them, or the screen turns them off", () => {
184
208
  render(<NativeRecordViewMenu view={page} row={brand} testID="none" />, { wrapper: wrap(provider({ runAction: undefined })) });
185
209
  expect(screen.queryByTestId("none")).toBeNull();
@@ -189,6 +189,41 @@ describe("the inbox on the phone", () => {
189
189
  fireEvent.click(screen.getByText("Ship it"));
190
190
  expect(runAction).toHaveBeenCalledTimes(1);
191
191
  });
192
+
193
+ it("doesn't run markRead when the provider serves no actions for the resource", async () => {
194
+ // @feat:declared-data-access
195
+ const runAction = vi.fn(async () => ({ data: null }));
196
+ const capabilities = vi.fn(async () => ({ search: true, sort: true, filterOperators: [], bulkWrites: false, versionedWrites: false, compositeKeys: false, actions: false }));
197
+ const inbox = defineView(tasks, { inbox: { unread: "unread" }, actions: [{ name: "markRead", label: "Mark read" }] });
198
+ render(<NativeResourceView view={inbox} type="inbox" onRowPress={vi.fn()} />, { wrapper: wrap(provider({ runAction, capabilities: capabilities as DataProvider["capabilities"] })) });
199
+ await waitFor(() => expect(capabilities).toHaveBeenCalled());
200
+ await act(async () => undefined);
201
+ fireEvent.click(await screen.findByText("Write the plan"));
202
+ await act(async () => undefined);
203
+ expect(runAction).not.toHaveBeenCalled();
204
+ });
205
+
206
+ // A row pressed before the capabilities answer opens without being marked read: a press is one
207
+ // tap, with no open row to come back to, so the next press after the answer marks it.
208
+ it("runs markRead through the provider only once its capabilities answer, at the row's version", async () => {
209
+ // @feat:declared-data-access
210
+ const runAction = vi.fn(async () => ({ data: null }));
211
+ type Caps = Awaited<ReturnType<NonNullable<DataProvider["capabilities"]>>>;
212
+ const waiting: ((c: Caps) => void)[] = [];
213
+ const capabilities = vi.fn(() => new Promise<Caps>((resolve) => waiting.push(resolve))) as DataProvider["capabilities"];
214
+ const answer = (c: Caps) => waiting.splice(0).forEach((resolve) => resolve(c));
215
+ const inbox = defineView(tasks, { inbox: { unread: "unread" }, actions: [{ name: "markRead", label: "Mark read" }] });
216
+ render(<NativeResourceView view={inbox} type="inbox" onRowPress={vi.fn()} />, { wrapper: wrap(provider({ runAction, capabilities })) });
217
+ fireEvent.click(await screen.findByText("Write the plan"));
218
+ await act(async () => undefined);
219
+ expect(runAction).not.toHaveBeenCalled();
220
+ answer({ search: true, sort: true, filterOperators: [], bulkWrites: false, versionedWrites: true, compositeKeys: false, actions: true, version: { field: "title", kind: "timestamp" } });
221
+ // Once the answer has rendered, opening the row runs markRead at the row's version.
222
+ await waitFor(() => {
223
+ fireEvent.click(screen.getByText("Write the plan"));
224
+ expect(runAction).toHaveBeenCalledWith("tasks", { id: "a", action: "markRead", expectedVersion: "Write the plan" });
225
+ });
226
+ });
192
227
  });
193
228
 
194
229
  describe("review fixes on the phone", () => {