Skip to content

Commit 308dfdd

Browse files
committed
feat(projects): Synchronize the useRouterPush of soybean
1 parent 95243a0 commit 308dfdd

File tree

8 files changed

+93
-61
lines changed

8 files changed

+93
-61
lines changed
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import CreateRouter from './router';
22
import { useRoute } from './hooks/useRoute';
33
import { useRouter } from './hooks/useRouter';
4-
import type { AfterEach, BeforeEach, RouteLocationNormalizedLoaded } from './types';
54
import type { RouteRecordNormalized } from './matcher/types';
65

76
export default CreateRouter;
87

98
export { useRoute, useRouter };
109

11-
export type { RouteLocationNormalizedLoaded, RouteRecordNormalized, BeforeEach, AfterEach };
10+
export type { RouteRecordNormalized };
11+
12+
export type {
13+
LocationQueryValueRaw,
14+
RouteLocationNamedRaw,
15+
AfterEach,
16+
BeforeEach,
17+
RouteLocationNormalizedLoaded
18+
} from './types';

packages/simple-router/src/router.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CreateRouter {
3333
initRoute = false;
3434
reactRouter: RemixRouter;
3535
initReactRoutes: RouteObject[] = [];
36-
matcher: CreateRouterMatcher;
36+
private matcher: CreateRouterMatcher;
3737
currentRoute = START_LOCATION_NORMALIZED;
3838
getReactRoutes: (route: ElegantConstRoute) => RouteObject;
3939

@@ -67,6 +67,8 @@ class CreateRouter {
6767
await init(this.reactRouter);
6868
this.reactRouter.navigate(this.reactRouter.state.location, { replace: true });
6969
});
70+
71+
this.push = this.push.bind(this);
7072
}
7173

