]> Untitled Git - lemmy-ui.git/commitdiff
Fix first loads not working
authorabias <abias1122@gmail.com>
Fri, 16 Jun 2023 22:12:14 +0000 (18:12 -0400)
committerabias <abias1122@gmail.com>
Fri, 16 Jun 2023 22:12:14 +0000 (18:12 -0400)
package.json
src/server/index.tsx
src/shared/components/home/home.tsx
src/shared/components/post/create-post.tsx
src/shared/interfaces.ts
src/shared/routes.ts
src/shared/utils.ts

index 2298d9e140c6e40705f39fd9caf0e5173bc698ae..df264eb03ace26314e24d12bc89bcc6f3b5655cd 100644 (file)
     "start": "yarn build:dev --watch"
   },
   "lint-staged": {
-    "*.{ts,tsx,js}": ["prettier --write", "eslint --fix"],
-    "*.{css, scss}": ["prettier --write"],
-    "package.json": ["sortpack"]
+    "*.{ts,tsx,js}": [
+      "prettier --write",
+      "eslint --fix"
+    ],
+    "*.{css, scss}": [
+      "prettier --write"
+    ],
+    "package.json": [
+      "sortpack"
+    ]
   },
   "dependencies": {
     "@babel/plugin-proposal-decorators": "^7.21.0",
index 783845fb80f57f0885651935846ea7c6cf771e85..4cff98b54afa3d45b473971c9631d6d89c17188e 100644 (file)
@@ -17,13 +17,10 @@ import {
   ILemmyConfig,
   InitialFetchRequest,
   IsoDataOptionalSite,
+  RouteData,
 } from "../shared/interfaces";
 import { routes } from "../shared/routes";
-import {
-  FailedRequestState,
-  RequestState,
-  wrapClient,
-} from "../shared/services/HttpService";
+import { FailedRequestState, wrapClient } from "../shared/services/HttpService";
 import {
   ErrorPageData,
   favIconPngUrl,
@@ -140,7 +137,7 @@ server.get("/*", async (req, res) => {
     // This bypasses errors, so that the client can hit the error on its own,
     // in order to remove the jwt on the browser. Necessary for wrong jwts
     let site: GetSiteResponse | undefined = undefined;
-    let routeData: Record<string, RequestState<any>> = {};
+    let routeData: RouteData = {};
     let errorPageData: ErrorPageData | undefined = undefined;
     let try_site = await client.getSite(getSiteForm);
     if (try_site.state === "failed" && try_site.msg == "not_logged_in") {
@@ -164,7 +161,7 @@ server.get("/*", async (req, res) => {
         return res.redirect("/setup");
       }
 
-      if (site) {
+      if (site && activeRoute?.fetchInitialData) {
         const initialFetchReq: InitialFetchRequest = {
           client,
           auth,
@@ -173,19 +170,7 @@ server.get("/*", async (req, res) => {
           site,
         };
 
-        if (activeRoute?.fetchInitialData) {
-          const routeDataKeysAndVals = await Promise.all(
-            Object.entries(activeRoute.fetchInitialData(initialFetchReq)).map(
-              async ([key, val]) => [key, await val]
-            )
-          );
-
-          routeData = routeDataKeysAndVals.reduce((acc, [key, val]) => {
-            acc[key] = val;
-
-            return acc;
-          }, {});
-        }
+        routeData = await activeRoute.fetchInitialData(initialFetchReq);
       }
     } else if (try_site.state === "failed") {
       errorPageData = getErrorPageData(new Error(try_site.msg), site);
index 4dc797cebabadaa02a38099ce19da22f7b47f822..ed379b846af0374628291939b74d76a565c119f1 100644 (file)
@@ -124,6 +124,39 @@ type HomeData = RouteDataResponse<{
   trendingCommunitiesRes: ListCommunitiesResponse;
 }>;
 
+function getRss(listingType: ListingType) {
+  const { sort } = getHomeQueryParams();
+  const auth = myAuth();
+
+  let rss: string | undefined = undefined;
+
+  switch (listingType) {
+    case "All": {
+      rss = `/feeds/all.xml?sort=${sort}`;
+      break;
+    }
+    case "Local": {
+      rss = `/feeds/local.xml?sort=${sort}`;
+      break;
+    }
+    case "Subscribed": {
+      rss = auth ? `/feeds/front/${auth}.xml?sort=${sort}` : undefined;
+      break;
+    }
+  }
+
+  return (
+    rss && (
+      <>
+        <a href={rss} rel={relTags} title="RSS">
+          <Icon icon="rss" classes="text-muted small" />
+        </a>
+        <link rel="alternate" type="application/atom+xml" href={rss} />
+      </>
+    )
+  );
+}
+
 function getDataTypeFromQuery(type?: string): DataType {
   return type ? DataType[type] : DataType.Post;
 }
@@ -235,11 +268,8 @@ export class Home extends Component<any, HomeState> {
 
     // Only fetch the data if coming from another route
     if (FirstLoadService.isFirstLoad) {
-      const {
-        trendingCommunitiesRes: trendingCommunitiesRes,
-        commentsRes: commentsRes,
-        postsRes: postsRes,
-      } = this.isoData.routeData;
+      const { trendingCommunitiesRes, commentsRes, postsRes } =
+        this.isoData.routeData;
 
       this.state = {
         ...this.state,
@@ -360,7 +390,7 @@ export class Home extends Component<any, HomeState> {
                 ></div>
               )}
               <div className="d-block d-md-none">{this.mobileView}</div>
-              {this.posts()}
+              {this.posts}
             </main>
             <aside className="d-none d-md-block col-md-4">
               {this.mySidebar}
@@ -575,7 +605,7 @@ export class Home extends Component<any, HomeState> {
     await this.fetchData();
   }
 
-  posts() {
+  get posts() {
     const { page } = getHomeQueryParams();
 
     return (
@@ -594,7 +624,7 @@ export class Home extends Component<any, HomeState> {
     const siteRes = this.state.siteRes;
 
     if (dataType === DataType.Post) {
-      switch (this.state.postsRes?.state) {
+      switch (this.state.postsRes.state) {
         case "loading":
           return (
             <h5>
@@ -700,44 +730,11 @@ export class Home extends Component<any, HomeState> {
         <span className="mr-2">
           <SortSelect sort={sort} onChange={this.handleSortChange} />
         </span>
-        {this.getRss(listingType)}
+        {getRss(listingType)}
       </div>
     );
   }
 
-  getRss(listingType: ListingType) {
-    const { sort } = getHomeQueryParams();
-    const auth = myAuth();
-
-    let rss: string | undefined = undefined;
-
-    switch (listingType) {
-      case "All": {
-        rss = `/feeds/all.xml?sort=${sort}`;
-        break;
-      }
-      case "Local": {
-        rss = `/feeds/local.xml?sort=${sort}`;
-        break;
-      }
-      case "Subscribed": {
-        rss = auth ? `/feeds/front/${auth}.xml?sort=${sort}` : undefined;
-        break;
-      }
-    }
-
-    return (
-      rss && (
-        <>
-          <a href={rss} rel={relTags} title="RSS">
-            <Icon icon="rss" classes="text-muted small" />
-          </a>
-          <link rel="alternate" type="application/atom+xml" href={rss} />
-        </>
-      )
-    );
-  }
-
   async fetchTrendingCommunities() {
     this.setState({ trendingCommunitiesRes: { state: "loading" } });
     this.setState({
index a0e439bb8b842c852025cc68631510810cf863f8..05279c35d0d7a47249e3534d943d52e1ecb7b472 100644 (file)
@@ -35,7 +35,7 @@ export interface CreatePostProps {
 }
 
 type CreatePostData = RouteDataResponse<{
-  communityResponse?: GetCommunityResponse;
+  communityResponse: GetCommunityResponse;
   initialCommunitiesRes: ListCommunitiesResponse;
 }>;
 
@@ -244,6 +244,7 @@ export class CreatePost extends Component<
   >): Promise<CreatePostData> {
     const data: CreatePostData = {
       initialCommunitiesRes: await fetchCommunitiesForOptions(client),
+      communityResponse: { state: "empty" },
     };
 
     if (communityId) {
index dbba70406b1314037691fa5b6add9b023138b76d..6afebb62c2a68b6c5ae19acf7dac07c20e7ac671 100644 (file)
@@ -6,16 +6,16 @@ import { ErrorPageData } from "./utils";
 /**
  * This contains serialized data, it needs to be deserialized before use.
  */
-export interface IsoData<T extends Record<string, RequestState<any>> = any> {
+export interface IsoData<T extends RouteData = any> {
   path: string;
   routeData: T;
   site_res: GetSiteResponse;
   errorPageData?: ErrorPageData;
 }
 
-export type IsoDataOptionalSite<
-  T extends Record<string, RequestState<any>> = any
-> = Partial<IsoData<T>> &
+export type IsoDataOptionalSite<T extends RouteData = any> = Partial<
+  IsoData<T>
+> &
   Pick<IsoData<T>, Exclude<keyof IsoData<T>, "site_res">>;
 
 export interface ILemmyConfig {
@@ -82,3 +82,5 @@ export interface CommentNodeI {
   children: Array<CommentNodeI>;
   depth: number;
 }
+
+export type RouteData = Record<string, RequestState<any>>;
index 68ac6a995f7303a22fb79286432a68bcdbaceedc..6e3ed4987edc7062e6c26d76134427b6d1ad619c 100644 (file)
@@ -21,13 +21,10 @@ import { CreatePost } from "./components/post/create-post";
 import { Post } from "./components/post/post";
 import { CreatePrivateMessage } from "./components/private_message/create-private-message";
 import { Search } from "./components/search";
-import { InitialFetchRequest } from "./interfaces";
-import { RequestState } from "./services/HttpService";
+import { InitialFetchRequest, RouteData } from "./interfaces";
 
-interface IRoutePropsWithFetch<T extends Record<string, RequestState<any>>>
-  extends IRouteProps {
-  // TODO Make sure this one is good.
-  fetchInitialData?(req: InitialFetchRequest): T;
+interface IRoutePropsWithFetch<T extends RouteData> extends IRouteProps {
+  fetchInitialData?(req: InitialFetchRequest): Promise<T>;
 }
 
 export const routes: IRoutePropsWithFetch<Record<string, any>>[] = [
index e1a0c14e97b0b81428704f6497cc1f766353c039..97d05fca56933ffe9f9cef648c1f86d630a8eaba 100644 (file)
@@ -43,7 +43,13 @@ import tippy from "tippy.js";
 import Toastify from "toastify-js";
 import { getHttpBase } from "./env";
 import { i18n, languages } from "./i18next";
-import { CommentNodeI, DataType, IsoData, VoteType } from "./interfaces";
+import {
+  CommentNodeI,
+  DataType,
+  IsoData,
+  RouteData,
+  VoteType,
+} from "./interfaces";
 import { HttpService, UserService } from "./services";
 import { RequestState } from "./services/HttpService";
 
@@ -1166,9 +1172,7 @@ export function isBrowser() {
   return typeof window !== "undefined";
 }
 
-export function setIsoData<T extends Record<string, RequestState<any>>>(
-  context: any
-): IsoData<T> {
+export function setIsoData<T extends RouteData>(context: any): IsoData<T> {
   // If its the browser, you need to deserialize the data from the window
   if (isBrowser()) {
     return window.isoData;