From 3aac93f7a17c7adecb7cf28d5d56e24ff53f331c Mon Sep 17 00:00:00 2001 From: Marek Mateja Date: Mon, 29 May 2023 15:47:09 +0200 Subject: [PATCH 1/5] Add ability to refetch permissions on demand --- packages/ra-core/src/auth/usePermissions.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/ra-core/src/auth/usePermissions.ts b/packages/ra-core/src/auth/usePermissions.ts index d293c3cdf61..a9160765bca 100644 --- a/packages/ra-core/src/auth/usePermissions.ts +++ b/packages/ra-core/src/auth/usePermissions.ts @@ -19,7 +19,7 @@ const emptyParams = {}; * * @param {Object} params Any params you want to pass to the authProvider * - * @returns The current auth check state. Destructure as { permissions, error, isLoading }. + * @returns The current auth check state. Destructure as { permissions, error, isLoading, refetch }. * * @example * import { usePermissions } from 'react-admin'; @@ -54,6 +54,7 @@ const usePermissions = ( permissions: result.data, isLoading: result.isLoading, error: result.error, + refetch: result.refetch, }), [result] ); From 38ac43d488422510f9f8770890377f52296882c5 Mon Sep 17 00:00:00 2001 From: Marek Mateja Date: Tue, 30 May 2023 10:39:20 +0200 Subject: [PATCH 2/5] Add documentation for permission reloading --- docs/usePermissions.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/usePermissions.md b/docs/usePermissions.md index ed7feccd9a1..8aed8e71d6f 100644 --- a/docs/usePermissions.md +++ b/docs/usePermissions.md @@ -61,6 +61,32 @@ const MyPage = () => { } ``` +## Refreshing permissions + +Permissions are loaded on page load and cached. If your application requires permissions to be refreshed, for example after a change modifying user permissions, you can use `refetch` function to trigger reload. + +```jsx +const GrantAdminPermissionsButton = () => { + const record = useRecordContext(); + const [ update ] = useUpdate(); + const { refetch } = usePermissions(); + + const handleClick = () => { + update( + "users", + { id: record.id, data: { admin: true }, previousData: record }, + ); + refetch(); + } + + return ( + + ) +} +``` + ## RBAC [The ra-rbac module](https://marmelab.com/ra-enterprise/modules/ra-rbac) provides an alternative implementation of the `usePermissions` hook. It returns an array of permissions, resulting in the merge of the user permissions and the permissions from the user roles. From fcf7364e4eee6cc9f79c0eee7188180c52f804d7 Mon Sep 17 00:00:00 2001 From: Marek Mateja Date: Tue, 30 May 2023 12:31:24 +0200 Subject: [PATCH 3/5] Update wording in refreshing permissions doc Co-authored-by: Jean-Baptiste Kaiser --- docs/usePermissions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/usePermissions.md b/docs/usePermissions.md index 8aed8e71d6f..e66afab741c 100644 --- a/docs/usePermissions.md +++ b/docs/usePermissions.md @@ -63,7 +63,7 @@ const MyPage = () => { ## Refreshing permissions -Permissions are loaded on page load and cached. If your application requires permissions to be refreshed, for example after a change modifying user permissions, you can use `refetch` function to trigger reload. +Permissions are loaded when the app loads and then cached. If your application requires permissions to be refreshed, for example after a change modifying user permissions, you can use `refetch` function to trigger reload. ```jsx const GrantAdminPermissionsButton = () => { From a1f491617d24c01374300599f7be1ccde677245c Mon Sep 17 00:00:00 2001 From: Marek Mateja Date: Tue, 30 May 2023 12:32:43 +0200 Subject: [PATCH 4/5] Move refetch to onSuccess in doc example Co-authored-by: Jean-Baptiste Kaiser --- docs/usePermissions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/usePermissions.md b/docs/usePermissions.md index e66afab741c..0de6c9da804 100644 --- a/docs/usePermissions.md +++ b/docs/usePermissions.md @@ -75,8 +75,8 @@ const GrantAdminPermissionsButton = () => { update( "users", { id: record.id, data: { admin: true }, previousData: record }, + { onSuccess: refetch }, ); - refetch(); } return ( From 3349c8cdbcc64df97e988e227c3c120fbc8895d4 Mon Sep 17 00:00:00 2001 From: Marek Mateja Date: Tue, 30 May 2023 12:34:45 +0200 Subject: [PATCH 5/5] Wrap code example with `raw` to avoid issues with Jekyll --- docs/usePermissions.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/usePermissions.md b/docs/usePermissions.md index 0de6c9da804..62afd5d799a 100644 --- a/docs/usePermissions.md +++ b/docs/usePermissions.md @@ -65,6 +65,7 @@ const MyPage = () => { Permissions are loaded when the app loads and then cached. If your application requires permissions to be refreshed, for example after a change modifying user permissions, you can use `refetch` function to trigger reload. +{% raw %} ```jsx const GrantAdminPermissionsButton = () => { const record = useRecordContext(); @@ -86,6 +87,7 @@ const GrantAdminPermissionsButton = () => { ) } ``` +{% endraw %} ## RBAC