7274
/**

src/components/advanced/DragContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const DragContent: FC<Props> = ({ columns, setColumnChecks }) => {
5050
{...provider.dragHandleProps}
5151
className="h-36px flex-y-center rd-4px hover:(bg-primary bg-opacity-20)"
5252
>
53-
{IconMdiDrag({ className: 'mr-8px h-full cursor-move text-icon' })}
53+
<IconMdiDrag className="mr-8px h-full cursor-move text-icon" />
5454
<Checkbox
5555
checked={item.checked}
5656
onClick={() => handleChange(item.checked, index)}

src/components/advanced/TableColumnSetting.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const TableColumnSetting: FC<Props> = memo(({ content }) => {
1616
content={content}
1717
>
1818
<Button
19-
icon={IconAntDesignSettingOutlined({})}
19+
icon={<IconAntDesignSettingOutlined />}
2020
size="small"
2121
>
2222
{t('common.columnSetting')}

src/constants/app.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ export const loginModuleRecord: Record<UnionKey.LoginModule, App.I18n.I18nKey> =
1212
'pwd-login': 'page.login.pwdLogin.title',
1313
'code-login': 'page.login.codeLogin.title',
1414
register: 'page.login.register.title',
15-
'reset-pwd': 'page.login.resetPwd.title',
16-
'bind-wechat': 'page.login.bindWeChat.title'
15+
'reset-pwd': 'page.login.resetPwd.title'
1716
};
1817

1918
export const themeLayoutModeRecord: Record<UnionKey.ThemeLayoutMode, App.I18n.I18nKey> = {

src/hooks/common/login.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ export function useLogin() {
1919

2020
if (userName) {
2121
dispatch(initAuthRoute());
22-
if (redirect) {
23-
redirectFromLogin();
24-
}
22+
await redirectFromLogin(redirect);
2523
window.$notification?.success({
2624
message: t('page.login.common.loginSuccess'),
2725
description: t('page.login.common.welcomeBack', { userName })

src/hooks/common/routerPush.ts

Lines changed: 76 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,43 @@
1-
import { useSearchParams } from 'react-router-dom';
21
import { startTransition } from 'react';
3-
import { router } from '@/router';
2+
import { useRouter } from '@sa/simple-router';
3+
import type { RouteKey } from '@elegant-router/types';
4+
import type { RouteLocationNamedRaw } from '@sa/simple-router';
5+
6+
interface RouterPushOptions {
7+
query?: Record<string, string>;
8+
params?: Record<string, string>;
9+
}
410

511
/**
612
* Router push
713
*
814
* Jump to the specified route, it can replace function router.push
915
*/
1016
export function useRouterPush() {
17+
const router = useRouter();
18+
1119
const [searchParams] = useSearchParams();
12-
const { pathname } = useLocation();
1320

14-
const routerBack = () => {
15-
router.reactRouter.navigate(-1);
16-
};
21+
const routerPush = router.push;
1722

18-
async function toHome() {
19-
return router.push('/');
20-
}
23+
const routerBack = router.back;
2124

22-
function getQuery() {
23-
const query: Record<string, string> = {};
24-
for (const pairs of searchParams.entries()) {
25-
query[pairs[0]] = pairs[1];
26-
}
27-
return query;
28-
}
25+
async function routerPushByKey(key: RouteKey, options?: RouterPushOptions) {
26+
const { query, params } = options || {};
2927

30-
/**
31-
* Navigate to login page
32-
*
33-
* @param loginModule The login module
34-
* @param redirectUrl The redirect url, if not specified, it will be the current route fullPath
35-
*/
36-
async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) {
37-
const module = loginModule || 'pwd-login';
28+
const routeLocation: RouteLocationNamedRaw = {
29+
name: key
30+
};
3831

39-
const redirect = redirectUrl || pathname;
32+
if (Object.keys(query || {}).length) {
33+
routeLocation.query = query;
34+
}
4035

41-
return router.reactRouter.navigate({ pathname: `/login/${module}`, search: `redirect=${redirect}` });
42-
}
43-
function objectToQueryParams(obj: Record<string, string | number>) {
44-
const params = new URLSearchParams();
45-
for (const key in obj) {
46-
if (Object.hasOwn(obj, key)) {
47-
params.append(key, obj[key] as string);
48-
}
36+
if (Object.keys(params || {}).length) {
37+
routeLocation.params = params;
4938
}
50-
return params.toString();
39+
40+
return routerPush(routeLocation);
5141
}
5242

5343
/**
@@ -67,41 +57,77 @@ export function useRouterPush() {
6757
return query;
6858
};
6959

70-
const menuPush = (key: string) => {
71-
startTransition(() => {
72-
const query = getRouteQueryOfMetaByKey(key);
73-
router.push({ name: key, query });
74-
});
75-
};
60+
function routerPushByKeyWithMetaQuery(key: RouteKey) {
61+
const query = getRouteQueryOfMetaByKey(key);
62+
63+
return routerPushByKey(key, { query });
64+
}
65+
66+
async function toHome() {
67+
return routerPush('/');
68+
}
69+
70+
/**
71+
* Navigate to login page
72+
*
73+
* @param loginModule The login module
74+
* @param redirectUrl The redirect url, if not specified, it will be the current route fullPath
75+
*/
76+
async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) {
77+
const module = loginModule || 'pwd-login';
78+
79+
const options: RouterPushOptions = {
80+
params: {
81+
module
82+
}
83+
};
84+
85+
const redirect = redirectUrl || router.currentRoute.fullPath;
86+
87+
options.query = {
88+
redirect
89+
};
90+
91+
return routerPushByKey('login', options);
92+
}
7693

7794
/**
7895
* Toggle login module
7996
*
8097
* @param module
8198
*/
82-
function toggleLoginModule(module: UnionKey.LoginModule) {
83-
const query = getQuery();
99+
async function toggleLoginModule(module: UnionKey.LoginModule) {
100+
const query = router.currentRoute.query as Record<string, string>;
101+
102+
return routerPushByKey(`login_${module}`, { query });
103+
}
84104

85-
router.reactRouter.navigate({
86-
pathname: `/login/${module}`,
87-
search: objectToQueryParams(query)
105+
function menuPush(key: string) {
106+
startTransition(() => {
107+
routerPushByKeyWithMetaQuery(key as RouteKey);
88108
});
89109
}
90110

91-
/** Redirect from login */
92-
function redirectFromLogin() {
111+
/**
112+
* Redirect from login
113+
*
114+
* @param [needRedirect=true] Whether to redirect after login. Default is `true`
115+
*/
116+
async function redirectFromLogin(needRedirect = true) {
93117
const redirect = searchParams.get('redirect');
94118

95-
if (redirect) {
96-
router.reactRouter.navigate(redirect);
119+
if (needRedirect && redirect) {
120+
routerPush(redirect);
97121
} else {
98122
toHome();
99123
}
100124
}
125+
101126
return {
102-
routerPush: router,
127+
routerPush,
103128
routerBack,
104129
toLogin,
130+
routerPushByKeyWithMetaQuery,
105131
menuPush,
106132
redirectFromLogin,
107133
toggleLoginModule

src/types/union-key.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ declare namespace UnionKey {
99
* - reset-pwd: reset password
1010
* - bind-wechat: bind wechat
1111
*/
12-
type LoginModule = 'pwd-login' | 'code-login' | 'register' | 'reset-pwd' | 'bind-wechat';
12+
type LoginModule = 'pwd-login' | 'code-login' | 'register' | 'reset-pwd';
1313

1414
/** Theme scheme */
1515
type ThemeScheme = 'light' | 'dark' | 'auto';

0 commit comments

Comments
 (0)