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.

feat(tryLoggedAndNormal): Add `loginToContinue` modal for tryLoggedAndNormal guard

haitao(lj)9a28d8f6c61c
diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml
index 35d0f40f..7189c0d3 100644
--- a/i18n/en_US.yaml
+++ b/i18n/en_US.yaml
@@ -608,6 +608,7 @@ ui:
empty: Cannot be empty.
login:
page_title: Welcome to {{site_name}}
+ login_to_continue: Log in to continue
info_sign: Don't have an account? <1>Sign up</1>
info_login: Already have an account? <1>Log in</1>
agreements: By registering, you agree to the <1>privacy policy</1> and <3>terms of service</3>.
diff --git a/ui/src/components/Comment/index.tsx b/ui/src/components/Comment/index.tsx
index 8bf98aae..2df82b81 100644
--- a/ui/src/components/Comment/index.tsx
+++ b/ui/src/components/Comment/index.tsx
@@ -75,6 +75,9 @@ const Comment = ({ objectId, mode, commentId }) => {
}, [data]);
const handleReply = (id) => {
+ if (!tryNormalLogged(true)) {
+ return;
+ }
setComments(
comments.map((item) => {
if (item.comment_id === id) {
@@ -305,7 +308,9 @@ const Comment = ({ objectId, mode, commentId }) => {
variant="link"
className="p-0 fs-14 btn-no-border"
onClick={() => {
- setVisibleComment(!visibleComment);
+ if (tryNormalLogged(true)) {
+ setVisibleComment(!visibleComment);
+ }
}}>
{t('btn_add_comment')}
</Button>
diff --git a/ui/src/components/Header/index.tsx b/ui/src/components/Header/index.tsx
index d18f9ae6..15a6bae9 100644
--- a/ui/src/components/Header/index.tsx
+++ b/ui/src/components/Header/index.tsx
@@ -60,6 +60,7 @@ const Header: FC = () => {
const handleLogout = async () => {
await logout();
clearUserStore();
+ window.location.replace(window.location.href);
};
const onLoginClick = (evt) => {
evt.preventDefault();
diff --git a/ui/src/components/Modal/LoginToContinueModal.tsx b/ui/src/components/Modal/LoginToContinueModal.tsx
new file mode 100644
index 00000000..e96ad71c
--- /dev/null
+++ b/ui/src/components/Modal/LoginToContinueModal.tsx
@@ -0,0 +1,56 @@
+import React from 'react';
+import { Modal } from 'react-bootstrap';
+import { useTranslation } from 'react-i18next';
+import { Link } from 'react-router-dom';
+
+import { loginToContinueStore, siteInfoStore } from '@/stores';
+
+interface IProps {
+ visible: boolean;
+}
+
+const Index: React.FC<IProps> = ({ visible = false }) => {
+ const { t } = useTranslation('translation', { keyPrefix: 'login' });
+ const { update: updateStore } = loginToContinueStore();
+ const { siteInfo } = siteInfoStore((_) => _);
+ const closeModal = () => {
+ updateStore({ show: false });
+ };
+ const linkClick = (evt) => {
+ evt.stopPropagation();
+ closeModal();
+ };
+ return (
+ <Modal
+ title="LoginToContinue"
+ show={visible}
+ onHide={closeModal}
+ centered
+ fullscreen="sm-down">
+ <Modal.Header closeButton>
+ <Modal.Title as="h5">{t('login_to_continue')}</Modal.Title>
+ </Modal.Header>
+ <Modal.Body className="p-5">
+ <div className="d-flex flex-column align-items-center text-center text-body">
+ <h3>{t('page_title', { site_name: siteInfo.name })}</h3>
+ <p>{siteInfo.description}</p>
+ </div>
+ <div className="d-grid gap-2">
+ <Link
+ to="/users/login"
+ className="btn btn-primary"
+ onClick={linkClick}>
+ {t('login', { keyPrefix: 'btns' })}
+ </Link>
+ <Link
+ to="/users/register"
+ className="btn btn-link"
+ onClick={linkClick}>
+ {t('signup', { keyPrefix: 'btns' })}
+ </Link>
+ </div>
+ </Modal.Body>
+ </Modal>
+ );
+};
+export default Index;
diff --git a/ui/src/components/Modal/index.tsx b/ui/src/components/Modal/index.tsx
index a42e4d04..81764934 100644
--- a/ui/src/components/Modal/index.tsx
+++ b/ui/src/components/Modal/index.tsx
@@ -1,6 +1,7 @@
import DefaultModal from './Modal';
import confirm, { Config } from './Confirm';
import PicAuthCodeModal from './PicAuthCodeModal';
+import LoginToContinueModal from './LoginToContinueModal';
type ModalType = typeof DefaultModal & {
confirm: (config: Config) => void;
@@ -14,3 +15,4 @@ Modal.confirm = function (props: Config) {
export default Modal;
export { PicAuthCodeModal };
+export { LoginToContinueModal };
diff --git a/ui/src/components/Operate/index.tsx b/ui/src/components/Operate/index.tsx
index a711d8ed..a4ea5ed1 100644
--- a/ui/src/components/Operate/index.tsx
+++ b/ui/src/components/Operate/index.tsx
@@ -97,7 +97,7 @@ const Index: FC<IProps> = ({
deleteAnswer({
id: aid,
}).then(() => {
- // refersh page
+ // refresh page
toast.onShow({
msg: t('tip_answer_deleted'),
variant: 'success',
diff --git a/ui/src/pages/Layout/index.tsx b/ui/src/pages/Layout/index.tsx
index a8186360..bef2b6da 100644
--- a/ui/src/pages/Layout/index.tsx
+++ b/ui/src/pages/Layout/index.tsx
@@ -4,7 +4,7 @@ import { HelmetProvider } from 'react-helmet-async';
import { SWRConfig } from 'swr';
-import { toastStore } from '@/stores';
+import { toastStore, loginToContinueStore } from '@/stores';
import {
Header,
Footer,
@@ -13,13 +13,14 @@ import {
CustomizeTheme,
PageTags,
} from '@/components';
+import { LoginToContinueModal } from '@/components/Modal';
const Layout: FC = () => {
const { msg: toastMsg, variant, clear: toastClear } = toastStore();
const closeToast = () => {
toastClear();
};
-
+ const { show: showLoginToContinueModal } = loginToContinueStore();
return (
<HelmetProvider>
<PageTags />
@@ -35,6 +36,7 @@ const Layout: FC = () => {
<Toast msg={toastMsg} variant={variant} onClose={closeToast} />
<Footer />
<Customize />
+ <LoginToContinueModal visible={showLoginToContinueModal} />
</SWRConfig>
</HelmetProvider>
);
diff --git a/ui/src/pages/Questions/Detail/components/Question/index.tsx b/ui/src/pages/Questions/Detail/components/Question/index.tsx
index 13f5a6e2..9eeb66e1 100644
--- a/ui/src/pages/Questions/Detail/components/Question/index.tsx
+++ b/ui/src/pages/Questions/Detail/components/Question/index.tsx
@@ -12,7 +12,7 @@ import {
FormatTime,
htmlRender,
} from '@/components';
-import { formatCount } from '@/utils';
+import { formatCount, guard } from '@/utils';
import { following } from '@/services';
import { pathFactory } from '@/router/pathFactory';
@@ -33,6 +33,9 @@ const Index: FC<Props> = ({ data, initPage, hasAnswer, isLogged }) => {
const handleFollow = (e) => {
e.preventDefault();
+ if (!guard.tryNormalLogged(true)) {
+ return;
+ }
following({
object_id: data?.id,
is_cancel: followed,
diff --git a/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx b/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx
index 5f96b422..f1c7abbb 100644
--- a/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx
+++ b/ui/src/pages/Questions/Detail/components/WriteAnswer/index.tsx
@@ -8,6 +8,7 @@ import classNames from 'classnames';
import { Editor, Modal, TextArea } from '@/components';
import { FormDataType } from '@/common/interface';
import { postAnswer } from '@/services';
+import { guard } from '@/utils';
interface Props {
visible?: boolean;
@@ -31,10 +32,13 @@ const Index: FC<Props> = ({ visible = false, data, callback }) => {
},
});
const [showEditor, setShowEditor] = useState<boolean>(visible);
- const [focusType, setForceType] = useState('');
- const [editorFoucsState, setEditorFoucsState] = useState(false);
+ const [focusType, setFocusType] = useState('');
+ const [editorFocusState, setEditorFocusState] = useState(false);
const handleSubmit = () => {
+ if (!guard.tryNormalLogged(true)) {
+ return;
+ }
if (!formData.content.value) {
setFormData({
content: {
@@ -63,6 +67,9 @@ const Index: FC<Props> = ({ visible = false, data, callback }) => {
};
const clickBtn = () => {
+ if (!guard.tryNormalLogged(true)) {
+ return;
+ }
if (data?.answered && !showEditor) {
Modal.confirm({
title: t('confirm_title'),
@@ -82,10 +89,14 @@ const Index: FC<Props> = ({ visible = false, data, callback }) => {
handleSubmit();
};
- const handleFocusForTextArea = () => {
- setForceType('answer');
+ const handleFocusForTextArea = (evt) => {
+ if (!guard.tryNormalLogged(true)) {
+ evt.currentTarget.blur();
+ return;
+ }
+ setFocusType('answer');
setShowEditor(true);
- setEditorFoucsState(true);
+ setEditorFocusState(true);
};
return (
@@ -116,7 +127,7 @@ const Index: FC<Props> = ({ visible = false, data, callback }) => {
focusType === 'answer' && 'focus',
)}
value={formData.content.value}
- autoFocus={editorFoucsState}
+ autoFocus={editorFocusState}
onChange={(val) => {
setFormData({
content: {
@@ -127,10 +138,10 @@ const Index: FC<Props> = ({ visible = false, data, callback }) => {
});
}}
onFocus={() => {
- setForceType('answer');
+ setFocusType('answer');
}}
onBlur={() => {
- setForceType('');
+ setFocusType('');
}}
/>
)}
diff --git a/ui/src/pages/Tags/Detail/index.tsx b/ui/src/pages/Tags/Detail/index.tsx
index 51204ce0..a703f9a4 100644
--- a/ui/src/pages/Tags/Detail/index.tsx
+++ b/ui/src/pages/Tags/Detail/index.tsx
@@ -9,7 +9,7 @@ import { FollowingTags } from '@/components';
import { useTagInfo, useFollow, useQuerySynonymsTags } from '@/services';
import QuestionList from '@/components/QuestionList';
import HotQuestions from '@/components/HotQuestions';
-import { escapeRemove } from '@/utils';
+import { escapeRemove, guard } from '@/utils';
import { pathFactory } from '@/router/pathFactory';
const Questions: FC = () => {
@@ -23,6 +23,9 @@ const Questions: FC = () => {
const { data: followResp } = useFollow(tagFollow);
const { data: synonymsRes } = useQuerySynonymsTags(tagInfo?.tag_id);
const toggleFollow = () => {
+ if (!guard.tryNormalLogged(true)) {
+ return;
+ }
setTagFollow({
is_cancel: tagInfo.is_follower,
object_id: tagInfo.tag_id,
diff --git a/ui/src/pages/Tags/index.tsx b/ui/src/pages/Tags/index.tsx
index 35c25875..a23aa8a0 100644
--- a/ui/src/pages/Tags/index.tsx
+++ b/ui/src/pages/Tags/index.tsx
@@ -6,6 +6,7 @@ import { useTranslation } from 'react-i18next';
import { usePageTags } from '@/hooks';
import { Tag, Pagination, QueryGroup, TagsLoader } from '@/components';
import { formatCount } from '@/utils';
+import { tryNormalLogged } from '@/utils/guard';
import { useQueryTags, following } from '@/services';
const sortBtns = ['popular', 'name', 'newest'];
@@ -35,6 +36,9 @@ const Tags = () => {
};
const handleFollow = (tag) => {
+ if (!tryNormalLogged(true)) {
+ return;
+ }
following({
object_id: tag.tag_id,
is_cancel: tag.is_follower,
diff --git a/ui/src/router/RouteGuard.tsx b/ui/src/router/RouteGuard.tsx
index 0ec79272..60d1249c 100644
--- a/ui/src/router/RouteGuard.tsx
+++ b/ui/src/router/RouteGuard.tsx
@@ -3,7 +3,6 @@ import { useLocation, useNavigate } from 'react-router-dom';
import { floppyNavigation } from '@/utils';
import { TGuardFunc } from '@/utils/guard';
-import { loggedUserInfoStore } from '@/stores';
const Index: FC<{
children: ReactNode;
@@ -17,8 +16,7 @@ const Index: FC<{
}) => {
const navigate = useNavigate();
const location = useLocation();
- const { user } = loggedUserInfoStore();
- const runGuards = () => {
+ const callGuards = () => {
if (onEnter) {
const gr = onEnter();
const redirectUrl = gr.redirect;
@@ -30,13 +28,8 @@ const Index: FC<{
}
};
useEffect(() => {
- runGuards();
+ callGuards();
}, [location]);
- useEffect(() => {
- if (!user.access_token) {
- runGuards();
- }
- }, [user]);
return (
<>
{/* Route Guard */}
diff --git a/ui/src/stores/index.ts b/ui/src/stores/index.ts
index b8acdf73..91b70f52 100644
--- a/ui/src/stores/index.ts
+++ b/ui/src/stores/index.ts
@@ -9,6 +9,7 @@ import brandingStore from './branding';
import pageTagStore from './pageTags';
import customizeStore from './customize';
import themeSettingStore from './themeSetting';
+import loginToContinueStore from './loginToContinue';
export {
toastStore,
@@ -21,4 +22,5 @@ export {
customizeStore,
themeSettingStore,
seoSettingStore,
+ loginToContinueStore,
};
diff --git a/ui/src/stores/loginToContinue.ts b/ui/src/stores/loginToContinue.ts
new file mode 100644
index 00000000..6c9a59b0
--- /dev/null
+++ b/ui/src/stores/loginToContinue.ts
@@ -0,0 +1,16 @@
+import create from 'zustand';
+
+interface IProps {
+ show: boolean;
+ update: (params: { show: boolean }) => void;
+}
+
+const loginToContinueStore = create<IProps>((set) => ({
+ show: false,
+ update: (params) =>
+ set({
+ ...params,
+ }),
+}));
+
+export default loginToContinueStore;
diff --git a/ui/src/utils/guard.ts b/ui/src/utils/guard.ts
index 140e28c0..966c17ee 100644
--- a/ui/src/utils/guard.ts
+++ b/ui/src/utils/guard.ts
@@ -8,6 +8,7 @@ import {
customizeStore,
themeSettingStore,
seoSettingStore,
+ loginToContinueStore,
} from '@/stores';
import { RouteAlias } from '@/router/alias';
import Storage from '@/utils/storage';
@@ -225,7 +226,7 @@ export const tryNormalLogged = (canNavigate: boolean = false) => {
// must assert logged state first and return
if (!us.isLogged) {
if (canNavigate) {
- floppyNavigation.navigateToLogin();
+ loginToContinueStore.getState().update({ show: true });
}
return false;
}