Skip to content

Commit 6a0ab44

Browse files
committed
fix(platform): adjust init
1 parent 031913e commit 6a0ab44

3 files changed

Lines changed: 19 additions & 24 deletions

File tree

packages/platform/src/app/App.tsx

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,45 @@ import type { UserState } from './core/state';
22
import type { DRootProps } from '@react-devui/ui';
33
import type { DLang } from '@react-devui/ui/utils/types';
44

5+
import { isNull } from 'lodash';
56
import { useEffect, useMemo, useState } from 'react';
6-
import { useTranslation } from 'react-i18next';
7-
import { useNavigate } from 'react-router-dom';
87

98
import { useAsync, useMount, useStorage } from '@react-devui/hooks';
109
import { DNotification, DToast } from '@react-devui/ui';
1110
import { DRoot } from '@react-devui/ui';
1211

1312
import { AppRoutes } from './Routes';
14-
import { LOGIN_PATH } from './config/other';
1513
import { STORAGE_KEY } from './config/storage';
16-
import { useHttp, useInit } from './core';
14+
import { TOKEN, useHttp, useInit } from './core';
1715
import { useNotifications, useToasts } from './core/state';
1816

1917
export type AppTheme = 'light' | 'dark';
2018

2119
export function App() {
22-
const { i18n } = useTranslation();
2320
const http = useHttp();
2421
const init = useInit();
25-
const navigate = useNavigate();
2622
const async = useAsync();
27-
const [loading, setLoading] = useState(true);
23+
const [loading, setLoading] = useState(!isNull(TOKEN.value));
2824
const languageStorage = useStorage<DLang>(...STORAGE_KEY.language);
2925
const themeStorage = useStorage<AppTheme>(...STORAGE_KEY.theme);
3026
const [notifications] = useNotifications();
3127
const [toasts] = useToasts();
3228

3329
useMount(() => {
34-
i18n.changeLanguage(languageStorage.value);
35-
36-
http<UserState>({
37-
url: '/auth/me',
38-
method: 'get',
39-
}).subscribe({
40-
next: (res) => {
41-
setLoading(false);
42-
init(res);
43-
},
44-
error: () => {
45-
setLoading(false);
46-
navigate(LOGIN_PATH);
47-
},
48-
});
30+
if (!isNull(TOKEN.value)) {
31+
http<UserState>({
32+
url: '/auth/me',
33+
method: 'get',
34+
}).subscribe({
35+
next: (res) => {
36+
setLoading(false);
37+
init(res);
38+
},
39+
error: () => {
40+
setLoading(false);
41+
},
42+
});
43+
}
4944
});
5045

5146
useEffect(() => {

packages/platform/src/app/core/http/mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ if (environment.http.mock) {
6868
.reply(withDelay(500, [200, (TOKEN as JWTToken<JWTTokenPayload & { admin: boolean }>).payload?.admin ? admin : user]));
6969

7070
for (const username of ['admin', 'user']) {
71-
mock.onPost('/api/v1/login', { username }).reply(() => {
71+
mock.onPost('/api/v1/auth/login', { username }).reply(() => {
7272
return new Promise((resolve) => {
7373
setTimeout(() => {
7474
resolve([

packages/platform/src/app/routes/login/Login.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default function Login(): JSX.Element | null {
6161
const handleSubmit = () => {
6262
setLoginLoading(true);
6363
http<{ user: UserState; token: string }>({
64-
url: '/login',
64+
url: '/auth/login',
6565
method: 'post',
6666
data: { username: accountForm.get('username').value },
6767
}).subscribe({

0 commit comments

Comments
 (0)