Skip to content

Commit 1eb3be9

Browse files
docs(plugin-activity-guard): separate setup and usage
1 parent 175a899 commit 1eb3be9

1 file changed

Lines changed: 30 additions & 47 deletions

File tree

‎extensions/plugin-activity-guard/README.md‎

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,74 +24,57 @@ dependencies.
2424

2525
## Setup
2626

27-
Register the plugin with a Guard for each Activity that has an entry policy.
28-
The Activity names and parameters are inferred from your
29-
`@stackflow/config` registration.
30-
31-
```ts
32-
// stackflow.config.ts
33-
import { defineConfig } from "@stackflow/config";
34-
35-
declare module "@stackflow/config" {
36-
interface Register {
37-
Home: {};
38-
Checkout: { orderId: string };
39-
SignIn: { returnTo: string };
40-
Terms: { orderId: string };
41-
}
42-
}
43-
44-
export const config = defineConfig({
45-
activities: [
46-
{ name: "Home" },
47-
{ name: "Checkout" },
48-
{ name: "SignIn" },
49-
{ name: "Terms" },
50-
],
51-
initialActivity: () => "Home",
52-
transitionDuration: 350,
53-
});
54-
```
27+
Add `activityGuardPlugin()` to your Stackflow configuration and map each
28+
guarded Activity to its Guard. This example assumes `Checkout`, `SignIn`, and
29+
`Terms` are already registered in `@stackflow/config`.
5530

5631
```tsx
57-
import type { ActivityGuardFor } from "@stackflow/plugin-activity-guard";
58-
import {
59-
activityGuardPlugin,
60-
all,
61-
redirect,
62-
} from "@stackflow/plugin-activity-guard";
32+
import { activityGuardPlugin } from "@stackflow/plugin-activity-guard";
6333
import { stackflow } from "@stackflow/react";
6434
import { config } from "./stackflow.config";
65-
import { Checkout, Home, SignIn, Terms } from "./activities";
66-
67-
const requireSignIn: ActivityGuardFor<"Checkout"> = ({ activityParams }) =>
68-
isSignedIn()
69-
? true
70-
: redirect("SignIn", { returnTo: activityParams.orderId });
71-
72-
const requireTerms: ActivityGuardFor<"Checkout"> = ({ activityParams }) =>
73-
hasAcceptedTerms()
74-
? true
75-
: redirect("Terms", { orderId: activityParams.orderId });
35+
import { Checkout, SignIn, Terms } from "./activities";
36+
import { checkoutGuard } from "./checkoutGuard";
7637

7738
export const { Stack } = stackflow({
7839
config,
7940
components: {
80-
Home,
8141
Checkout,
8242
SignIn,
8343
Terms,
8444
},
8545
plugins: [
8646
activityGuardPlugin({
8747
guards: {
88-
Checkout: all(requireSignIn, requireTerms),
48+
Checkout: checkoutGuard,
8949
},
9050
}),
9151
],
9252
});
9353
```
9454

55+
## Usage
56+
57+
The following Guard requires both sign-in and terms acceptance before entering
58+
`Checkout`. Activity names and parameters are inferred from your
59+
`@stackflow/config` registration.
60+
61+
```ts
62+
import type { ActivityGuardFor } from "@stackflow/plugin-activity-guard";
63+
import { all, redirect } from "@stackflow/plugin-activity-guard";
64+
65+
const requireSignIn: ActivityGuardFor<"Checkout"> = ({ activityParams }) =>
66+
isSignedIn()
67+
? true
68+
: redirect("SignIn", { returnTo: activityParams.orderId });
69+
70+
const requireTerms: ActivityGuardFor<"Checkout"> = ({ activityParams }) =>
71+
hasAcceptedTerms()
72+
? true
73+
: redirect("Terms", { orderId: activityParams.orderId });
74+
75+
export const checkoutGuard = all(requireSignIn, requireTerms);
76+
```
77+
9578
Each Guard receives the requested `activityName` and its typed
9679
`activityParams`. Return `true` to allow the entry, or return
9780
`redirect(activityName, activityParams)` to replace its target. In the example,

0 commit comments

Comments
 (0)