Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix: remove redundant code

shuai4a123b933d42
diff --git a/ui/src/components/FollowingTags/index.tsx b/ui/src/components/FollowingTags/index.tsx
index 06fec6ec..9e3156b0 100644
--- a/ui/src/components/FollowingTags/index.tsx
+++ b/ui/src/components/FollowingTags/index.tsx
@@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
import { NavLink } from 'react-router-dom';
import { TagSelector, Tag } from '@/components';
-import { tryNormalLogged } from '@/utils/guard';
+import { tryLoggedAndActicevated } from '@/utils/guard';
import { useFollowingTags, followTags } from '@/services';
const Index: FC = () => {
@@ -32,7 +32,7 @@ const Index: FC = () => {
});
};
- if (!tryNormalLogged()) {
+ if (!tryLoggedAndActicevated().ok) {
return null;
}
diff --git a/ui/src/pages/Users/AccountForgot/index.tsx b/ui/src/pages/Users/AccountForgot/index.tsx
index e7a77ea5..7af609cc 100644
--- a/ui/src/pages/Users/AccountForgot/index.tsx
+++ b/ui/src/pages/Users/AccountForgot/index.tsx
@@ -1,8 +1,7 @@
-import React, { useState, useEffect } from 'react';
+import React, { useState } from 'react';
import { Container, Col } from 'react-bootstrap';
import { Trans, useTranslation } from 'react-i18next';
-import { tryNormalLogged } from '@/utils/guard';
import { PageTitle } from '@/components';
import SendEmail from './components/sendEmail';
@@ -17,10 +16,6 @@ const Index: React.FC = () => {
setEmail(mail);
};
- useEffect(() => {
- tryNormalLogged();
- }, []);
-
return (
<>
<PageTitle title={t('account_recovery', { keyPrefix: 'page_title' })} />
diff --git a/ui/src/pages/Users/Login/index.tsx b/ui/src/pages/Users/Login/index.tsx
index b8794ff4..7a499e64 100644
--- a/ui/src/pages/Users/Login/index.tsx
+++ b/ui/src/pages/Users/Login/index.tsx
@@ -158,8 +158,6 @@ const Index: React.FC = () => {
if ((storeUser.id && storeUser.mail_status === 2) || isInactive) {
setStep(2);
- } else {
- guard.tryNormalLogged();
}
}, []);
diff --git a/ui/src/pages/Users/PasswordReset/index.tsx b/ui/src/pages/Users/PasswordReset/index.tsx
index b97bd707..ab00de30 100644
--- a/ui/src/pages/Users/PasswordReset/index.tsx
+++ b/ui/src/pages/Users/PasswordReset/index.tsx
@@ -1,4 +1,4 @@
-import React, { FormEvent, useState, useEffect } from 'react';
+import React, { FormEvent, useState } from 'react';
import { Container, Col, Form, Button } from 'react-bootstrap';
import { Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
@@ -7,7 +7,6 @@ import { loggedUserInfoStore } from '@/stores';
import { getQueryString } from '@/utils';
import type { FormDataType } from '@/common/interface';
import { replacementPassword } from '@/services';
-import { tryNormalLogged } from '@/utils/guard';
import { PageTitle } from '@/components';
const Index: React.FC = () => {
@@ -115,9 +114,6 @@ const Index: React.FC = () => {
});
};
- useEffect(() => {
- tryNormalLogged();
- }, []);
return (
<>
<PageTitle title={t('account_recovery', { keyPrefix: 'page_title' })} />
diff --git a/ui/src/pages/Users/Register/index.tsx b/ui/src/pages/Users/Register/index.tsx
index e4d94b2d..811de77d 100644
--- a/ui/src/pages/Users/Register/index.tsx
+++ b/ui/src/pages/Users/Register/index.tsx
@@ -1,9 +1,8 @@
-import React, { useState, useEffect } from 'react';
+import React, { useState } from 'react';
import { Container } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { PageTitle, Unactivate } from '@/components';
-import { tryNormalLogged } from '@/utils/guard';
import SignUpForm from './components/SignUpForm';
@@ -15,10 +14,6 @@ const Index: React.FC = () => {
setShowForm((bol) => !bol);
};
- useEffect(() => {
- tryNormalLogged();
- }, []);
-
return (
<Container style={{ paddingTop: '4rem', paddingBottom: '5rem' }}>
<h3 className="text-center mb-5">{t('page_title')}</h3>
diff --git a/ui/src/services/client/notification.ts b/ui/src/services/client/notification.ts
index a849db0a..6d34022b 100644
--- a/ui/src/services/client/notification.ts
+++ b/ui/src/services/client/notification.ts
@@ -3,7 +3,7 @@ import qs from 'qs';
import request from '@/utils/request';
import type * as Type from '@/common/interface';
-import { tryNormalLogged } from '@/utils/guard';
+import { tryLoggedAndActicevated } from '@/utils/guard';
export const useQueryNotifications = (params) => {
const apiUrl = `/answer/api/v1/notification/page?${qs.stringify(params, {
@@ -33,7 +33,7 @@ export const useQueryNotificationStatus = () => {
const apiUrl = '/answer/api/v1/notification/status';
return useSWR<{ inbox: number; achievement: number }>(
- tryNormalLogged() ? apiUrl : null,
+ tryLoggedAndActicevated().ok ? apiUrl : null,
request.instance.get,
{
refreshInterval: 3000,
diff --git a/ui/src/services/client/tag.ts b/ui/src/services/client/tag.ts
index 42b9f1ac..b1ec3a9a 100644
--- a/ui/src/services/client/tag.ts
+++ b/ui/src/services/client/tag.ts
@@ -2,7 +2,7 @@ import useSWR from 'swr';
import request from '@/utils/request';
import type * as Type from '@/common/interface';
-import { tryNormalLogged } from '@/utils/guard';
+import { tryLoggedAndActicevated } from '@/utils/guard';
export const deleteTag = (id) => {
return request.delete('/answer/api/v1/tag', {
@@ -24,7 +24,7 @@ export const saveSynonymsTags = (params) => {
export const useFollowingTags = () => {
let apiUrl = '';
- if (tryNormalLogged()) {
+ if (tryLoggedAndActicevated().ok) {
apiUrl = '/answer/api/v1/tags/following';
}
const { data, error, mutate } = useSWR<any[]>(apiUrl, request.instance.get);
diff --git a/ui/src/utils/common.ts b/ui/src/utils/common.ts
index dcc2d084..31bb0a40 100644
--- a/ui/src/utils/common.ts
+++ b/ui/src/utils/common.ts
@@ -85,6 +85,7 @@ function formatUptime(value) {
return `< 1 ${t('dates.hour')}`;
}
+
export {
getQueryString,
thousandthDivision,
diff --git a/ui/src/utils/guard.ts b/ui/src/utils/guard.ts
index 2e123bf7..7f926fba 100644
--- a/ui/src/utils/guard.ts
+++ b/ui/src/utils/guard.ts
@@ -154,9 +154,9 @@ export const admin = () => {
/**
* try user was logged and all state ok
- * @param autoLogin
+ * @param canNavigate // if true, will navigate to login page if not logged
*/
-export const tryNormalLogged = (autoLogin: boolean = false) => {
+export const tryNormalLogged = (canNavigate: boolean = false) => {
const us = deriveLoginState();
if (us.isNormal) {
@@ -164,7 +164,7 @@ export const tryNormalLogged = (autoLogin: boolean = false) => {
}
// must assert logged state first and return
if (!us.isLogged) {
- if (autoLogin) {
+ if (canNavigate) {
floppyNavigation.navigateToLogin();
}
return false;
@@ -182,6 +182,16 @@ export const tryNormalLogged = (autoLogin: boolean = false) => {
return false;
};
+export const tryLoggedAndActicevated = () => {
+ const gr: TGuardResult = { ok: true };
+ const us = deriveLoginState();
+ console.log('tryLogged', us);
+ if (!us.isLogged || !us.isActivated) {
+ gr.ok = false;
+ }
+ return gr;
+};
+
export const initAppSettingsStore = async () => {
const appSettings = await getAppSettings();
if (appSettings) {