Skip to content

Commit 02e1b3f

Browse files
authored
Merge branch 'main' into patch-1
2 parents 6e11a34 + e20a07c commit 02e1b3f

File tree

7 files changed

+88
-69
lines changed

7 files changed

+88
-69
lines changed

docs/api/create-route.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ import { createRoute } from 'atomic-router';
2323
export const homeRoute = createRoute();
2424
export const postRoute = createRoute<{ postId: string }>();
2525

26-
homeRoute.open();
27-
postRoute.open({ postId: '123' });
26+
// prefer `sample` over imperative events call
27+
homeRoute.open(); // Effect
28+
postRoute.open({ postId: '123' }); // Effect<{ postId: string }>
2829

2930
postRoute.$params.watch(console.log);
3031
```
@@ -36,21 +37,29 @@ postRoute.$params.watch(console.log);
3637
Open the route with specified params and query
3738

3839
```ts
39-
postRoute.navigate({
40-
params: { postId: '123' },
41-
query: { foo: 'bar' },
42-
});
40+
sample({
41+
clock: someThingHappened,
42+
fn: () => ({
43+
params: { postId: '123' },
44+
query: { foo: 'bar' },
45+
}),
46+
target: postRoute.navigate,
47+
})
4348
// /posts/:postId -> /posts/123?foo=bar
4449
```
4550

4651
You can also add `replace: true` option to do `history.replace` instead of `history.push`:
4752

4853
```ts
49-
postRoute.navigate({
50-
params: { postId: '123' },
51-
query: { foo: 'bar' },
52-
replace: true,
53-
});
54+
sample({
55+
clock: someThingHappened,
56+
fn: () => ({
57+
params: { postId: '123' },
58+
query: { foo: 'bar' },
59+
replace: true,
60+
}),
61+
target: postRoute.navigate,
62+
})
5463
```
5564

5665
**Signature:** `Effect<RouteParamsAndQuery<RouteParams> & { replace?: boolean }, RouteParamsAndQuery<RouteParams>>`
@@ -60,7 +69,11 @@ postRoute.navigate({
6069
The same as `.navigate` but with params only
6170

6271
```ts
63-
postRoute.open({ postId: '123' });
72+
sample({
73+
clock: somethingHappened,
74+
fn: () => ({ postId: '123' }),
75+
target: postRoute.open,
76+
})
6477
// /posts/:postId -> /posts/123
6578
```
6679

@@ -121,7 +134,7 @@ sample({
121134
Detects whether the route is currently opened or not.
122135

123136
```ts
124-
postRoute.$isOpened.getState(); // true/false
137+
postRoute.$isOpened; // true/false
125138
```
126139

127140
**Signature:** `Store<boolean>`
@@ -133,7 +146,7 @@ Current route params. These params are used as tokens for URL set in **router**.
133146
If route is not opened, `$params` will be `{}`
134147

135148
```ts
136-
postRoute.$params.getState(); // { postId: 123 }
149+
postRoute.$params; // { postId: 123 }
137150
```
138151

139152
**Signature:** `Store<RouteParams>`
@@ -145,7 +158,7 @@ Current route query. These are used to build [**Query String**](https://en.wikip
145158
If route is not opened, `$query` will be `{}`
146159

147160
```ts
148-
postRoute.$query.getState(); // { foo: 'bar' }
161+
postRoute.$query; // { foo: 'bar' }
149162
```
150163

151164
**Signature:** `Store<RouteQuery>`

docs/api/create-router-controls.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,37 @@ Create controls:
2525
// @/shared/routing
2626
import { createRouterControls } from 'atomic-router';
2727

28-
export const controls = createRouterControls()
28+
export const controls = createRouterControls();
2929

30-
controls.$query // Store<RouteQuery> Store with current query params
31-
controls.back // Event<void> Trigger it to call history.back
32-
controls.forward // Event<void> Trigger it to call history.forward
30+
controls.$query; // StoreWritable<RouteQuery> Store with current query params
31+
controls.back; // EventCallable<void> Trigger it to call history.back
32+
controls.forward; // EventCallable<void> Trigger it to call history.forward
3333
```
3434

