Skip to content

Avoid reloading app after login/register + remove calling same api requests multiple times. #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions client/packages/lowcoder/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,13 @@ import { CodeEditorTooltipContainer } from "base/codeEditor/codeEditor";
import { ProductLoading } from "components/ProductLoading";
import { language, trans } from "i18n";
import { loadComps } from "comps";
import { fetchHomeData } from "redux/reduxActions/applicationActions";
import { initApp } from "util/commonUtils";
import ApplicationHome from "./pages/ApplicationV2";
import { favicon } from "@lowcoder-ee/assets/images";
import { hasQueryParam } from "util/urlUtils";
import { isFetchUserFinished } from "redux/selectors/usersSelectors";
import { SystemWarning } from "./components/SystemWarning";
import { getBrandingConfig, getSystemConfigFetching } from "./redux/selectors/configSelectors";
import { getBrandingConfig } from "./redux/selectors/configSelectors";
import { buildMaterialPreviewURL } from "./util/materialUtils";
import GlobalInstances from 'components/GlobalInstances';

Expand All @@ -71,35 +70,22 @@ const Wrapper = (props: { children: React.ReactNode }) => (

type AppIndexProps = {
isFetchUserFinished: boolean;
isFetchHomeFinished: boolean;
// isFetchingConfig: boolean;
currentOrgId?: string;
orgDev: boolean;
defaultHomePage: string | null | undefined;
fetchConfig: (orgId?: string) => void;
getCurrentUser: () => void;
fetchHome: () => void;
favicon: string;
brandName: string;
};

class AppIndex extends React.Component<AppIndexProps, any> {
componentDidMount() {
this.props.getCurrentUser();
const { pathname } = history.location;

this.props.fetchConfig(this.props.currentOrgId);

if (pathname === BASE_URL) {
this.props.fetchHome();
}
}

componentDidUpdate(prevProps: AppIndexProps) {
if (history.location.pathname === BASE_URL) {
this.props.fetchHome();
}
if(prevProps.currentOrgId !== this.props.currentOrgId) {
if(prevProps.currentOrgId !== this.props.currentOrgId && this.props.currentOrgId !== '') {
this.props.fetchConfig(this.props.currentOrgId);
}
}
Expand All @@ -108,11 +94,7 @@ class AppIndex extends React.Component<AppIndexProps, any> {
const isTemplate = hasQueryParam("template");
const pathname = history.location.pathname;
// make sure all users in this app have checked login info
if (
!this.props.isFetchUserFinished ||
// this.props.isFetchingConfig ||
(pathname === BASE_URL && !this.props.isFetchHomeFinished)
) {
if (!this.props.isFetchUserFinished) {
const hideLoadingHeader = isTemplate || isAuthUnRequired(pathname);
return <ProductLoading hideHeader={hideLoadingHeader} />;
}
Expand Down Expand Up @@ -185,11 +167,9 @@ class AppIndex extends React.Component<AppIndexProps, any> {

const mapStateToProps = (state: AppState) => ({
isFetchUserFinished: isFetchUserFinished(state),
// isFetchingConfig: getSystemConfigFetching(state),
orgDev: state.ui.users.user.orgDev,
currentOrgId: state.ui.users.user.currentOrgId,
defaultHomePage: state.ui.application.homeOrg?.commonSettings.defaultHomePage,
isFetchHomeFinished: state.ui.application.loadingStatus.fetchHomeDataFinished,
favicon: getBrandingConfig(state)?.favicon
? buildMaterialPreviewURL(getBrandingConfig(state)?.favicon!)
: favicon,
Expand All @@ -201,7 +181,6 @@ const mapDispatchToProps = (dispatch: any) => ({
dispatch(fetchUserAction());
},
fetchConfig: (orgId?: string) => dispatch(fetchConfigAction(orgId)),
fetchHome: () => dispatch(fetchHomeData({})),
});

const AppIndexWithProps = connect(mapStateToProps, mapDispatchToProps)(AppIndex);
Expand Down
10 changes: 1 addition & 9 deletions client/packages/lowcoder/src/pages/ApplicationV2/HomeView.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { useDispatch, useSelector } from "react-redux";
import { useSelector } from "react-redux";
import { HomeLayout } from "./HomeLayout";
import { useEffect } from "react";
import { fetchFolderElements } from "../../redux/reduxActions/folderActions";
import { getUser } from "../../redux/selectors/usersSelectors";
import { folderElementsSelector } from "../../redux/selectors/folderSelector";

export function HomeView() {
const dispatch = useDispatch();

const elements = useSelector(folderElementsSelector)[""];
const user = useSelector(getUser);

useEffect(() => {
dispatch(fetchFolderElements({}));
}, []);

if (!user.currentOrgId) {
return null;
}
Expand Down
18 changes: 14 additions & 4 deletions client/packages/lowcoder/src/pages/userAuth/authUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import {
ThirdPartyAuthType,
ThirdPartyConfigType,
} from "constants/authConstants";
import history from "util/history";

export const AuthContext = createContext<{
systemConfig?: SystemConfig;
inviteInfo?: AuthInviteInfo;
thirdPartyAuthError?: boolean;
fetchUserAfterAuthSuccess?: () => void;
}>(undefined as any);

export const getSafeAuthRedirectURL = (redirectUrl: string | null) => {
Expand All @@ -39,15 +41,21 @@ export const getSafeAuthRedirectURL = (redirectUrl: string | null) => {
export function useAuthSubmit(
requestFunc: () => AxiosPromise<ApiResponse>,
infoCompleteCheck: boolean,
redirectUrl: string | null
redirectUrl: string | null,
onAuthSuccess?: () => void,
) {
const [loading, setLoading] = useState(false);
return {
loading: loading,
onSubmit: () => {
setLoading(true);
requestFunc()
.then((resp) => authRespValidate(resp, infoCompleteCheck, redirectUrl))
.then((resp) => authRespValidate(
resp,
infoCompleteCheck,
redirectUrl,
onAuthSuccess,
))
.catch((e) => {
messageInstance.error(e.message);
})
Expand All @@ -66,7 +74,8 @@ export function useAuthSubmit(
export function authRespValidate(
resp: AxiosResponse<ApiResponse>,
infoCompleteCheck: boolean,
redirectUrl: string | null
redirectUrl: string | null,
onAuthSuccess?: () => void
) {
let replaceUrl = redirectUrl || BASE_URL;
if (infoCompleteCheck) {
Expand All @@ -76,7 +85,8 @@ export function authRespValidate(
: USER_INFO_COMPLETION;
}
if (doValidResponse(resp)) {
window.location.replace(replaceUrl);
onAuthSuccess?.();
history.replace(replaceUrl);
} else if (
resp.data.code === SERVER_ERROR_CODES.EXCEED_MAX_USER_ORG_COUNT ||
resp.data.code === SERVER_ERROR_CODES.ALREADY_IN_ORGANIZATION
Expand Down
5 changes: 3 additions & 2 deletions client/packages/lowcoder/src/pages/userAuth/formLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function FormLogin(props: FormLoginProps) {
const [account, setAccount] = useState("");
const [password, setPassword] = useState("");
const redirectUrl = useRedirectUrl();
const { systemConfig, inviteInfo } = useContext(AuthContext);
const { systemConfig, inviteInfo, fetchUserAfterAuthSuccess } = useContext(AuthContext);
const invitationId = inviteInfo?.invitationId;
const authId = systemConfig?.form.id;
const location = useLocation();
Expand All @@ -49,7 +49,8 @@ export default function FormLogin(props: FormLoginProps) {
authId,
}),
false,
redirectUrl
redirectUrl,
fetchUserAfterAuthSuccess,
);

return (
Expand Down
6 changes: 6 additions & 0 deletions client/packages/lowcoder/src/pages/userAuth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AuthRoutes } from "@lowcoder-ee/constants/authConstants";
import { AuthLocationState } from "constants/authConstants";
import { ProductLoading } from "components/ProductLoading";
import { fetchConfigAction } from "redux/reduxActions/configActions";
import { fetchUserAction } from "redux/reduxActions/userActions";
import _ from "lodash";

export default function UserAuth() {
Expand All @@ -34,12 +35,17 @@ export default function UserAuth() {
return <ProductLoading hideHeader />;
}

const fetchUserAfterAuthSuccess = () => {
dispatch(fetchUserAction());
}

return (
<AuthContext.Provider
value={{
systemConfig: systemConfig,
inviteInfo: location.state?.inviteInfo,
thirdPartyAuthError: location.state?.thirdPartyAuthError,
fetchUserAfterAuthSuccess,
}}
>
<Switch location={location}>
Expand Down
2 changes: 1 addition & 1 deletion client/packages/lowcoder/src/pages/userAuth/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function Login() {
heading={loginHeading}
subHeading={loginSubHeading}
>
{loginCardView}
<FormLogin organizationId={organizationId} />
</AuthContainer>
);
}
Expand Down
12 changes: 5 additions & 7 deletions client/packages/lowcoder/src/pages/userAuth/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ function UserRegister() {
const [password, setPassword] = useState("");
const redirectUrl = useRedirectUrl();
const location = useLocation();
const { systemConfig, inviteInfo } = useContext(AuthContext);
const { systemConfig, inviteInfo, fetchUserAfterAuthSuccess } = useContext(AuthContext);
const invitationId = inviteInfo?.invitationId;
// const invitedOrganizationId = inviteInfo?.invitedOrganizationId;

const orgId = useParams<any>().orgId;
const organizationId = useMemo(() => {
if(inviteInfo?.invitedOrganizationId) {
Expand All @@ -53,6 +53,7 @@ function UserRegister() {
}, [ inviteInfo, orgId ])

const authId = systemConfig?.form.id;

const { loading, onSubmit } = useAuthSubmit(
() =>
UserApi.formLogin({
Expand All @@ -64,13 +65,10 @@ function UserRegister() {
authId,
}),
false,
redirectUrl
redirectUrl,
fetchUserAfterAuthSuccess,
);

if (!systemConfig || !systemConfig?.form.enableRegister) {
return null;
}

const registerHeading = REACT_APP_LOWCODER_CUSTOM_AUTH_WELCOME_TEXT !== ""
? REACT_APP_LOWCODER_CUSTOM_AUTH_WELCOME_TEXT
: trans("userAuth.register")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { AUTH_LOGIN_URL, AUTH_REGISTER_URL, BASE_URL } from "constants/routesURL
import history from "util/history";
import PageSkeleton from "components/PageSkeleton";
import { trans } from "i18n";
import { useEffect, useState } from "react";
import { useContext, useEffect, useState } from "react";
import { getAuthenticator } from "@lowcoder-ee/pages/userAuth/thirdParty/authenticator";
import { AuthRedirectUrlParams } from "pages/userAuth/thirdParty/authenticator";
import { loadAuthParams } from "pages/userAuth/authUtils";
import { AuthContext, loadAuthParams } from "pages/userAuth/authUtils";

function getUrlParams(queryParams: URLSearchParams): AuthRedirectUrlParams {
const ticket = queryParams.get("ticket");
Expand Down Expand Up @@ -53,6 +53,8 @@ export function AuthRedirect() {
const queryParams = new URLSearchParams(location.search);
const urlParam = getUrlParams(queryParams);
const [authParams, setAuthParam] = useState<AuthSessionStoreParams>();
const { fetchUserAfterAuthSuccess } = useContext(AuthContext);

useEffect(() => {
const localAuthParams = loadAuthParams();
if (!localAuthParams) {
Expand All @@ -61,8 +63,9 @@ export function AuthRedirect() {
setAuthParam(localAuthParams);
}
}, []);

if (authParams && validateParam(authParams, urlParam)) {
getAuthenticator(authParams, urlParam).doAuth();
getAuthenticator(authParams, urlParam).doAuth(fetchUserAfterAuthSuccess);
}
return <PageSkeleton hideSideBar />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@ export abstract class AbstractAuthenticator {
this.redirectUrl = decodeURIComponent(getRedirectUrl(authParams.authType));
}

doAuth() {
doAuth(onAuthSuccess?: () => void) {
const { authParams } = this;
(authParams.authGoal === "login" || authParams.authGoal === "register")
? this.doLogin()
? this.doLogin(onAuthSuccess)
: this.doBind();
}

protected doLogin() {
protected doLogin(onAuthSuccess?: () => void) {
this.login()
.then((resp) => {
authRespValidate(
resp,
this.needInfoCheck(this.authParams.sourceType),
this.authParams.afterLoginRedirect
this.authParams.afterLoginRedirect,
onAuthSuccess,
);
})
.catch((e) => {
Expand Down