@verikit/vue 0.24.2 → 0.24.4
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/dist/form/use-verikit-schema-tree-form.d.ts +3 -1
- package/dist/form/use-verikit-schema-tree-form.d.ts.map +1 -1
- package/dist/form/use-verikit-schema-tree-form.js +8 -1
- package/dist/query/index.d.ts +2 -2
- package/dist/query/index.d.ts.map +1 -1
- package/dist/query/index.js +1 -1
- package/dist/query/use-resource-mutations.d.ts.map +1 -1
- package/dist/query/use-resource-mutations.js +1 -0
- package/dist/query/use-resource-queries.d.ts +7 -1
- package/dist/query/use-resource-queries.d.ts.map +1 -1
- package/dist/query/use-resource-queries.js +13 -0
- package/dist/query/use-resource-table.d.ts +9 -3
- package/dist/query/use-resource-table.d.ts.map +1 -1
- package/dist/query/use-resource-table.js +3 -1
- package/dist/resource/declared-action-button.d.ts +14 -0
- package/dist/resource/declared-action-button.d.ts.map +1 -0
- package/dist/resource/declared-action-button.js +24 -0
- package/dist/resource/index.d.ts +4 -0
- package/dist/resource/index.d.ts.map +1 -1
- package/dist/resource/index.js +2 -0
- package/dist/resource/resource-action-dialog.d.ts +103 -0
- package/dist/resource/resource-action-dialog.d.ts.map +1 -0
- package/dist/resource/resource-action-dialog.js +207 -0
- package/dist/resource/resource-detail.d.ts +124 -0
- package/dist/resource/resource-detail.d.ts.map +1 -0
- package/dist/resource/resource-detail.js +273 -0
- package/dist/resource/resource-table.d.ts +13 -0
- package/dist/resource/resource-table.d.ts.map +1 -1
- package/dist/resource/resource-table.js +108 -35
- package/package.json +6 -6
|
@@ -0,0 +1,124 @@
|
|
|
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 and Delete actions, and the default for `editAction`, `deleteAction`, and `resourceActions`.
|
|
12
|
+
*/
|
|
13
|
+
actions?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Renders the built-in Edit action, which opens a `ResourceForm` dialog pre-filled from the record. Hidden when the server reports the actor can't update the record, or when an update is denied (403 or 404) for the rest of this component's lifetime. Defaults to `actions`.
|
|
16
|
+
*/
|
|
17
|
+
editAction?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Renders the built-in Delete action, which opens a confirmation dialog. Hidden when the server reports the actor can't delete the record, or when a delete is denied (403 or 404) for the rest of this component's lifetime. Defaults to `actions`.
|
|
20
|
+
*/
|
|
21
|
+
deleteAction?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* 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`.
|
|
24
|
+
*/
|
|
25
|
+
resourceActions?: boolean;
|
|
26
|
+
/** Renders extra actions alongside the built-in and declared ones. */
|
|
27
|
+
renderActions?: (record: DetailRecord) => VNodeChild;
|
|
28
|
+
/**
|
|
29
|
+
* 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.
|
|
30
|
+
*/
|
|
31
|
+
onDeleted?: (record: DetailRecord) => void;
|
|
32
|
+
/** Class name applied to the outer container. */
|
|
33
|
+
className?: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 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. Edit and Delete are hidden up front when the server reports the actor can't update or delete the record. 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.
|
|
37
|
+
*/
|
|
38
|
+
export declare const ResourceDetail: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
39
|
+
resource: {
|
|
40
|
+
type: PropType<ResourceDetailProps["resource"]>;
|
|
41
|
+
required: true;
|
|
42
|
+
};
|
|
43
|
+
id: {
|
|
44
|
+
type: StringConstructor;
|
|
45
|
+
required: true;
|
|
46
|
+
};
|
|
47
|
+
actions: {
|
|
48
|
+
type: BooleanConstructor;
|
|
49
|
+
default: boolean;
|
|
50
|
+
};
|
|
51
|
+
editAction: {
|
|
52
|
+
type: PropType<boolean | undefined>;
|
|
53
|
+
default: undefined;
|
|
54
|
+
};
|
|
55
|
+
deleteAction: {
|
|
56
|
+
type: PropType<boolean | undefined>;
|
|
57
|
+
default: undefined;
|
|
58
|
+
};
|
|
59
|
+
resourceActions: {
|
|
60
|
+
type: PropType<boolean | undefined>;
|
|
61
|
+
default: undefined;
|
|
62
|
+
};
|
|
63
|
+
renderActions: {
|
|
64
|
+
type: PropType<ResourceDetailProps["renderActions"]>;
|
|
65
|
+
default: undefined;
|
|
66
|
+
};
|
|
67
|
+
onDeleted: {
|
|
68
|
+
type: PropType<ResourceDetailProps["onDeleted"]>;
|
|
69
|
+
default: undefined;
|
|
70
|
+
};
|
|
71
|
+
className: {
|
|
72
|
+
type: PropType<string | undefined>;
|
|
73
|
+
default: undefined;
|
|
74
|
+
};
|
|
75
|
+
}>, () => string | number | boolean | void | import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
76
|
+
[key: string]: any;
|
|
77
|
+
}> | import("vue").VNodeArrayChildren | null, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
78
|
+
resource: {
|
|
79
|
+
type: PropType<ResourceDetailProps["resource"]>;
|
|
80
|
+
required: true;
|
|
81
|
+
};
|
|
82
|
+
id: {
|
|
83
|
+
type: StringConstructor;
|
|
84
|
+
required: true;
|
|
85
|
+
};
|
|
86
|
+
actions: {
|
|
87
|
+
type: BooleanConstructor;
|
|
88
|
+
default: boolean;
|
|
89
|
+
};
|
|
90
|
+
editAction: {
|
|
91
|
+
type: PropType<boolean | undefined>;
|
|
92
|
+
default: undefined;
|
|
93
|
+
};
|
|
94
|
+
deleteAction: {
|
|
95
|
+
type: PropType<boolean | undefined>;
|
|
96
|
+
default: undefined;
|
|
97
|
+
};
|
|
98
|
+
resourceActions: {
|
|
99
|
+
type: PropType<boolean | undefined>;
|
|
100
|
+
default: undefined;
|
|
101
|
+
};
|
|
102
|
+
renderActions: {
|
|
103
|
+
type: PropType<ResourceDetailProps["renderActions"]>;
|
|
104
|
+
default: undefined;
|
|
105
|
+
};
|
|
106
|
+
onDeleted: {
|
|
107
|
+
type: PropType<ResourceDetailProps["onDeleted"]>;
|
|
108
|
+
default: undefined;
|
|
109
|
+
};
|
|
110
|
+
className: {
|
|
111
|
+
type: PropType<string | undefined>;
|
|
112
|
+
default: undefined;
|
|
113
|
+
};
|
|
114
|
+
}>> & Readonly<{}>, {
|
|
115
|
+
className: string | undefined;
|
|
116
|
+
actions: boolean;
|
|
117
|
+
renderActions: ((record: DetailRecord) => VNodeChild) | undefined;
|
|
118
|
+
onDeleted: ((record: DetailRecord) => void) | undefined;
|
|
119
|
+
editAction: boolean | undefined;
|
|
120
|
+
deleteAction: boolean | undefined;
|
|
121
|
+
resourceActions: boolean | undefined;
|
|
122
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
123
|
+
export {};
|
|
124
|
+
//# 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,KA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AAG,SAAS,CAAC;;;;cAI7B,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;;;;;;6BA1CvB,YAAY,KAAK,UAAU;yBAI/B,YAAY,KAAK,IAAI;;;;4EAgZ1C,CAAC"}
|
|
@@ -0,0 +1,273 @@
|
|
|
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, 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. Edit and Delete are hidden up front when the server reports the actor can't update or delete the record. 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
|
+
editAction: {
|
|
27
|
+
type: Boolean,
|
|
28
|
+
default: undefined,
|
|
29
|
+
},
|
|
30
|
+
deleteAction: {
|
|
31
|
+
type: Boolean,
|
|
32
|
+
default: undefined,
|
|
33
|
+
},
|
|
34
|
+
resourceActions: {
|
|
35
|
+
type: Boolean,
|
|
36
|
+
default: undefined,
|
|
37
|
+
},
|
|
38
|
+
renderActions: {
|
|
39
|
+
type: Function,
|
|
40
|
+
default: undefined,
|
|
41
|
+
},
|
|
42
|
+
onDeleted: {
|
|
43
|
+
type: Function,
|
|
44
|
+
default: undefined,
|
|
45
|
+
},
|
|
46
|
+
className: {
|
|
47
|
+
type: String,
|
|
48
|
+
default: undefined,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
setup(props) {
|
|
52
|
+
const deleted = ref(false);
|
|
53
|
+
const query = useResourceFindWithActions(props.resource.name, () => props.id, { enabled: () => !deleted.value });
|
|
54
|
+
const fields = resolveVerikitFields(props.resource);
|
|
55
|
+
const recordActions = (props.resourceActions ?? props.actions)
|
|
56
|
+
? resourceActionSchemas(props.resource, "record")
|
|
57
|
+
: [];
|
|
58
|
+
const editOpen = ref(false);
|
|
59
|
+
const deleteOpen = ref(false);
|
|
60
|
+
const deniedUpdate = ref(false);
|
|
61
|
+
const deniedDelete = ref(false);
|
|
62
|
+
const deniedActions = ref({});
|
|
63
|
+
const activeAction = ref(null);
|
|
64
|
+
const actionError = ref(null);
|
|
65
|
+
function denyAction(action) {
|
|
66
|
+
deniedActions.value = { ...deniedActions.value, [action.name]: true };
|
|
67
|
+
}
|
|
68
|
+
const directAction = useRunResourceAction(props.resource.name, {
|
|
69
|
+
onSuccess: () => {
|
|
70
|
+
actionError.value = null;
|
|
71
|
+
},
|
|
72
|
+
onError: (mutationError, variables) => {
|
|
73
|
+
if (isActionDenied(mutationError, variables.recordId)) {
|
|
74
|
+
denyAction(variables.action);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
actionError.value = mutationError.message;
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
function startAction(action) {
|
|
82
|
+
actionError.value = null;
|
|
83
|
+
if (actionNeedsDialog(action)) {
|
|
84
|
+
activeAction.value = action;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
directAction.mutate({ action, recordId: props.id });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const deleteMutation = useDeleteResource(props.resource.name, {
|
|
91
|
+
onError: (mutationError) => {
|
|
92
|
+
// The server answers a denied delete with 404 so as not to reveal
|
|
93
|
+
// whether the record exists, the same as a denied record action.
|
|
94
|
+
if (isActionDenied(mutationError, props.id)) {
|
|
95
|
+
deniedDelete.value = true;
|
|
96
|
+
deleteOpen.value = false;
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
onSuccess: () => {
|
|
100
|
+
deleteOpen.value = false;
|
|
101
|
+
deleted.value = true;
|
|
102
|
+
props.onDeleted?.(query.data.value.record);
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
function statusMessage(message) {
|
|
106
|
+
return h("p", {
|
|
107
|
+
role: "status",
|
|
108
|
+
class: cn("text-sm text-muted-foreground", props.className),
|
|
109
|
+
}, message);
|
|
110
|
+
}
|
|
111
|
+
function actionBar(record, showEdit, showDelete) {
|
|
112
|
+
const unavailable = query.data.value.actions;
|
|
113
|
+
return h("div", { class: "flex flex-wrap items-center justify-end gap-2 pb-3" }, [
|
|
114
|
+
...recordActions.map((action) => deniedActions.value[action.name]
|
|
115
|
+
? null
|
|
116
|
+
: declaredActionButton({
|
|
117
|
+
action,
|
|
118
|
+
unavailable: unavailable[action.name],
|
|
119
|
+
variant: "outline",
|
|
120
|
+
disabled: directAction.isPending.value,
|
|
121
|
+
onClick: () => startAction(action),
|
|
122
|
+
})),
|
|
123
|
+
showEdit
|
|
124
|
+
? h(Button, {
|
|
125
|
+
type: "button",
|
|
126
|
+
variant: "outline",
|
|
127
|
+
size: "sm",
|
|
128
|
+
onClick: () => {
|
|
129
|
+
editOpen.value = true;
|
|
130
|
+
},
|
|
131
|
+
}, { default: () => [h(PencilIcon), "Edit"] })
|
|
132
|
+
: null,
|
|
133
|
+
showDelete
|
|
134
|
+
? h(Button, {
|
|
135
|
+
type: "button",
|
|
136
|
+
variant: "outline",
|
|
137
|
+
size: "sm",
|
|
138
|
+
onClick: () => {
|
|
139
|
+
deleteMutation.reset();
|
|
140
|
+
deleteOpen.value = true;
|
|
141
|
+
},
|
|
142
|
+
}, { default: () => [h(Trash2Icon), "Delete"] })
|
|
143
|
+
: null,
|
|
144
|
+
props.renderActions ? props.renderActions(record) : null,
|
|
145
|
+
]);
|
|
146
|
+
}
|
|
147
|
+
function editDialog(record) {
|
|
148
|
+
return h(Dialog, {
|
|
149
|
+
open: editOpen.value,
|
|
150
|
+
onOpenChange: (open) => {
|
|
151
|
+
editOpen.value = open;
|
|
152
|
+
},
|
|
153
|
+
}, {
|
|
154
|
+
default: () => h(DialogContent, {}, {
|
|
155
|
+
default: () => [
|
|
156
|
+
h(DialogHeader, {}, {
|
|
157
|
+
default: () => h(DialogTitle, {}, { default: () => `Edit ${props.resource.name}` }),
|
|
158
|
+
}),
|
|
159
|
+
h(ResourceForm, {
|
|
160
|
+
resource: props.resource,
|
|
161
|
+
id: props.id,
|
|
162
|
+
defaultValues: record,
|
|
163
|
+
onSuccess: () => {
|
|
164
|
+
editOpen.value = false;
|
|
165
|
+
},
|
|
166
|
+
onError: (mutationError) => {
|
|
167
|
+
if (isActionDenied(mutationError, props.id)) {
|
|
168
|
+
deniedUpdate.value = true;
|
|
169
|
+
editOpen.value = false;
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
submitLabel: "Save changes",
|
|
173
|
+
}),
|
|
174
|
+
],
|
|
175
|
+
}),
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
function deleteDialog() {
|
|
179
|
+
return h(Dialog, {
|
|
180
|
+
open: deleteOpen.value,
|
|
181
|
+
onOpenChange: (open) => {
|
|
182
|
+
deleteOpen.value = open;
|
|
183
|
+
},
|
|
184
|
+
}, {
|
|
185
|
+
default: () => h(DialogContent, {}, {
|
|
186
|
+
default: () => [
|
|
187
|
+
h(DialogHeader, {}, {
|
|
188
|
+
default: () => [
|
|
189
|
+
h(DialogTitle, {}, {
|
|
190
|
+
default: () => `Delete this ${props.resource.name}?`,
|
|
191
|
+
}),
|
|
192
|
+
h(DialogDescription, {}, { default: () => "This can't be undone." }),
|
|
193
|
+
],
|
|
194
|
+
}),
|
|
195
|
+
deleteMutation.error.value &&
|
|
196
|
+
!isActionDenied(deleteMutation.error.value, props.id)
|
|
197
|
+
? h("p", { role: "alert", class: "text-sm text-destructive" }, deleteMutation.error.value.message)
|
|
198
|
+
: null,
|
|
199
|
+
h(DialogFooter, {}, {
|
|
200
|
+
default: () => [
|
|
201
|
+
h(DialogClose, { asChild: true }, {
|
|
202
|
+
default: () => h(Button, { type: "button", variant: "outline" }, { default: () => "Cancel" }),
|
|
203
|
+
}),
|
|
204
|
+
h(Button, {
|
|
205
|
+
type: "button",
|
|
206
|
+
variant: "destructive",
|
|
207
|
+
disabled: deleteMutation.isPending.value,
|
|
208
|
+
onClick: () => deleteMutation.mutate(props.id),
|
|
209
|
+
}, {
|
|
210
|
+
default: () => deleteMutation.isPending.value
|
|
211
|
+
? "Deleting…"
|
|
212
|
+
: "Delete",
|
|
213
|
+
}),
|
|
214
|
+
],
|
|
215
|
+
}),
|
|
216
|
+
],
|
|
217
|
+
}),
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
return () => {
|
|
221
|
+
if (deleted.value) {
|
|
222
|
+
return statusMessage(`This ${props.resource.name} was deleted.`);
|
|
223
|
+
}
|
|
224
|
+
if (query.isPending.value) {
|
|
225
|
+
return h("p", { class: cn("text-sm text-muted-foreground", props.className) }, "Loading…");
|
|
226
|
+
}
|
|
227
|
+
if (query.isError.value) {
|
|
228
|
+
const error = query.error.value;
|
|
229
|
+
return hasStatus(error, 404)
|
|
230
|
+
? statusMessage(`This ${props.resource.name} doesn't exist or you can't view it.`)
|
|
231
|
+
: h("p", {
|
|
232
|
+
role: "alert",
|
|
233
|
+
class: cn("text-sm text-destructive", props.className),
|
|
234
|
+
}, error.message);
|
|
235
|
+
}
|
|
236
|
+
const { record, operations } = query.data.value;
|
|
237
|
+
const editEnabled = props.editAction ?? props.actions;
|
|
238
|
+
const deleteEnabled = props.deleteAction ?? props.actions;
|
|
239
|
+
const showEdit = editEnabled && !deniedUpdate.value && !operations.update;
|
|
240
|
+
const showDelete = deleteEnabled && !deniedDelete.value && !operations.delete;
|
|
241
|
+
const visibleFields = Object.values(fields).filter((field) => !field.hidden && Object.hasOwn(record, field.name));
|
|
242
|
+
const hasActions = showEdit ||
|
|
243
|
+
showDelete ||
|
|
244
|
+
recordActions.length > 0 ||
|
|
245
|
+
Boolean(props.renderActions);
|
|
246
|
+
return h("div", { class: cn("w-full", props.className) }, [
|
|
247
|
+
hasActions ? actionBar(record, showEdit, showDelete) : null,
|
|
248
|
+
actionError.value
|
|
249
|
+
? h("p", { role: "alert", class: "pb-3 text-sm text-destructive" }, actionError.value)
|
|
250
|
+
: null,
|
|
251
|
+
h("dl", {
|
|
252
|
+
class: "grid gap-x-6 gap-y-3 rounded-lg border border-border p-4 sm:grid-cols-[minmax(8rem,max-content)_1fr]",
|
|
253
|
+
}, visibleFields.map((field) => h("div", { key: field.name, class: "contents" }, [
|
|
254
|
+
h("dt", { class: "text-sm font-medium text-muted-foreground" }, field.label ?? field.name),
|
|
255
|
+
h("dd", { class: "text-sm break-words" }, formatFieldValue(record[field.name])),
|
|
256
|
+
]))),
|
|
257
|
+
recordActions.length > 0
|
|
258
|
+
? h(ResourceActionDialog, {
|
|
259
|
+
resourceName: props.resource.name,
|
|
260
|
+
action: activeAction.value,
|
|
261
|
+
recordId: props.id,
|
|
262
|
+
onClose: () => {
|
|
263
|
+
activeAction.value = null;
|
|
264
|
+
},
|
|
265
|
+
onDenied: denyAction,
|
|
266
|
+
})
|
|
267
|
+
: null,
|
|
268
|
+
editEnabled ? editDialog(record) : null,
|
|
269
|
+
deleteEnabled ? deleteDialog() : null,
|
|
270
|
+
]);
|
|
271
|
+
};
|
|
272
|
+
},
|
|
273
|
+
});
|
|
@@ -10,6 +10,10 @@ export interface ResourceTableProps<TRecord extends RowData = Record<string, unk
|
|
|
10
10
|
* Edit/Delete row actions.
|
|
11
11
|
*/
|
|
12
12
|
actions?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Renders the actions declared on the resource via `defineResource({ actions })`: record-scoped ones per row, collection-scoped ones in the toolbar. Labels, confirmations, and input forms come from each declaration; actions with neither a confirmation nor a form run on click. The table asks the server which of them the actor can't run on each page: 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 for a record action, which the server returns so as not to reveal whether the record exists) is hidden for the rest of this component's lifetime. Defaults to `actions`.
|
|
15
|
+
*/
|
|
16
|
+
resourceActions?: boolean;
|
|
13
17
|
/** Renders per-row actions (e.g. custom edit/delete buttons). */
|
|
14
18
|
renderActions?: (record: TRecord) => VNodeChild;
|
|
15
19
|
/** Renders custom actions for the current row selection. */
|
|
@@ -36,6 +40,10 @@ export declare const ResourceTable: import("vue").DefineComponent<import("vue").
|
|
|
36
40
|
type: BooleanConstructor;
|
|
37
41
|
default: boolean;
|
|
38
42
|
};
|
|
43
|
+
resourceActions: {
|
|
44
|
+
type: PropType<boolean | undefined>;
|
|
45
|
+
default: undefined;
|
|
46
|
+
};
|
|
39
47
|
renderActions: {
|
|
40
48
|
type: PropType<ResourceTableProps["renderActions"]>;
|
|
41
49
|
default: undefined;
|
|
@@ -67,6 +75,10 @@ export declare const ResourceTable: import("vue").DefineComponent<import("vue").
|
|
|
67
75
|
type: BooleanConstructor;
|
|
68
76
|
default: boolean;
|
|
69
77
|
};
|
|
78
|
+
resourceActions: {
|
|
79
|
+
type: PropType<boolean | undefined>;
|
|
80
|
+
default: undefined;
|
|
81
|
+
};
|
|
70
82
|
renderActions: {
|
|
71
83
|
type: PropType<ResourceTableProps["renderActions"]>;
|
|
72
84
|
default: undefined;
|
|
@@ -88,6 +100,7 @@ export declare const ResourceTable: import("vue").DefineComponent<import("vue").
|
|
|
88
100
|
actions: boolean;
|
|
89
101
|
pageSize: number | undefined;
|
|
90
102
|
renderActions: ((record: Record<string, unknown>) => VNodeChild) | undefined;
|
|
103
|
+
resourceActions: boolean | undefined;
|
|
91
104
|
renderBulkActions: ((records: Record<string, unknown>[], clearSelection: () => void) => VNodeChild) | undefined;
|
|
92
105
|
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
93
106
|
//# sourceMappingURL=resource-table.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resource-table.d.ts","sourceRoot":"","sources":["../../src/resource/resource-table.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2B,KAAK,QAAQ,EAAE,KAAK,UAAU,EAAE,MAAM,KAAK,CAAC;AAY9E,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"resource-table.d.ts","sourceRoot":"","sources":["../../src/resource/resource-table.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2B,KAAK,QAAQ,EAAE,KAAK,UAAU,EAAE,MAAM,KAAK,CAAC;AAY9E,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAuBnD,OAAO,EAEL,KAAK,uBAAuB,EAC5B,KAAK,sBAAsB,EAC5B,MAAM,gCAAgC,CAAC;AA0BxC,uCAAuC;AACvC,MAAM,WAAW,kBAAkB,CACjC,OAAO,SAAS,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAA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MAAM,IAAI,KACvB,UAAU;4EA+7Bf,CAAC"}
|
|
@@ -5,37 +5,19 @@ import { Checkbox } from "#components/checkbox";
|
|
|
5
5
|
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "#components/dialog";
|
|
6
6
|
import { Input } from "#components/input";
|
|
7
7
|
import { cn } from "#lib/utils";
|
|
8
|
+
import { actionNeedsDialog, resourceActionSchemas, unavailableAction, } from "@verikit/ui-core/actions/resource-actions";
|
|
9
|
+
import { formatFieldValue } from "@verikit/ui-core/layout/format-value";
|
|
8
10
|
import { recordId } from "@verikit/ui-core/query/optimistic";
|
|
9
11
|
import { useDeleteResource } from "../query/use-resource-mutations.js";
|
|
10
12
|
import { useResourceTable, } from "../query/use-resource-table.js";
|
|
13
|
+
import { isActionDenied, isPermissionDenied, ResourceActionDialog, useRunResourceAction, } from "./resource-action-dialog.js";
|
|
14
|
+
import { declaredActionButton } from "./declared-action-button.js";
|
|
11
15
|
import { ResourceForm } from "./resource-form.js";
|
|
12
16
|
import { filterableFields, ResourceTableFilterPanel, } from "./resource-table-filters.js";
|
|
13
|
-
/**
|
|
14
|
-
* True for an error whose `status` is 403 (permission denied). Duck-typed on
|
|
15
|
-
* `status`, not `instanceof VerikitClientError`.
|
|
16
|
-
*/
|
|
17
|
-
function isPermissionDenied(error) {
|
|
18
|
-
return (typeof error === "object" &&
|
|
19
|
-
error !== null &&
|
|
20
|
-
"status" in error &&
|
|
21
|
-
error.status === 403);
|
|
22
|
-
}
|
|
23
17
|
// `header` is always the field's required string label.
|
|
24
18
|
function headerText(header) {
|
|
25
19
|
return header.column.columnDef.header;
|
|
26
20
|
}
|
|
27
|
-
function cellText(value) {
|
|
28
|
-
if (value === null || value === undefined) {
|
|
29
|
-
return "";
|
|
30
|
-
}
|
|
31
|
-
if (value instanceof Date) {
|
|
32
|
-
return value.toLocaleString();
|
|
33
|
-
}
|
|
34
|
-
if (typeof value === "boolean") {
|
|
35
|
-
return value ? "Yes" : "No";
|
|
36
|
-
}
|
|
37
|
-
return String(value);
|
|
38
|
-
}
|
|
39
21
|
function sortIcon(direction) {
|
|
40
22
|
if (direction === "asc") {
|
|
41
23
|
return h(ArrowUpIcon, { class: "size-3.5" });
|
|
@@ -61,6 +43,10 @@ export const ResourceTable = defineComponent({
|
|
|
61
43
|
default: undefined,
|
|
62
44
|
},
|
|
63
45
|
actions: { type: Boolean, default: false },
|
|
46
|
+
resourceActions: {
|
|
47
|
+
type: Boolean,
|
|
48
|
+
default: undefined,
|
|
49
|
+
},
|
|
64
50
|
renderActions: {
|
|
65
51
|
type: Function,
|
|
66
52
|
default: undefined,
|
|
@@ -79,7 +65,17 @@ export const ResourceTable = defineComponent({
|
|
|
79
65
|
},
|
|
80
66
|
},
|
|
81
67
|
setup(props) {
|
|
82
|
-
const
|
|
68
|
+
const showResourceActions = props.resourceActions ?? props.actions;
|
|
69
|
+
const recordActions = showResourceActions
|
|
70
|
+
? resourceActionSchemas(props.resource, "record")
|
|
71
|
+
: [];
|
|
72
|
+
const collectionActions = showResourceActions
|
|
73
|
+
? resourceActionSchemas(props.resource, "collection")
|
|
74
|
+
: [];
|
|
75
|
+
const { table, isLoading, error, fields, filters, setFilters, actions: actionAvailability, } = useResourceTable(props.resource, {
|
|
76
|
+
pageSize: props.pageSize,
|
|
77
|
+
includeActions: recordActions.length > 0 || collectionActions.length > 0,
|
|
78
|
+
});
|
|
83
79
|
const createOpen = ref(false);
|
|
84
80
|
const createDenied = ref(false);
|
|
85
81
|
const editRecord = ref(null);
|
|
@@ -89,6 +85,61 @@ export const ResourceTable = defineComponent({
|
|
|
89
85
|
const bulkDeleteOpen = ref(false);
|
|
90
86
|
const bulkDeleteError = ref(null);
|
|
91
87
|
const bulkDeleting = ref(false);
|
|
88
|
+
const activeAction = ref(null);
|
|
89
|
+
const deniedCollectionActions = ref({});
|
|
90
|
+
const actionError = ref(null);
|
|
91
|
+
function denyAction(action, id) {
|
|
92
|
+
if (id === undefined) {
|
|
93
|
+
deniedCollectionActions.value = {
|
|
94
|
+
...deniedCollectionActions.value,
|
|
95
|
+
[action.name]: true,
|
|
96
|
+
};
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
deniedRows.value = {
|
|
100
|
+
...deniedRows.value,
|
|
101
|
+
[id]: {
|
|
102
|
+
...deniedRows.value[id],
|
|
103
|
+
actions: { ...deniedRows.value[id]?.actions, [action.name]: true },
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
const directAction = useRunResourceAction(props.resource.name, {
|
|
108
|
+
onSuccess: () => {
|
|
109
|
+
actionError.value = null;
|
|
110
|
+
},
|
|
111
|
+
onError: (mutationError, variables) => {
|
|
112
|
+
if (isActionDenied(mutationError, variables.recordId)) {
|
|
113
|
+
denyAction(variables.action, variables.recordId);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
actionError.value = mutationError.message;
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
function startAction(action, id) {
|
|
121
|
+
actionError.value = null;
|
|
122
|
+
if (actionNeedsDialog(action)) {
|
|
123
|
+
activeAction.value = { action, recordId: id };
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
directAction.mutate({ action, recordId: id });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function tableActionButton(action, variant, id) {
|
|
130
|
+
return declaredActionButton({
|
|
131
|
+
action,
|
|
132
|
+
unavailable: unavailableAction(actionAvailability.value, action.name, id),
|
|
133
|
+
variant,
|
|
134
|
+
disabled: directAction.isPending.value,
|
|
135
|
+
onClick: () => startAction(action, id),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function declaredRowActions(record) {
|
|
139
|
+
const id = recordId(record);
|
|
140
|
+
const denied = deniedRows.value[id]?.actions;
|
|
141
|
+
return recordActions.map((action) => denied?.[action.name] ? null : tableActionButton(action, "ghost", id));
|
|
142
|
+
}
|
|
92
143
|
const deleteMutation = useDeleteResource(props.resource.name, {
|
|
93
144
|
onError: (mutationError, id) => {
|
|
94
145
|
if (isPermissionDenied(mutationError)) {
|
|
@@ -175,6 +226,7 @@ export const ResourceTable = defineComponent({
|
|
|
175
226
|
}
|
|
176
227
|
function rowActions(record) {
|
|
177
228
|
return h("div", { class: "flex items-center justify-end gap-1" }, [
|
|
229
|
+
declaredRowActions(record),
|
|
178
230
|
props.actions ? builtInRowActions(record) : null,
|
|
179
231
|
props.renderActions ? props.renderActions(record) : null,
|
|
180
232
|
]);
|
|
@@ -187,7 +239,9 @@ export const ResourceTable = defineComponent({
|
|
|
187
239
|
const deleteId = deleteRecord.value
|
|
188
240
|
? recordId(deleteRecord.value)
|
|
189
241
|
: undefined;
|
|
190
|
-
const hasActionsColumn = props.actions ||
|
|
242
|
+
const hasActionsColumn = props.actions ||
|
|
243
|
+
Boolean(props.renderActions) ||
|
|
244
|
+
recordActions.length > 0;
|
|
191
245
|
const hasSelectionColumn = props.actions || Boolean(props.renderBulkActions);
|
|
192
246
|
const hasFilterableFields = filterableFields(fields).length > 0;
|
|
193
247
|
const activeFilterCount = Object.keys(filters.value).length;
|
|
@@ -235,16 +289,24 @@ export const ResourceTable = defineComponent({
|
|
|
235
289
|
}, { default: () => "Clear filters" })
|
|
236
290
|
: null,
|
|
237
291
|
]),
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
292
|
+
h("div", { class: "flex items-center gap-2" }, [
|
|
293
|
+
...collectionActions.map((action) => deniedCollectionActions.value[action.name]
|
|
294
|
+
? null
|
|
295
|
+
: tableActionButton(action, "outline")),
|
|
296
|
+
props.actions && !createDenied.value
|
|
297
|
+
? h(Button, {
|
|
298
|
+
type: "button",
|
|
299
|
+
size: "sm",
|
|
300
|
+
onClick: () => {
|
|
301
|
+
createOpen.value = true;
|
|
302
|
+
},
|
|
303
|
+
}, { default: () => [h(PlusIcon), "New"] })
|
|
304
|
+
: null,
|
|
305
|
+
]),
|
|
247
306
|
]),
|
|
307
|
+
actionError.value
|
|
308
|
+
? h("p", { role: "alert", class: "pb-3 text-sm text-destructive" }, actionError.value)
|
|
309
|
+
: null,
|
|
248
310
|
filtersOpen.value && hasFilterableFields
|
|
249
311
|
? h("div", { class: "pb-3" }, [
|
|
250
312
|
h(ResourceTableFilterPanel, {
|
|
@@ -342,7 +404,7 @@ export const ResourceTable = defineComponent({
|
|
|
342
404
|
: null,
|
|
343
405
|
...row
|
|
344
406
|
.getAllCells()
|
|
345
|
-
.map((cell) => h("td", { key: cell.id, class: "px-3 py-2" },
|
|
407
|
+
.map((cell) => h("td", { key: cell.id, class: "px-3 py-2" }, formatFieldValue(cell.getValue()))),
|
|
346
408
|
hasActionsColumn
|
|
347
409
|
? h("td", { class: "px-3 py-2 text-right" }, [
|
|
348
410
|
rowActions(row.original),
|
|
@@ -375,7 +437,7 @@ export const ResourceTable = defineComponent({
|
|
|
375
437
|
class: "flex items-center justify-between gap-2 py-1 text-sm",
|
|
376
438
|
}, [
|
|
377
439
|
h("span", { class: "text-muted-foreground" }, headerText({ column: cell.column })),
|
|
378
|
-
h("span", { class: "text-right" },
|
|
440
|
+
h("span", { class: "text-right" }, formatFieldValue(cell.getValue())),
|
|
379
441
|
])),
|
|
380
442
|
hasActionsColumn
|
|
381
443
|
? h("div", { class: "flex justify-end gap-2 pt-2" }, [
|
|
@@ -407,6 +469,17 @@ export const ResourceTable = defineComponent({
|
|
|
407
469
|
}, { default: () => h(ChevronRightIcon) }),
|
|
408
470
|
]),
|
|
409
471
|
]),
|
|
472
|
+
recordActions.length > 0 || collectionActions.length > 0
|
|
473
|
+
? h(ResourceActionDialog, {
|
|
474
|
+
resourceName: props.resource.name,
|
|
475
|
+
action: activeAction.value?.action ?? null,
|
|
476
|
+
recordId: activeAction.value?.recordId,
|
|
477
|
+
onClose: () => {
|
|
478
|
+
activeAction.value = null;
|
|
479
|
+
},
|
|
480
|
+
onDenied: (action) => denyAction(action, activeAction.value?.recordId),
|
|
481
|
+
})
|
|
482
|
+
: null,
|
|
410
483
|
props.actions
|
|
411
484
|
? h(Dialog, {
|
|
412
485
|
open: createOpen.value,
|