3535
Pass it to `createHistoryRouter`:
3636
```ts
3737
// @/app/router
38-
import { createHistoryRouter } from 'atomic-router'
38+
import { createHistoryRouter } from 'atomic-router';
39+
import { controls } from '@/shared/routing';
3940

40-
import { controls } from '@/shared/routing'
41-
42-
const routes = [/* ... */]
41+
const routesMap = [/* ... */];
4342

4443
export const router = createHistoryRouter({
45-
routes,
46-
controls
47-
})
44+
routes: routesMap,
45+
controls,
46+
});
4847
```
4948

5049
Use it anywhere:
5150
```ts
5251
// @/pages/register
5352
import { sample, createEvent } from 'effector'
54-
5553
import { controls } from '@/shared/routing'
5654

57-
const cancelButtonPressed = createEvent<MouseEvent>()
55+
export const cancelButtonPressed = createEvent();
5856

5957
sample({
6058
clock: cancelButtonPressed,
61-
target: controls.back
62-
})
59+
target: controls.back,
60+
});
6361
```

docs/api/create-router.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,37 @@ import { createHistoryRouter } from 'atomic-router';
99
## Usage
1010

1111
```ts
12+
import { sample } from 'effector';
1213
import { createHistoryRouter } from 'atomic-router';
1314
import { createBrowserHistory, createMemoryHistory } from 'history';
1415

1516
import { homeRoute } from '@/pages/home';
1617
import { postsRoute } from '@/pages/posts';
1718
import { postRoute } from '@/pages/post';
1819

19-
// 1. Define routes
20-
const routes = [
20+
import { appStarted } from '@/shared/init';
21+
22+
// 1. Define routes and URLs map
23+
const routesMap = [
2124
{ path: '/', route: homeRoute },
2225
{ path: '/posts', route: postsRoute },
2326
{ path: '/posts/:postId', route: postRoute },
2427
];
2528

2629
// 2. Create router
2730
const router = createHistoryRouter({
28-
routes: routes,
31+
routes: routesMap,
2932
});
3033

3134
// 3. Create history
32-
const history = isSsr ? createMemoryHistory() : createBrowserHistory();
35+
const history = isSSR ? createMemoryHistory() : createBrowserHistory();
3336

3437
// 4. Attach it to router
35-
router.setHistory(history);
38+
sample({
39+
clock: appStarted,
40+
fn: () => history,
41+
target: router.setHistory,
42+
})
3643
```
3744

3845
## Handling 404 errors

docs/api/query-sync.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ import { querySync } from 'atomic-router';
1515
### Basic example
1616

1717
```ts
18-
import { createStore } from 'effector'
19-
import { createRoute, querySync } from 'atomic-router'
18+
import { createStore } from 'effector';
19+
import { querySync } from 'atomic-router';
2020

21-
import { controls } from '@/shared/routing'
21+
import { controls } from '@/shared/routing';
2222

23-
const $utmSource = createStore("")
23+
const $utmSource = createStore("");
2424

2525
querySync({
2626
source: { utm_source: $utmSource },
27-
controls
28-
})
27+
controls,
28+
});
2929
```
3030

3131
This will
@@ -37,20 +37,20 @@ This will
3737
You can pass `route` param if you want this sync to work only for a specific route:
3838

3939
```ts
40-
import { createStore } from 'effector'
41-
import { createRoute, querySync } from 'atomic-router'
40+
import { createStore } from 'effector';
41+
import { createRoute, querySync } from 'atomic-router';
4242

43-
import { controls } from '@/shared/routing'
43+
import { controls } from '@/shared/routing';
4444

45-
const searchRoute = createRoute()
45+
const searchRoute = createRoute();
4646

47-
const $q = createStore("")
47+
const $q = createStore("");
4848

4949
querySync({
5050
source: { q: $q },
51-
route: searchRoute,
52-
controls
53-
})
51+
route: searchRoute,
52+
controls,
53+
});
5454
```
5555

