Added RequestPermission event (#261)

* add action

* add event

* changeset

* Remove eve

* remove event

* Cleanup

* remove commnets
This commit is contained in:
Lukasz Ostrowski 2023-07-25 09:11:34 +02:00 committed by GitHub
parent 8eae1614f2
commit 1118ea9c0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 13 deletions

View file

@ -0,0 +1,11 @@
---
"@saleor/app-sdk": minor
---
Added new actions and events for dynamic permissions request.
Now App can ask Dashboard to grant more permissions than originally assigned.
Operation can be approved or rejected by the user.
This feature is available in Saleor 3.15 and higher

View file

@ -177,7 +177,7 @@ handleRedirect();
### Available actions
| Action | Arguments | Description |
| :-------------- | :--------------------------------------------------------------- | :--------------------------------------- |
| :---------------------------- | :----------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `Redirect` | `to` (string) - relative (inside Dashboard) or absolute URL path | |
| | `newContext` (boolean) - should open in a new browsing context | |
| `Notification` | `status` (`info` / `success` / `warning` / `error` / undefined) | |
@ -186,3 +186,4 @@ handleRedirect();
| | `apiMessage` (string / undefined) - error log from api | |
| `NotifyReady` | | Inform Dashboard that AppBridge is ready |
| `UpdateRouting` | `newRoute` - current path of App to be set in URL | |
| `RequestPermissions` (>=3.15) | `permissions` - array of AppPermission, `redirectPath` - path app will be redirected to after operation ends | Ask Dashboard to give more permissions to the app. Dashboard will unmount app. After user approves or denies, Dashboard will redirect to `redirectPath`. If operation fails, `?error=REASON` will be appended |

View file

@ -1,5 +1,6 @@
import { v4 as uuidv4 } from "uuid";
import { AppPermission } from "../types";
import { Values } from "./helpers";
// Using constants over Enums, more info: https://fettblog.eu/tidy-typescript-avoid-enums/
@ -20,6 +21,12 @@ export const ActionType = {
* Inform Dashboard that AppBridge is ready
*/
notifyReady: "notifyReady",
/**
* Request one or more permissions from the Dashboard
*
* Available from 3.15
*/
requestPermission: "requestPermissions",
} as const;
export type ActionType = Values<typeof ActionType>;
@ -110,11 +117,38 @@ function createNotifyReadyAction(): NotifyReady {
});
}
export type Actions = RedirectAction | NotificationAction | UpdateRouting | NotifyReady;
export type RequestPermissions = ActionWithId<
"requestPermissions",
{
permissions: AppPermission[];
redirectPath: string;
}
>;
function createRequestPermissionsAction(
permissions: AppPermission[],
redirectPath: string
): RequestPermissions {
return withActionId({
type: "requestPermissions",
payload: {
permissions,
redirectPath,
},
});
}
export type Actions =
| RedirectAction
| NotificationAction
| UpdateRouting
| NotifyReady
| RequestPermissions;
export const actions = {
Redirect: createRedirectAction,
Notification: createNotificationAction,
UpdateRouting: createUpdateRoutingAction,
NotifyReady: createNotifyReadyAction,
RequestPermissions: createRequestPermissionsAction,
};

View file

@ -19,8 +19,6 @@ type SubscribeMap = {
const debug = debugPkg.debug("app-sdk:AppBridge");
function eventStateReducer(state: AppBridgeState, event: Events) {
debug("Event reducer received event: %j", event);
switch (event.type) {
case EventType.handshake: {
const userJwtPayload = extractUserFromJwt(event.payload.token);