@verikit/vue 0.24.1 → 0.24.3

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.
Files changed (41) hide show
  1. package/README.md +10 -0
  2. package/dist/client/use-verikit-client.d.ts +16 -15
  3. package/dist/client/use-verikit-client.d.ts.map +1 -1
  4. package/dist/client/use-verikit-client.js +34 -18
  5. package/dist/form/use-verikit-schema-tree-form.d.ts +3 -1
  6. package/dist/form/use-verikit-schema-tree-form.d.ts.map +1 -1
  7. package/dist/form/use-verikit-schema-tree-form.js +8 -1
  8. package/dist/query/index.d.ts +2 -2
  9. package/dist/query/index.d.ts.map +1 -1
  10. package/dist/query/index.js +1 -1
  11. package/dist/query/use-resource-form.d.ts +3 -3
  12. package/dist/query/use-resource-form.d.ts.map +1 -1
  13. package/dist/query/use-resource-mutations.d.ts +4 -4
  14. package/dist/query/use-resource-mutations.d.ts.map +1 -1
  15. package/dist/query/use-resource-mutations.js +5 -4
  16. package/dist/query/use-resource-queries.d.ts +13 -7
  17. package/dist/query/use-resource-queries.d.ts.map +1 -1
  18. package/dist/query/use-resource-queries.js +16 -3
  19. package/dist/query/use-resource-schema-tree-form.d.ts +3 -3
  20. package/dist/query/use-resource-schema-tree-form.d.ts.map +1 -1
  21. package/dist/query/use-resource-table.d.ts +9 -3
  22. package/dist/query/use-resource-table.d.ts.map +1 -1
  23. package/dist/query/use-resource-table.js +4 -2
  24. package/dist/resource/declared-action-button.d.ts +14 -0
  25. package/dist/resource/declared-action-button.d.ts.map +1 -0
  26. package/dist/resource/declared-action-button.js +24 -0
  27. package/dist/resource/index.d.ts +4 -0
  28. package/dist/resource/index.d.ts.map +1 -1
  29. package/dist/resource/index.js +2 -0
  30. package/dist/resource/resource-action-dialog.d.ts +103 -0
  31. package/dist/resource/resource-action-dialog.d.ts.map +1 -0
  32. package/dist/resource/resource-action-dialog.js +207 -0
  33. package/dist/resource/resource-detail.d.ts +98 -0
  34. package/dist/resource/resource-detail.d.ts.map +1 -0
  35. package/dist/resource/resource-detail.js +258 -0
  36. package/dist/resource/resource-form.d.ts +1 -1
  37. package/dist/resource/resource-form.d.ts.map +1 -1
  38. package/dist/resource/resource-table.d.ts +13 -0
  39. package/dist/resource/resource-table.d.ts.map +1 -1
  40. package/dist/resource/resource-table.js +108 -35
  41. package/package.json +6 -6