5656
### With a `clock`
@@ -59,29 +59,29 @@ If we don't want to spam route updates, there's a `clock` parameter.
5959
If it's passed, `querySync` will update route only when `clock` it's triggered:
6060

6161
```ts
62-
import { createRoute, querySync } from 'atomic-router'
63-
import { createStore, createEvent } from 'effector'
62+
import { createRoute, querySync } from 'atomic-router';
63+
import { createStore, createEvent } from 'effector';
6464

65-
import { controls } from '@/shared/routing'
65+
import { controls } from '@/shared/routing';
6666

67-
const canvasEditorRoute = createRoute()
67+
const canvasEditorRoute = createRoute();
6868

69-
const canvasDragged = createEvent()
70-
const canvasDragEnded = createEvent()
69+
const canvasDragged = createEvent();
70+
const canvasDragEnded = createEvent();
7171

72-
const $x = createStore('0')
73-
const $y = createStore('0')
72+
const $x = createStore('0');
73+
const $y = createStore('0');
7474

75-
$x.on(canvasDragged, (_, { x }) => x)
75+
$x.on(canvasDragged, (_, { x }) => x);
7676

77-
$y.on(canvasDragged, (_, { y }) => y)
77+
$y.on(canvasDragged, (_, { y }) => y);
7878

7979
querySync({
8080
source: { x: $x, y: $y },
8181
route: canvasEditorRoute,
8282
clock: canvasDragEnded,
83-
controls
84-
})
83+
controls,
84+
});
8585
```
8686

8787
## `cleanup` parameter
@@ -97,10 +97,10 @@ querySync({
9797
// Strip empty params ('', 0, false, null)
9898
empty: true,
9999
// Preserves params that should've been removed by irerelevant/empty params
100-
preserve: ['utm_source']
100+
preserve: ['utm_source'],
101101
},
102-
controls
103-
})
102+
controls,
103+
});
104104
```
105105

106106
- `cleanup` is optional. Default strategy is `{ irrelevant: true, empty: false, preserve: [] }`

docs/examples/protected-route.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ Then, we can create a simple wrapper:
3232

3333
```ts
3434
// @/shared/route
35-
import { chainRoute, RouteInstance, RouteParamsAndQuery } from 'atomic-router';
35+
import { chainRoute, RouteParams, RouteInstance, RouteParamsAndQuery } from 'atomic-router';
3636
import { createEvent } from 'effector';
3737

3838
import { $isAuthorized, tokenReceived } from '@/shared/auth';
3939

40-
export function chainAuthorized<Params>(route: RouteInstance<Params>) {
40+
export function chainAuthorized<Params extends RouteParams>(route: RouteInstance<Params>) {
4141
const sessionCheckStarted = createEvent<RouteParamsAndQuery<Params>>();
4242

4343
const alreadyAuthorized = sample({

docs/react/api/create-routes-view.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import { router } from '@/app/routing';
2525

2626
const RoutesView = createRoutesView({
2727
routes: [
28-
{ route: Home.route, view: HomePage.view },
29-
{ route: Post.route, view: PostPage.view },
28+
{ route: HomePage.route, view: HomePage.view },
29+
{ route: HomePage.route, view: PostPage.view },
3030
],
3131
otherwise() {
3232
return <div>Page not found!</div>;

docs/react/scope.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Example:
88
// No scope
99
import { Route, createRoutesView } from 'atomic-router-react';
1010

11-
// Scoped imports (correct for SSR)
11+
// Scoped imports (correct for effector < 23 and atomic-router-react < 0.9)
12+
// On newer versions you can safely use `atomic-router-react` without `/scope` in the SSR or SPA
1213
import { Route, createRoutesView } from 'atomic-router-react/scope';
1314
```

0 commit comments

Comments
 (0)