fix: remove redundant code

This commit is contained in:
shuai 2022-11-08 17:11:19 +08:00
parent 41f83ab175
commit 4a123b933d
9 changed files with 23 additions and 28 deletions

View File

@ -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;
}

View File

@ -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' })} />

View File

@ -158,8 +158,6 @@ const Index: React.FC = () => {
if ((storeUser.id && storeUser.mail_status === 2) || isInactive) {
setStep(2);
} else {
guard.tryNormalLogged();
}
}, []);

View File

@ -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' })} />

View File

@ -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>

View File

@ -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,

View File

@ -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);

View File

@ -85,6 +85,7 @@ function formatUptime(value) {
return `< 1 ${t('dates.hour')}`;
}
export {
getQueryString,
thousandthDivision,

View File

@ -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) {