@@ -0,0 +1,103 @@
1
+ import type { UseMutationReturnType } from "@tanstack/vue-query";
2
+ import type { ActionSchemaLike } from "@verikit/core";
3
+ import { type ActionResult } from "@verikit/client";
4
+ import { type PropType } from "vue";
5
+ import type { VerikitFieldRegistry } from "../fields/types.js";
6
+ /** True for an error whose `status` matches, duck-typed like `ResourceTable`. */
7
+ export declare function hasStatus(error: unknown, status: number): boolean;
8
+ /** True for an error whose `status` is 403, duck-typed like `ResourceTable`. */
9
+ export declare function isPermissionDenied(error: unknown): boolean;
10
+ /**
11
+ * True when the server denied running an action. The server denies record actions with 404 rather than 403 so it never reveals whether the record exists; the caller already holds that record, so a 404 there means the action can't run on it (denied, or the record is gone).
12
+ */
13
+ export declare function isActionDenied(error: unknown, recordId: string | undefined): boolean;
14
+ /** Variables for {@link useRunResourceAction}. */
15
+ export interface RunResourceActionVariables {
16
+ action: ActionSchemaLike;
17
+ recordId?: string;
18
+ input?: Record<string, unknown>;
19
+ }
20
+ /**
21
+ * Runs any declared action of a resource over `@verikit/client`, invalidating the resource's queries on success, or when the server reports the action unavailable. Sends `confirmed: true` for actions that declare a confirmation, since callers only run those after the user accepted it.
22
+ */
23
+ export declare function useRunResourceAction(resourceName: string, options?: {
24
+ onSuccess?: () => void;
25
+ onError?: (error: Error, variables: RunResourceActionVariables) => void;
26
+ }): UseMutationReturnType<ActionResult<unknown>, Error, RunResourceActionVariables, unknown>;
27
+ /** Props for {@link ResourceActionDialog}. */
28
+ export interface ResourceActionDialogProps {
29
+ /** Resource the action belongs to. */
30
+ resourceName: string;
31
+ /** Declared action to run; the dialog is open while this is set. */
32
+ action: ActionSchemaLike | null;
33
+ /** Record the action targets, for record-scoped actions. */
34
+ recordId?: string;
35
+ /** Called when the dialog closes, after success or cancel. */
36
+ onClose: () => void;
37
+ /** Called when the server denies the action (403, or 404 for a record action); the dialog then closes. */
38
+ onDenied?: (action: ActionSchemaLike) => void;
39
+ /** Optional renderer overrides for the action's form fields. */
40
+ registry?: Partial<VerikitFieldRegistry>;
41
+ }
42
+ /**
43
+ * Confirms and collects input for a declared resource action, then runs it over `@verikit/client`. Labels, confirmation copy, and form fields all come from the action's declaration in `defineResource({ actions })`.
44
+ */
45
+ export declare const ResourceActionDialog: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
46
+ resourceName: {
47
+ type: StringConstructor;
48
+ required: true;
49
+ };
50
+ action: {
51
+ type: PropType<ActionSchemaLike | null>;
52
+ default: null;
53
+ };
54
+ recordId: {
55
+ type: PropType<string | undefined>;
56
+ default: undefined;
57
+ };
58
+ onClose: {
59
+ type: PropType<() => void>;
60
+ required: true;
61
+ };
62
+ onDenied: {
63
+ type: PropType<ResourceActionDialogProps["onDenied"]>;
64
+ default: undefined;
65
+ };
66
+ registry: {
67
+ type: PropType<Partial<VerikitFieldRegistry> | undefined>;
68
+ default: undefined;
69
+ };
70
+ }>, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
71
+ [key: string]: any;
72
+ }>, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
73
+ resourceName: {
74
+ type: StringConstructor;
75
+ required: true;
76
+ };
77
+ action: {
78
+ type: PropType<ActionSchemaLike | null>;
79
+ default: null;
80
+ };
81
+ recordId: {
82
+ type: PropType<string | undefined>;
83
+ default: undefined;
84
+ };
85
+ onClose: {
86
+ type: PropType<() => void>;
87
+ required: true;
88
+ };
89
+ onDenied: {
90
+ type: PropType<ResourceActionDialogProps["onDenied"]>;
91
+ default: undefined;
92
+ };
93
+ registry: {
94
+ type: PropType<Partial<VerikitFieldRegistry> | undefined>;
95
+ default: undefined;
96
+ };
97
+ }>> & Readonly<{}>, {
98
+ registry: Partial<VerikitFieldRegistry> | undefined;
99
+ action: ActionSchemaLike | null;
100
+ recordId: string | undefined;
101
+ onDenied: ((action: ActionSchemaLike) => void) | undefined;
102
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
103
+ //# sourceMappingURL=resource-action-dialog.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resource-action-dialog.d.ts","sourceRoot":"","sources":["../../src/resource/resource-action-dialog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAEjE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EAAsB,KAAK,YAAY,EAAE,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAAsB,KAAK,QAAQ,EAAE,MAAM,KAAK,CAAC;AAgBxD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAG/D,iFAAiF;AACjF,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAOjE;AAED,gFAAgF;AAChF,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAE1D;AAWD;;GAEG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,MAAM,GAAG,SAAS,GAC3B,OAAO,CAKT;AAED,kDAAkD;AAClD,MAAM,WAAW,0BAA0B;IACzC,MAAM,EAAE,gBAAgB,CAAC;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,YAAY,EAAE,MAAM,EACpB,OAAO,GAAE;IACP,SAAS,CAAC,EAAE,MAAM,IAAI,CAAC;IACvB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,0BAA0B,KAAK,IAAI,CAAC;CACpE,GACL,qBAAqB,CACtB,YAAY,CAAC,OAAO,CAAC,EACrB,KAAK,EACL,0BAA0B,EAC1B,OAAO,CACR,CAwBA;AAED,8CAA8C;AAC9C,MAAM,WAAW,yBAAyB;IACxC,sCAAsC;IACtC,YAAY,EAAE,MAAM,CAAC;IACrB,oEAAoE;IACpE,MAAM,EAAE,gBAAgB,GAAG,IAAI,CAAC;IAChC,4DAA4D;IAC5D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8DAA8D;IAC9D,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,0GAA0G;IAC1G,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,gBAAgB,KAAK,IAAI,CAAC;IAC9C,gEAAgE;IAChE,QAAQ,CAAC,EAAE,OAAO,CAAC,oBAAoB,CAAC,CAAC;CAC1C;AAyID;;GAEG;AACH,eAAO,MAAM,oBAAoB;;;;;;cAKX,QAAQ,CAAC,gBAAgB,GAAG,IAAI,CAAC;;;;cAIjC,QAAQ,CAAC,MAAM,GAAG,SAAS,CAAC;;;;cAI1B,QAAQ,CAAC,MAAM,IAAI,CAAC;;;;cAIpB,QAAQ,CAAC,yBAAyB,CAAC,UAAU,CAAC,CAAC;;;;cAIjD,QAAQ,CAAC,OAAO,CAAC,oBAAoB,CAAC,GAAG,SAAS,CAAC;;;;;;;;;;;cAhBnD,QAAQ,CAAC,gBAAgB,GAAG,IAAI,CAAC;;;;cAIjC,QAAQ,CAAC,MAAM,GAAG,SAAS,CAAC;;;;cAI1B,QAAQ,CAAC,MAAM,IAAI,CAAC;;;;cAIpB,QAAQ,CAAC,yBAAyB,CAAC,UAAU,CAAC,CAAC;;;;cAIjD,QAAQ,CAAC,OAAO,CAAC,oBAAoB,CAAC,GAAG,SAAS,CAAC;;;;;;;wBApKnD,gBAAgB,KAAK,IAAI;4EA2M7C,CAAC"}
@@ -0,0 +1,207 @@
1
+ import { useMutation, useQueryClient } from "@tanstack/vue-query";
2
+ import { VerikitClientError } from "@verikit/client";
3
+ import { defineComponent, h } from "vue";
4
+ import { Button } from "#components/button";
5
+ import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "#components/dialog";
6
+ import { actionLabel } from "@verikit/ui-core/actions/resource-actions";
7
+ import { resourceQueryKeys } from "@verikit/ui-core/query/query-keys";
8
+ import { validationIssuesToFieldErrors } from "@verikit/ui-core/form/submission";
9
+ import { useVerikitClient } from "../client/use-verikit-client.js";
10
+ import { RenderField } from "../fields/registry.js";
11
+ import { useVerikitForm } from "../form/use-verikit-form.js";
12
+ /** True for an error whose `status` matches, duck-typed like `ResourceTable`. */
13
+ export function hasStatus(error, status) {
14
+ return (typeof error === "object" &&
15
+ error !== null &&
16
+ "status" in error &&
17
+ error.status === status);
18
+ }
19
+ /** True for an error whose `status` is 403, duck-typed like `ResourceTable`. */
20
+ export function isPermissionDenied(error) {
21
+ return hasStatus(error, 403);
22
+ }
23
+ function isActionUnavailable(error) {
24
+ return (typeof error === "object" &&
25
+ error !== null &&
26
+ "code" in error &&
27
+ error.code === "ACTION_UNAVAILABLE");
28
+ }
29
+ /**
30
+ * True when the server denied running an action. The server denies record actions with 404 rather than 403 so it never reveals whether the record exists; the caller already holds that record, so a 404 there means the action can't run on it (denied, or the record is gone).
31
+ */
32
+ export function isActionDenied(error, recordId) {
33
+ return (isPermissionDenied(error) ||
34
+ (recordId !== undefined && hasStatus(error, 404)));
35
+ }
36
+ /**
37
+ * Runs any declared action of a resource over `@verikit/client`, invalidating the resource's queries on success, or when the server reports the action unavailable. Sends `confirmed: true` for actions that declare a confirmation, since callers only run those after the user accepted it.
38
+ */
39
+ export function useRunResourceAction(resourceName, options = {}) {
40
+ const client = useVerikitClient();
41
+ const queryClient = useQueryClient();
42
+ const keys = resourceQueryKeys(resourceName, client);
43
+ return useMutation({
44
+ mutationFn: ({ action, recordId, input }) => client.resource(resourceName).action(action.name, input, {
45
+ recordId,
46
+ confirmed: action.confirmation ? true : undefined,
47
+ }),
48
+ onSuccess: () => {
49
+ void queryClient.invalidateQueries({ queryKey: keys.all });
50
+ options.onSuccess?.();
51
+ },
52
+ onError: (error, variables) => {
53
+ // The list's action availability is stale: refetch it so the action
54
+ // shows as unavailable.
55
+ if (isActionUnavailable(error)) {
56
+ void queryClient.invalidateQueries({ queryKey: keys.all });
57
+ }
58
+ options.onError?.(error, variables);
59
+ },
60
+ });
61
+ }
62
+ const ActionDialogBody = defineComponent({
63
+ name: "ResourceActionDialogBody",
64
+ props: {
65
+ resourceName: { type: String, required: true },
66
+ action: {
67
+ type: Object,
68
+ required: true,
69
+ },
70
+ recordId: {
71
+ type: String,
72
+ default: undefined,
73
+ },
74
+ onClose: {
75
+ type: Function,
76
+ required: true,
77
+ },
78
+ onDenied: {
79
+ type: Function,
80
+ default: undefined,
81
+ },
82
+ registry: {
83
+ type: Object,
84
+ default: undefined,
85
+ },
86
+ },
87
+ setup(props) {
88
+ const action = props.action;
89
+ const fields = action.form ?? {};
90
+ const run = useRunResourceAction(props.resourceName, {
91
+ onSuccess: () => props.onClose(),
92
+ onError: (error) => {
93
+ if (isActionDenied(error, props.recordId)) {
94
+ props.onDenied?.(action);
95
+ props.onClose();
96
+ }
97
+ else if (error instanceof VerikitClientError && error.issues) {
98
+ form.setFieldErrors(validationIssuesToFieldErrors(error.issues));
99
+ }
100
+ },
101
+ });
102
+ const form = useVerikitForm({
103
+ fields,
104
+ onSubmit: (input) => run.mutateAsync({ action, recordId: props.recordId, input }),
105
+ });
106
+ const description = action.confirmation?.message ?? action.description;
107
+ function handleSubmit(event) {
108
+ event.preventDefault();
109
+ event.stopPropagation();
110
+ // `run.error` already surfaces mutation failures; suppress the rejection here.
111
+ form.submit().catch(() => { });
112
+ }
113
+ return () => [
114
+ h(DialogHeader, {}, {
115
+ default: () => [
116
+ h(DialogTitle, {}, {
117
+ default: () => action.confirmation?.title ?? actionLabel(action),
118
+ }),
119
+ description
120
+ ? h(DialogDescription, {}, { default: () => description })
121
+ : null,
122
+ ],
123
+ }),
124
+ h("form", { class: "grid gap-4", onSubmit: handleSubmit, novalidate: true }, [
125
+ ...Object.keys(fields).map((name) => RenderField({
126
+ registry: props.registry,
127
+ ...form.getFieldProps(name),
128
+ })),
129
+ run.error.value && !isActionDenied(run.error.value, props.recordId)
130
+ ? h("p", { role: "alert", class: "text-sm text-destructive" }, run.error.value.message)
131
+ : null,
132
+ h(DialogFooter, {}, {
133
+ default: () => [
134
+ h(DialogClose, { asChild: true }, {
135
+ default: () => h(Button, { type: "button", variant: "outline" }, {
136
+ default: () => action.confirmation?.cancelLabel ?? "Cancel",
137
+ }),
138
+ }),
139
+ h(Button, {
140
+ type: "submit",
141
+ variant: action.variant === "danger" ? "destructive" : "default",
142
+ disabled: run.isPending.value,
143
+ }, {
144
+ default: () => run.isPending.value
145
+ ? "Running…"
146
+ : (action.confirmation?.confirmLabel ??
147
+ actionLabel(action)),
148
+ }),
149
+ ],
150
+ }),
151
+ ]),
152
+ ];
153
+ },
154
+ });
155
+ /**
156
+ * Confirms and collects input for a declared resource action, then runs it over `@verikit/client`. Labels, confirmation copy, and form fields all come from the action's declaration in `defineResource({ actions })`.
157
+ */
158
+ export const ResourceActionDialog = defineComponent({
159
+ name: "ResourceActionDialog",
160
+ props: {
161
+ resourceName: { type: String, required: true },
162
+ action: {
163
+ type: Object,
164
+ default: null,
165
+ },
166
+ recordId: {
167
+ type: String,
168
+ default: undefined,
169
+ },
170
+ onClose: {
171
+ type: Function,
172
+ required: true,
173
+ },
174
+ onDenied: {
175
+ type: Function,
176
+ default: undefined,
177
+ },
178
+ registry: {
179
+ type: Object,
180
+ default: undefined,
181
+ },
182
+ },
183
+ setup(props) {
184
+ return () => h(Dialog, {
185
+ open: props.action !== null,
186
+ onOpenChange: (open) => {
187
+ if (!open) {
188
+ props.onClose();
189
+ }
190
+ },
191
+ }, {
192
+ default: () => h(DialogContent, {}, {
193
+ default: () => props.action
194
+ ? h(ActionDialogBody, {
195
+ key: `${props.action.name}:${props.recordId ?? ""}`,
196
+ resourceName: props.resourceName,
197
+ action: props.action,
198
+ recordId: props.recordId,
199
+ onClose: props.onClose,
200
+ onDenied: props.onDenied,
201
+ registry: props.registry,
202
+ })
203
+ : null,
204
+ }),
205
+ });
206
+ },
207
+ });
@@ -0,0 +1,98 @@
1
+ import { type PropType, type VNodeChild } from "vue";
2
+ import type { Resource, ResourceSchema } from "@verikit/core";
3
+ type DetailRecord = Record<string, unknown>;
4
+ /** Props for {@link ResourceDetail}. */
5
+ export interface ResourceDetailProps {
6
+ /** The resource (or its schema) whose fields and actions the page shows. */
7
+ resource: Resource | ResourceSchema;
8
+ /** Id of the record to show. */
9
+ id: string;
10
+ /**
11
+ * Renders the built-in Edit (opens a `ResourceForm` dialog pre-filled from the record) and Delete (opens a confirmation dialog) actions. One the server denies (403) is hidden for the rest of this component's lifetime.
12
+ */
13
+ actions?: boolean;
14
+ /**
15
+ * Renders the record-scoped actions declared on the resource via `defineResource({ actions })`. The page asks the server which of them the actor can't run on this record: those it may not run are hidden, and those that can't run right now are disabled, with the reason as a tooltip. An action denied when run (403 or 404) is hidden for the rest of this component's lifetime. Defaults to `actions`.
16
+ */
17
+ resourceActions?: boolean;
18
+ /** Renders extra actions alongside the built-in and declared ones. */
19
+ renderActions?: (record: DetailRecord) => VNodeChild;
20
+ /**
21
+ * Called after the record is deleted through the built-in Delete action, e.g. to navigate away. The page shows a "deleted" message until then.
22
+ */
23
+ onDeleted?: (record: DetailRecord) => void;
24
+ /** Class name applied to the outer container. */
25
+ className?: string;
26
+ }
27
+ /**
28
+ * Shows one record's fields as a label/value list, with its actions: the built-in Edit and Delete, and the record-scoped actions declared on the resource. Fields marked `.hidden()`, or left out by the server because the actor can't read them, aren't shown. A record the actor can't read shows as not found, as the server doesn't reveal whether it exists.
29
+ */
30
+ export declare const ResourceDetail: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
31
+ resource: {
32
+ type: PropType<ResourceDetailProps["resource"]>;
33
+ required: true;
34
+ };
35
+ id: {
36
+ type: StringConstructor;
37
+ required: true;
38
+ };
39
+ actions: {
40
+ type: BooleanConstructor;
41
+ default: boolean;
42
+ };
43
+ resourceActions: {
44
+ type: PropType<boolean | undefined>;
45
+ default: undefined;
46
+ };
47
+ renderActions: {
48
+ type: PropType<ResourceDetailProps["renderActions"]>;
49
+ default: undefined;
50
+ };
51
+ onDeleted: {
52
+ type: PropType<ResourceDetailProps["onDeleted"]>;
53
+ default: undefined;
54
+ };
55
+ className: {
56
+ type: PropType<string | undefined>;
57
+ default: undefined;
58
+ };
59
+ }>, () => string | number | boolean | void | import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
60
+ [key: string]: any;
61
+ }> | import("vue").VNodeArrayChildren | null, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
62
+ resource: {
63
+ type: PropType<ResourceDetailProps["resource"]>;
64
+ required: true;
65
+ };
66
+ id: {
67
+ type: StringConstructor;
68
+ required: true;
69
+ };
70
+ actions: {
71
+ type: BooleanConstructor;
72
+ default: boolean;
73
+ };
74
+ resourceActions: {
75
+ type: PropType<boolean | undefined>;
76
+ default: undefined;
77
+ };
78
+ renderActions: {
79
+ type: PropType<ResourceDetailProps["renderActions"]>;
80
+ default: undefined;
81
+ };
82
+ onDeleted: {
83
+ type: PropType<ResourceDetailProps["onDeleted"]>;
84
+ default: undefined;
85
+ };
86
+ className: {
87
+ type: PropType<string | undefined>;
88
+ default: undefined;
89
+ };
90
+ }>> & Readonly<{}>, {
91
+ className: string | undefined;
92
+ actions: boolean;
93
+ renderActions: ((record: DetailRecord) => VNodeChild) | undefined;
94
+ onDeleted: ((record: DetailRecord) => void) | undefined;
95
+ resourceActions: boolean | undefined;
96
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
97
+ export {};
98
+ //# sourceMappingURL=resource-detail.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resource-detail.d.ts","sourceRoot":"","sources":["../../src/resource/resource-detail.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2B,KAAK,QAAQ,EAAE,KAAK,UAAU,EAAE,MAAM,KAAK,CAAC;AAa9E,OAAO,KAAK,EAAoB,QAAQ,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAmBhF,KAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE5C,wCAAwC;AACxC,MAAM,WAAW,mBAAmB;IAClC,4EAA4E;IAC5E,QAAQ,EAAE,QAAQ,GAAG,cAAc,CAAC;IACpC,gCAAgC;IAChC,EAAE,EAAE,MAAM,CAAC;IACX;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;OAEG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,sEAAsE;IACtE,aAAa,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,UAAU,CAAC;IACrD;;OAEG;IACH,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAC;IAC3C,iDAAiD;IACjD,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,eAAO,MAAM,cAAc;;cAIL,QAAQ,CAAC,mBAAmB,CAAC,UAAU,CAAC,CAAC;;;;;;;;;;;;cAMxC,QAAQ,CAAC,OAAO,GAAG,SAAS,CAAC;;;;cAI5B,QAAQ,CAAC,mBAAmB,CAAC,eAAe,CAAC,CAAC;;;;cAI9C,QAAQ,CAAC,mBAAmB,CAAC,WAAW,CAAC,CAAC;;;;cAI5C,QAAQ,CAAC,MAAM,GAAG,SAAS,CAAC;;;;;;;cAlB5B,QAAQ,CAAC,mBAAmB,CAAC,UAAU,CAAC,CAAC;;;;;;;;;;;;cAMxC,QAAQ,CAAC,OAAO,GAAG,SAAS,CAAC;;;;cAI5B,QAAQ,CAAC,mBAAmB,CAAC,eAAe,CAAC,CAAC;;;;cAI9C,QAAQ,CAAC,mBAAmB,CAAC,WAAW,CAAC,CAAC;;;;cAI5C,QAAQ,CAAC,MAAM,GAAG,SAAS,CAAC;;;;;;6BAlCvB,YAAY,KAAK,UAAU;yBAI/B,YAAY,KAAK,IAAI;;4EA4X1C,CAAC"}
@@ -0,0 +1,258 @@
1
+ import { defineComponent, h, ref } from "vue";
2
+ import { PencilIcon, Trash2Icon } from "lucide-vue-next";
3
+ import { Button } from "#components/button";
4
+ import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "#components/dialog";
5
+ import { cn } from "#lib/utils";
6
+ import { actionNeedsDialog, resourceActionSchemas, } from "@verikit/ui-core/actions/resource-actions";
7
+ import { resolveVerikitFields } from "@verikit/ui-core/form/resolve-fields";
8
+ import { formatFieldValue } from "@verikit/ui-core/layout/format-value";
9
+ import { useDeleteResource } from "../query/use-resource-mutations.js";
10
+ import { useResourceFindWithActions } from "../query/use-resource-queries.js";
11
+ import { declaredActionButton } from "./declared-action-button.js";
12
+ import { hasStatus, isActionDenied, isPermissionDenied, ResourceActionDialog, useRunResourceAction, } from "./resource-action-dialog.js";
13
+ import { ResourceForm } from "./resource-form.js";
14
+ /**
15
+ * Shows one record's fields as a label/value list, with its actions: the built-in Edit and Delete, and the record-scoped actions declared on the resource. Fields marked `.hidden()`, or left out by the server because the actor can't read them, aren't shown. A record the actor can't read shows as not found, as the server doesn't reveal whether it exists.
16
+ */
17
+ export const ResourceDetail = defineComponent({
18
+ name: "ResourceDetail",
19
+ props: {
20
+ resource: {
21
+ type: Object,
22
+ required: true,
23
+ },
24
+ id: { type: String, required: true },
25
+ actions: { type: Boolean, default: false },
26
+ resourceActions: {
27
+ type: Boolean,
28
+ default: undefined,
29
+ },
30
+ renderActions: {
31
+ type: Function,
32
+ default: undefined,
33
+ },
34
+ onDeleted: {
35
+ type: Function,
36
+ default: undefined,
37
+ },
38
+ className: {
39
+ type: String,
40
+ default: undefined,
41
+ },
42
+ },
43
+ setup(props) {
44
+ const deleted = ref(false);
45
+ const query = useResourceFindWithActions(props.resource.name, () => props.id, { enabled: () => !deleted.value });
46
+ const fields = resolveVerikitFields(props.resource);
47
+ const recordActions = (props.resourceActions ?? props.actions)
48
+ ? resourceActionSchemas(props.resource, "record")
49
+ : [];
50
+ const editOpen = ref(false);
51
+ const deleteOpen = ref(false);
52
+ const deniedUpdate = ref(false);
53
+ const deniedDelete = ref(false);
54
+ const deniedActions = ref({});
55
+ const activeAction = ref(null);
56
+ const actionError = ref(null);
57
+ function denyAction(action) {
58
+ deniedActions.value = { ...deniedActions.value, [action.name]: true };
59
+ }
60
+ const directAction = useRunResourceAction(props.resource.name, {
61
+ onSuccess: () => {
62
+ actionError.value = null;
63
+ },
64
+ onError: (mutationError, variables) => {
65
+ if (isActionDenied(mutationError, variables.recordId)) {
66
+ denyAction(variables.action);
67
+ }
68
+ else {
69
+ actionError.value = mutationError.message;
70
+ }
71
+ },
72
+ });
73
+ function startAction(action) {
74
+ actionError.value = null;
75
+ if (actionNeedsDialog(action)) {
76
+ activeAction.value = action;
77
+ }
78
+ else {
79
+ directAction.mutate({ action, recordId: props.id });
80
+ }
81
+ }
82
+ const deleteMutation = useDeleteResource(props.resource.name, {
83
+ onError: (mutationError) => {
84
+ if (isPermissionDenied(mutationError)) {
85
+ deniedDelete.value = true;
86
+ deleteOpen.value = false;
87
+ }
88
+ },
89
+ onSuccess: () => {
90
+ deleteOpen.value = false;
91
+ deleted.value = true;
92
+ props.onDeleted?.(query.data.value.record);
93
+ },
94
+ });
95
+ function statusMessage(message) {
96
+ return h("p", {
97
+ role: "status",
98
+ class: cn("text-sm text-muted-foreground", props.className),
99
+ }, message);
100
+ }
101
+ function actionBar(record) {
102
+ const unavailable = query.data.value.actions;
103
+ return h("div", { class: "flex flex-wrap items-center justify-end gap-2 pb-3" }, [
104
+ ...recordActions.map((action) => deniedActions.value[action.name]
105
+ ? null
106
+ : declaredActionButton({
107
+ action,
108
+ unavailable: unavailable[action.name],
109
+ variant: "outline",
110
+ disabled: directAction.isPending.value,
111
+ onClick: () => startAction(action),
112
+ })),
113
+ props.actions && !deniedUpdate.value
114
+ ? h(Button, {
115
+ type: "button",
116
+ variant: "outline",
117
+ size: "sm",
118
+ onClick: () => {
119
+ editOpen.value = true;
120
+ },
121
+ }, { default: () => [h(PencilIcon), "Edit"] })
122
+ : null,
123
+ props.actions && !deniedDelete.value
124
+ ? h(Button, {
125
+ type: "button",
126
+ variant: "outline",
127
+ size: "sm",
128
+ onClick: () => {
129
+ deleteMutation.reset();
130
+ deleteOpen.value = true;
131
+ },
132
+ }, { default: () => [h(Trash2Icon), "Delete"] })
133
+ : null,
134
+ props.renderActions ? props.renderActions(record) : null,
135
+ ]);
136
+ }
137
+ function editDialog(record) {
138
+ return h(Dialog, {
139
+ open: editOpen.value,
140
+ onOpenChange: (open) => {
141
+ editOpen.value = open;
142
+ },
143
+ }, {
144
+ default: () => h(DialogContent, {}, {
145
+ default: () => [
146
+ h(DialogHeader, {}, {
147
+ default: () => h(DialogTitle, {}, { default: () => `Edit ${props.resource.name}` }),
148
+ }),
149
+ h(ResourceForm, {
150
+ resource: props.resource,
151
+ id: props.id,
152
+ defaultValues: record,
153
+ onSuccess: () => {
154
+ editOpen.value = false;
155
+ },
156
+ onError: (mutationError) => {
157
+ if (isPermissionDenied(mutationError)) {
158
+ deniedUpdate.value = true;
159
+ editOpen.value = false;
160
+ }
161
+ },
162
+ submitLabel: "Save changes",
163
+ }),
164
+ ],
165
+ }),
166
+ });
167
+ }
168
+ function deleteDialog() {
169
+ return h(Dialog, {
170
+ open: deleteOpen.value,
171
+ onOpenChange: (open) => {
172
+ deleteOpen.value = open;
173
+ },
174
+ }, {
175
+ default: () => h(DialogContent, {}, {
176
+ default: () => [
177
+ h(DialogHeader, {}, {
178
+ default: () => [
179
+ h(DialogTitle, {}, {
180
+ default: () => `Delete this ${props.resource.name}?`,
181
+ }),
182
+ h(DialogDescription, {}, { default: () => "This can't be undone." }),
183
+ ],
184
+ }),
185
+ deleteMutation.error.value &&
186
+ !isPermissionDenied(deleteMutation.error.value)
187
+ ? h("p", { role: "alert", class: "text-sm text-destructive" }, deleteMutation.error.value.message)
188
+ : null,
189
+ h(DialogFooter, {}, {
190
+ default: () => [
191
+ h(DialogClose, { asChild: true }, {
192
+ default: () => h(Button, { type: "button", variant: "outline" }, { default: () => "Cancel" }),
193
+ }),
194
+ h(Button, {
195
+ type: "button",
196
+ variant: "destructive",
197
+ disabled: deleteMutation.isPending.value,
198
+ onClick: () => deleteMutation.mutate(props.id),
199
+ }, {
200
+ default: () => deleteMutation.isPending.value
201
+ ? "Deleting…"
202
+ : "Delete",
203
+ }),
204
+ ],
205
+ }),
206
+ ],
207
+ }),
208
+ });
209
+ }
210
+ return () => {
211
+ if (deleted.value) {
212
+ return statusMessage(`This ${props.resource.name} was deleted.`);
213
+ }
214
+ if (query.isPending.value) {
215
+ return h("p", { class: cn("text-sm text-muted-foreground", props.className) }, "Loading…");
216
+ }
217
+ if (query.isError.value) {
218
+ const error = query.error.value;
219
+ return hasStatus(error, 404)
220
+ ? statusMessage(`This ${props.resource.name} doesn't exist or you can't view it.`)
221
+ : h("p", {
222
+ role: "alert",
223
+ class: cn("text-sm text-destructive", props.className),
224
+ }, error.message);
225
+ }
226
+ const record = query.data.value.record;
227
+ const visibleFields = Object.values(fields).filter((field) => !field.hidden && Object.hasOwn(record, field.name));
228
+ const hasActions = props.actions ||
229
+ recordActions.length > 0 ||
230
+ Boolean(props.renderActions);
231
+ return h("div", { class: cn("w-full", props.className) }, [
232
+ hasActions ? actionBar(record) : null,
233
+ actionError.value
234
+ ? h("p", { role: "alert", class: "pb-3 text-sm text-destructive" }, actionError.value)
235
+ : null,
236
+ h("dl", {
237
+ class: "grid gap-x-6 gap-y-3 rounded-lg border border-border p-4 sm:grid-cols-[minmax(8rem,max-content)_1fr]",
238
+ }, visibleFields.map((field) => h("div", { key: field.name, class: "contents" }, [
239
+ h("dt", { class: "text-sm font-medium text-muted-foreground" }, field.label ?? field.name),
240
+ h("dd", { class: "text-sm break-words" }, formatFieldValue(record[field.name])),
241
+ ]))),
242
+ recordActions.length > 0
243
+ ? h(ResourceActionDialog, {
244
+ resourceName: props.resource.name,
245
+ action: activeAction.value,
246
+ recordId: props.id,
247
+ onClose: () => {
248
+ activeAction.value = null;
249
+ },
250
+ onDenied: denyAction,
251
+ })
252
+ : null,
253
+ props.actions ? editDialog(record) : null,
254
+ props.actions ? deleteDialog() : null,
255
+ ]);
256
+ };
257
+ },
258
+ });