Skip to content

Commit f0611e7

Browse files
committed
docs: enhance hooks documentation with examples for data enrichment
1 parent 71c57e2 commit f0611e7

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

  • adminforth/documentation/docs/tutorial/03-Customization

adminforth/documentation/docs/tutorial/03-Customization/04-hooks.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ When user opens edit page, AdminForth makes a request to the backend to get the
4242

4343
![Initial data for edit page flow](get_resource_data.png)
4444

45-
Practically you can use `show.afterDatasourceResponse` to modify or add some data before it is displayed on the edit page.
45+
Practically you can use `show.afterDatasourceResponse` to modify or add some data before it is displayed on the edit page. In other words, at this stage you can enrich the data with some additional metadata which might be handy on edit page for custom Vue fields.
4646

47-
For example [upload plugin](/docs/tutorial/Plugins/upload/) uses this hook to generate signed preview URL so user can see existing uploaded file preview in form, and at the same time database stores only original file path which might be not accessible without presigned URL.
47+
For example [upload plugin](/docs/tutorial/Plugins/upload/) uses this hook to generate signed preview URL so user can see existing uploaded file preview in form, and at the same time database stores only original file path which might be not accessible without presigned URL.
4848

4949
## Saving data on edit page
5050

@@ -177,6 +177,28 @@ For example, you can change the way columns value is displayed by changing the v
177177
}
178178
```
179179

180+
Also you can use this hook to enrich the returned records list with some additional data fields which might be handy for custom Vue components defined in `components.list` or `components.show` for this resource. For example you can use [key-value adapter](/docs/tutorial/06-Adapters/07-key-value-adapters/) to get some global configuration values and add them to each record in the list:
181+
182+
```ts title='./resources/apartments.ts'
183+
184+
{
185+
...
186+
hooks: {
187+
list: {
188+
//diff-add
189+
afterDatasourceResponse: async ({ response }: { response: any }) => {
190+
//diff-add
191+
response.forEach((r: any) => {
192+
const taxRate = await kvAdapter.getValue('taxRate');
193+
r.priceWithTax = r.price * (1 + taxRate);
194+
});
195+
return { ok: true, error: "" };
196+
},
197+
},
198+
},
199+
}
200+
```
201+
180202
### Dropdown list of foreignResource
181203

182204
By default if there is `foreignResource` like we use for demo on `realtor_id` column, the filter will suggest a

0 commit comments

Comments
 (0)