Skip to content

Commit 9281a17

Browse files
authored
Merge pull request #594 from devforth/feature/AdminForth/1558/please-implement-codebase-to-m
fix: enhance URL handling in action configuration to support dynamic …
2 parents 1ac3af6 + 956d35e commit 9281a17

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

adminforth/documentation/docs/tutorial/03-Customization/09-Actions.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,15 @@ Example to generate dynamic URL:
190190
{
191191
name: 'View on Google',
192192
icon: 'flowbite:external-link-solid',
193-
url: async ({record, adminuser}) => `https://google.com/search?q=Apartment ${record.title}`,
193+
url: async ({record, recordId, adminUser, resource }) => `https://google.com/search?q=Apartment ${record.title}`,
194194
showIn: {
195195
list: true,
196196
showButton: true
197197
}
198198
}
199199
```
200200

201-
> ☝️ Note: Though url function might be async we recommend to omit long awaits, or ideally don't use them at all, cause slow execution of this hoock might be a subject of bottleneck for resource pages rendering. For bult actions the async functions would be called in parallel to optimize loading speed.
201+
> ☝️ Note: Though url function might be async we recommend to omit long awaits, or ideally don't use them at all, cause slow execution of this hook might be a subject of bottleneck for resource pages rendering. For built actions the async functions would be called in parallel to optimize loading speed.
202202
203203

204204
### Deep-level redirects.
@@ -207,14 +207,14 @@ Using `url` prop described above is recommended way to implementing URL navigati
207207

208208
However, rearely you might also like to decide whether to redirect only after performing some logic (conditionally). This way is not recommended for most of cases, because it is not compatible with action native features (we can't know URL before executing action body):
209209

210-
```
210+
211211
```ts
212212
{
213213
name: 'View on Google',
214214
icon: 'flowbite:external-link-solid',
215215
action: async ({ recordId }) => {
216216
if (await testSomething(recordId)) {
217-
return { ok: true, redirectURL: 'https://google.com/search?q=apartment' };
217+
return { ok: true, redirectUrl: 'https://google.com/search?q=apartment' };
218218
};
219219
return { ok: true, successMessage: 'Done' };
220220
},

adminforth/modules/restApi.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,6 +2238,11 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
22382238
if (!resource) {
22392239
return { error: await tr(`Resource {resourceId} not found`, 'errors', { resourceId }) };
22402240
}
2241+
2242+
const record = await this.adminforth.connectors[resource.dataSource].getRecordByPrimaryKey(resource, recordId);
2243+
if (!record){
2244+
return { error: `Record with ${recordId} not found` };
2245+
}
22412246
const { allowedActions } = await interpretResource(
22422247
adminUser,
22432248
resource,
@@ -2257,16 +2262,18 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
22572262
}
22582263

22592264
if (action.url) {
2265+
const redirectUrl = typeof action.url === 'function'
2266+
? await action.url({ record, recordId, adminUser, resource })
2267+
: action.url;
22602268
return {
22612269
actionId,
22622270
recordId,
2271+
record,
22632272
resourceId,
2264-
redirectUrl: action.url
2273+
redirectUrl,
22652274
}
22662275
}
2267-
22682276
const actionResponse = await action.action({ recordId, adminUser, resource, tr, adminforth: this.adminforth, response, extra: {...extra, cookies: cookies, headers: headers} });
2269-
22702277
return {
22712278
actionId,
22722279
recordId,

adminforth/types/Back.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@ export interface AdminForthActionInput {
14231423
adminUser: AdminUser;
14241424
standardAllowedActions: AllowedActions;
14251425
}) => boolean | Promise<boolean>);
1426-
url?: string;
1426+
url?: string | ((params: { adminUser: AdminUser; resource: AdminForthResource; recordId: string, record: any }) => string);
14271427
bulkHandler?: (params: {
14281428
adminforth: IAdminForth;
14291429
resource: AdminForthResource;

0 commit comments

Comments
 (0)