Skip to content

Commit

Permalink
fix(platform): adjust init
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed Nov 29, 2022
1 parent 031913e commit 6a0ab44
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
39 changes: 17 additions & 22 deletions packages/platform/src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,45 @@ import type { UserState } from './core/state';
import type { DRootProps } from '@react-devui/ui';
import type { DLang } from '@react-devui/ui/utils/types';

import { isNull } from 'lodash';
import { useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';

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

import { AppRoutes } from './Routes';
import { LOGIN_PATH } from './config/other';
import { STORAGE_KEY } from './config/storage';
import { useHttp, useInit } from './core';
import { TOKEN, useHttp, useInit } from './core';
import { useNotifications, useToasts } from './core/state';

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

export function App() {
const { i18n } = useTranslation();
const http = useHttp();
const init = useInit();
const navigate = useNavigate();
const async = useAsync();
const [loading, setLoading] = useState(true);
const [loading, setLoading] = useState(!isNull(TOKEN.value));
const languageStorage = useStorage<DLang>(...STORAGE_KEY.language);
const themeStorage = useStorage<AppTheme>(...STORAGE_KEY.theme);
const [notifications] = useNotifications();
const [toasts] = useToasts();

useMount(() => {
i18n.changeLanguage(languageStorage.value);

http<UserState>({
url: '/auth/me',
method: 'get',
}).subscribe({
next: (res) => {
setLoading(false);
init(res);
},
error: () => {
setLoading(false);
navigate(LOGIN_PATH);
},
});
if (!isNull(TOKEN.value)) {
http<UserState>({
url: '/auth/me',
method: 'get',
}).subscribe({
next: (res) => {
setLoading(false);
init(res);
},
error: () => {
setLoading(false);
},
});
}
});

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/platform/src/app/core/http/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ if (environment.http.mock) {
.reply(withDelay(500, [200, (TOKEN as JWTToken<JWTTokenPayload & { admin: boolean }>).payload?.admin ? admin : user]));

for (const username of ['admin', 'user']) {
mock.onPost('/api/v1/login', { username }).reply(() => {
mock.onPost('/api/v1/auth/login', { username }).reply(() => {
return new Promise((resolve) => {
setTimeout(() => {
resolve([
Expand Down
2 changes: 1 addition & 1 deletion packages/platform/src/app/routes/login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default function Login(): JSX.Element | null {
const handleSubmit = () => {
setLoginLoading(true);
http<{ user: UserState; token: string }>({
url: '/login',
url: '/auth/login',
method: 'post',
data: { username: accountForm.get('username').value },
}).subscribe({
Expand Down

0 comments on commit 6a0ab44

Please sign in to comment.