From 175d43d067ac452eb84fce697c20861090172e77 Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Fri, 23 Jun 2023 15:41:10 -0400 Subject: [PATCH] Refactor first load handling --- src/shared/components/app/app.tsx | 52 +++++++++++-------- .../components/community/create-community.tsx | 4 +- src/shared/components/home/legal.tsx | 4 +- src/shared/components/home/login.tsx | 4 +- src/shared/components/home/setup.tsx | 4 +- src/shared/components/home/signup.tsx | 4 +- .../components/person/password-change.tsx | 9 +--- src/shared/components/person/settings.tsx | 4 +- src/shared/components/person/verify-email.tsx | 4 +- src/shared/services/FirstLoadService.ts | 8 +++ 10 files changed, 46 insertions(+), 51 deletions(-) diff --git a/src/shared/components/app/app.tsx b/src/shared/components/app/app.tsx index e50a7070..96bf1016 100644 --- a/src/shared/components/app/app.tsx +++ b/src/shared/components/app/app.tsx @@ -4,7 +4,7 @@ import { Provider } from "inferno-i18next-dess"; import { Route, Switch } from "inferno-router"; import { IsoDataOptionalSite } from "../../interfaces"; import { routes } from "../../routes"; -import { I18NextService } from "../../services"; +import { FirstLoadService, I18NextService } from "../../services"; import AuthGuard from "../common/auth-guard"; import ErrorGuard from "../common/error-guard"; import { ErrorPage } from "./error-page"; @@ -45,27 +45,35 @@ export class App extends Component {
- {routes.map(({ path, component: RouteComponent }) => ( - ( - -
- {RouteComponent && - (isAuthPath(path ?? "") ? ( - - - - ) : ( - - ))} -
-
- )} - /> - ))} + {routes.map( + ({ path, component: RouteComponent, fetchInitialData }) => ( + { + if (!fetchInitialData) { + FirstLoadService.falsify(); + } + + return ( + +
+ {RouteComponent && + (isAuthPath(path ?? "") ? ( + + + + ) : ( + + ))} +
+
+ ); + }} + /> + ) + )}
diff --git a/src/shared/components/community/create-community.tsx b/src/shared/components/community/create-community.tsx index aad6630e..2ce5af5a 100644 --- a/src/shared/components/community/create-community.tsx +++ b/src/shared/components/community/create-community.tsx @@ -4,7 +4,7 @@ import { CreateCommunity as CreateCommunityI, GetSiteResponse, } from "lemmy-js-client"; -import { FirstLoadService, HttpService, I18NextService } from "../../services"; +import { HttpService, I18NextService } from "../../services"; import { HtmlTags } from "../common/html-tags"; import { CommunityForm } from "./community-form"; @@ -22,8 +22,6 @@ export class CreateCommunity extends Component { constructor(props: any, context: any) { super(props, context); this.handleCommunityCreate = this.handleCommunityCreate.bind(this); - - FirstLoadService.isFirstLoad; } get documentTitle(): string { diff --git a/src/shared/components/home/legal.tsx b/src/shared/components/home/legal.tsx index 750a91e9..85a413eb 100644 --- a/src/shared/components/home/legal.tsx +++ b/src/shared/components/home/legal.tsx @@ -2,7 +2,7 @@ import { setIsoData } from "@utils/app"; import { Component } from "inferno"; import { GetSiteResponse } from "lemmy-js-client"; import { mdToHtml } from "../../markdown"; -import { FirstLoadService, I18NextService } from "../../services"; +import { I18NextService } from "../../services"; import { HtmlTags } from "../common/html-tags"; interface LegalState { @@ -17,8 +17,6 @@ export class Legal extends Component { constructor(props: any, context: any) { super(props, context); - - FirstLoadService.isFirstLoad; } get documentTitle(): string { diff --git a/src/shared/components/home/login.tsx b/src/shared/components/home/login.tsx index d5dd4d29..1853a82b 100644 --- a/src/shared/components/home/login.tsx +++ b/src/shared/components/home/login.tsx @@ -3,7 +3,7 @@ import { isBrowser } from "@utils/browser"; import { validEmail } from "@utils/helpers"; import { Component, linkEvent } from "inferno"; import { GetSiteResponse, LoginResponse } from "lemmy-js-client"; -import { FirstLoadService, I18NextService, UserService } from "../../services"; +import { I18NextService, UserService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; import { toast } from "../../toast"; import { HtmlTags } from "../common/html-tags"; @@ -32,8 +32,6 @@ export class Login extends Component { constructor(props: any, context: any) { super(props, context); - - FirstLoadService.isFirstLoad; } componentDidMount() { diff --git a/src/shared/components/home/setup.tsx b/src/shared/components/home/setup.tsx index fb4c710f..bed12620 100644 --- a/src/shared/components/home/setup.tsx +++ b/src/shared/components/home/setup.tsx @@ -7,7 +7,7 @@ import { LoginResponse, Register, } from "lemmy-js-client"; -import { FirstLoadService, I18NextService, UserService } from "../../services"; +import { I18NextService, UserService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; import { Spinner } from "../common/icon"; import { SiteForm } from "./site-form"; @@ -47,8 +47,6 @@ export class Setup extends Component { super(props, context); this.handleCreateSite = this.handleCreateSite.bind(this); - - FirstLoadService.isFirstLoad; } async componentDidMount() { diff --git a/src/shared/components/home/signup.tsx b/src/shared/components/home/signup.tsx index 3bb7ea0e..a2d960dc 100644 --- a/src/shared/components/home/signup.tsx +++ b/src/shared/components/home/signup.tsx @@ -14,7 +14,7 @@ import { } from "lemmy-js-client"; import { joinLemmyUrl } from "../../config"; import { mdToHtml } from "../../markdown"; -import { FirstLoadService, I18NextService, UserService } from "../../services"; +import { I18NextService, UserService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; import { toast } from "../../toast"; import { HtmlTags } from "../common/html-tags"; @@ -84,8 +84,6 @@ export class Signup extends Component { super(props, context); this.handleAnswerChange = this.handleAnswerChange.bind(this); - - FirstLoadService.isFirstLoad; } async componentDidMount() { diff --git a/src/shared/components/person/password-change.tsx b/src/shared/components/person/password-change.tsx index 0403c553..dd85e05b 100644 --- a/src/shared/components/person/password-change.tsx +++ b/src/shared/components/person/password-change.tsx @@ -2,12 +2,7 @@ import { myAuth, setIsoData } from "@utils/app"; import { capitalizeFirstLetter } from "@utils/helpers"; import { Component, linkEvent } from "inferno"; import { GetSiteResponse, LoginResponse } from "lemmy-js-client"; -import { - FirstLoadService, - HttpService, - I18NextService, - UserService, -} from "../../services"; +import { HttpService, I18NextService, UserService } from "../../services"; import { RequestState } from "../../services/HttpService"; import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; @@ -35,8 +30,6 @@ export class PasswordChange extends Component { constructor(props: any, context: any) { super(props, context); - - FirstLoadService.isFirstLoad; } get documentTitle(): string { diff --git a/src/shared/components/person/settings.tsx b/src/shared/components/person/settings.tsx index 1ef95f5a..6ed11ef8 100644 --- a/src/shared/components/person/settings.tsx +++ b/src/shared/components/person/settings.tsx @@ -29,7 +29,7 @@ import { SortType, } from "lemmy-js-client"; import { elementUrl, emDash, relTags } from "../../config"; -import { FirstLoadService, UserService } from "../../services"; +import { UserService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; import { I18NextService, languages } from "../../services/I18NextService"; import { setupTippy } from "../../tippy"; @@ -170,8 +170,6 @@ export class Settings extends Component { this.handleBlockPerson = this.handleBlockPerson.bind(this); this.handleBlockCommunity = this.handleBlockCommunity.bind(this); - FirstLoadService.isFirstLoad; - const mui = UserService.Instance.myUserInfo; if (mui) { const { diff --git a/src/shared/components/person/verify-email.tsx b/src/shared/components/person/verify-email.tsx index a13df2d9..1800c3f2 100644 --- a/src/shared/components/person/verify-email.tsx +++ b/src/shared/components/person/verify-email.tsx @@ -1,7 +1,7 @@ import { setIsoData } from "@utils/app"; import { Component } from "inferno"; import { GetSiteResponse, VerifyEmailResponse } from "lemmy-js-client"; -import { FirstLoadService, I18NextService } from "../../services"; +import { I18NextService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; import { toast } from "../../toast"; import { HtmlTags } from "../common/html-tags"; @@ -22,8 +22,6 @@ export class VerifyEmail extends Component { constructor(props: any, context: any) { super(props, context); - - FirstLoadService.isFirstLoad; } async verify() { diff --git a/src/shared/services/FirstLoadService.ts b/src/shared/services/FirstLoadService.ts index 926e3acd..f059e377 100644 --- a/src/shared/services/FirstLoadService.ts +++ b/src/shared/services/FirstLoadService.ts @@ -17,6 +17,10 @@ export class FirstLoadService { return isFirst; } + falsify() { + this.#isFirstLoad = false; + } + static get #Instance() { return this.#instance ?? (this.#instance = new this()); } @@ -24,4 +28,8 @@ export class FirstLoadService { static get isFirstLoad() { return !isBrowser() || this.#Instance.isFirstLoad; } + + static falsify() { + this.#Instance.falsify(); + } }