From 4298c3fb91484155be45b1d1056d21689d240bbb Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 5 Dec 2023 22:12:54 -0500 Subject: [PATCH] About 1/3rd done with adding redux. --- package.json | 3 + src/client/index.tsx | 18 +++- src/server/handlers/catch-all-handler.tsx | 17 +++- src/shared/components/app/app.tsx | 8 +- src/shared/components/app/error-page.tsx | 9 +- src/shared/components/app/navbar.tsx | 21 ++-- src/shared/components/app/theme.tsx | 5 +- .../components/comment/comment-form.tsx | 5 +- .../components/comment/comment-node.tsx | 24 +++-- .../components/comment/comment-nodes.tsx | 3 + .../components/comment/comment-report.tsx | 3 + src/shared/components/common/error-guard.tsx | 10 +- .../components/common/language-select.tsx | 7 +- .../components/common/markdown-textarea.tsx | 10 +- src/shared/components/common/vote-buttons.tsx | 18 ++-- .../components/community/communities.tsx | 8 +- .../components/community/community-form.tsx | 1 + .../components/community/community-link.tsx | 5 +- src/shared/components/community/community.tsx | 28 ++++-- .../components/community/create-community.tsx | 7 +- src/shared/components/community/sidebar.tsx | 25 +++-- src/shared/components/home/admin-settings.tsx | 8 +- src/shared/components/home/emojis-form.tsx | 6 +- src/shared/components/home/home.tsx | 21 ++-- src/shared/components/home/instances.tsx | 7 +- src/shared/components/home/legal.tsx | 6 +- src/shared/components/home/login-reset.tsx | 6 +- src/shared/components/home/login.tsx | 1 + src/shared/components/home/signup.tsx | 8 +- src/shared/components/home/site-form.tsx | 1 + src/shared/components/modlog.tsx | 13 +-- src/shared/components/person/inbox.tsx | 18 ++-- .../components/person/person-listing.tsx | 5 +- src/shared/components/person/profile.tsx | 37 ++++--- .../person/registration-applications.tsx | 12 ++- src/shared/components/person/reports.tsx | 17 ++-- src/shared/components/person/settings.tsx | 39 +++++--- src/shared/components/person/verify-email.tsx | 6 +- src/shared/components/post/create-post.tsx | 8 +- src/shared/components/post/post-form.tsx | 7 +- src/shared/components/post/post-listing.tsx | 35 ++++--- src/shared/components/post/post.tsx | 18 ++-- .../create-private-message.tsx | 8 +- src/shared/components/remote-fetch.tsx | 7 +- src/shared/components/search.tsx | 7 +- src/shared/interfaces.ts | 2 +- src/shared/services/HttpService.ts | 52 +++++++++- src/shared/services/UnreadCounterService.ts | 2 +- src/shared/services/UserService.ts | 99 ------------------- src/shared/services/index.ts | 1 - src/shared/utils/app/index.ts | 4 +- src/shared/utils/app/initialize-site.ts | 1 + src/shared/utils/app/is-post-blocked.ts | 3 +- src/shared/utils/app/my-auth.ts | 20 +++- src/shared/utils/app/nsfw-check.ts | 5 +- src/shared/utils/app/selectable-languages.ts | 5 +- src/shared/utils/app/set-iso-data.ts | 11 --- src/shared/utils/app/setup-redux.ts | 13 +++ src/shared/utils/app/show-avatars.ts | 6 +- src/shared/utils/app/show-scores.ts | 6 +- .../utils/app/update-community-block.ts | 4 +- src/shared/utils/app/update-instance-block.ts | 4 +- src/shared/utils/app/update-person-block.ts | 4 +- src/shared/utils/app/update-redux.ts | 0 src/shared/utils/roles/am-admin.ts | 6 +- .../utils/roles/am-community-creator.ts | 5 +- src/shared/utils/roles/am-mod.ts | 5 +- src/shared/utils/roles/am-site-creator.ts | 5 +- src/shared/utils/roles/am-top-mod.ts | 5 +- src/shared/utils/roles/can-admin.ts | 5 +- .../utils/roles/can-create-community.ts | 8 +- src/shared/utils/roles/can-mod.ts | 9 +- src/shared/utils/roles/index.ts | 2 + src/shared/utils/roles/moderates-something.ts | 6 ++ yarn.lock | 38 +++++++ 75 files changed, 499 insertions(+), 373 deletions(-) delete mode 100644 src/shared/services/UserService.ts delete mode 100644 src/shared/utils/app/set-iso-data.ts create mode 100644 src/shared/utils/app/setup-redux.ts create mode 100644 src/shared/utils/app/update-redux.ts create mode 100644 src/shared/utils/roles/moderates-something.ts diff --git a/package.json b/package.json index caea727d..da22cd14 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "@babel/preset-typescript": "^7.21.5", "@babel/runtime": "^7.21.5", "@emoji-mart/data": "^1.1.0", + "@reduxjs/toolkit": "^2.0.1", "@shortcm/qr-image": "^9.0.2", "autosize": "^6.0.1", "babel-loader": "^9.1.3", @@ -67,6 +68,7 @@ "inferno-helmet": "^5.2.1", "inferno-hydrate": "^8.2.2", "inferno-i18next-dess": "0.0.2", + "inferno-redux": "^8.2.2", "inferno-router": "^8.2.2", "inferno-server": "^8.2.2", "jwt-decode": "^4.0.0", @@ -83,6 +85,7 @@ "markdown-it-sub": "^1.0.0", "markdown-it-sup": "^1.0.0", "mini-css-extract-plugin": "^2.7.5", + "redux": "^5.0.0", "register-service-worker": "^1.7.2", "run-node-webpack-plugin": "^1.3.0", "rxjs": "^7.8.1", diff --git a/src/client/index.tsx b/src/client/index.tsx index a7af06de..28181048 100644 --- a/src/client/index.tsx +++ b/src/client/index.tsx @@ -1,4 +1,4 @@ -import { initializeSite, setupDateFns } from "@utils/app"; +import { initializeSite, setupDateFns, setupRedux } from "@utils/app"; import { hydrate } from "inferno-hydrate"; import { BrowserRouter } from "inferno-router"; import { App } from "../shared/components/app/app"; @@ -6,16 +6,24 @@ import { App } from "../shared/components/app/app"; import "bootstrap/js/dist/collapse"; import "bootstrap/js/dist/dropdown"; import "bootstrap/js/dist/modal"; +import { Provider } from "inferno-redux"; async function startClient() { - initializeSite(window.isoData.site_res); + const windowData = window.isoData; + delete window.isoData; + + initializeSite(windowData?.site_res); await setupDateFns(); + const store = setupRedux(windowData); + const wrapper = ( - - - + + + + + ); const root = document.getElementById("root"); diff --git a/src/server/handlers/catch-all-handler.tsx b/src/server/handlers/catch-all-handler.tsx index 3466f3cc..22fca585 100644 --- a/src/server/handlers/catch-all-handler.tsx +++ b/src/server/handlers/catch-all-handler.tsx @@ -20,6 +20,8 @@ import { createSsrHtml } from "../utils/create-ssr-html"; import { getErrorPageData } from "../utils/get-error-page-data"; import { setForwardedHeaders } from "../utils/set-forwarded-headers"; import { getJwtCookie } from "../utils/has-jwt-cookie"; +import { Provider } from "inferno-redux"; +import { configureStore, createSlice } from "@reduxjs/toolkit"; export default async (req: Request, res: Response) => { try { @@ -108,10 +110,19 @@ export default async (req: Request, res: Response) => { errorPageData, }; + const slice = createSlice({ + name: "isoData", + initialState: { value: isoData }, + reducers: {}, + }); + const store = configureStore({ reducer: slice.reducer }); + const wrapper = ( - - - + + + + + ); const root = renderToString(wrapper); diff --git a/src/shared/components/app/app.tsx b/src/shared/components/app/app.tsx index 5709f766..0d443368 100644 --- a/src/shared/components/app/app.tsx +++ b/src/shared/components/app/app.tsx @@ -1,4 +1,4 @@ -import { isAnonymousPath, isAuthPath, setIsoData } from "@utils/app"; +import { isAnonymousPath, isAuthPath } from "@utils/app"; import { dataBsTheme } from "@utils/browser"; import { Component, RefObject, createRef, linkEvent } from "inferno"; import { Provider } from "inferno-i18next-dess"; @@ -17,20 +17,20 @@ import AnonymousGuard from "../common/anonymous-guard"; import { CodeTheme } from "./code-theme"; export class App extends Component { - private isoData: IsoDataOptionalSite = setIsoData(this.context); private readonly mainContentRef: RefObject; constructor(props: any, context: any) { super(props, context); this.mainContentRef = createRef(); } - handleJumpToContent(event) { + handleJumpToContent(event: any) { event.preventDefault(); this.mainContentRef.current?.focus(); } render() { - const siteRes = this.isoData.site_res; + const reduxState: IsoDataOptionalSite = this.context.store.getState().value; + const siteRes = reduxState.site_res; const siteView = siteRes?.site_view; return ( diff --git a/src/shared/components/app/error-page.tsx b/src/shared/components/app/error-page.tsx index a0b8d535..6d9acaf1 100644 --- a/src/shared/components/app/error-page.tsx +++ b/src/shared/components/app/error-page.tsx @@ -1,4 +1,3 @@ -import { setIsoData } from "@utils/app"; import { Component } from "inferno"; import { T } from "inferno-i18next-dess"; import { Link } from "inferno-router"; @@ -6,14 +5,13 @@ import { IsoDataOptionalSite } from "../../interfaces"; import { I18NextService } from "../../services"; export class ErrorPage extends Component { - private isoData: IsoDataOptionalSite = setIsoData(this.context); - constructor(props: any, context: any) { super(props, context); } render() { - const { errorPageData } = this.isoData; + const reduxState: IsoDataOptionalSite = this.context.store.getState(); + const errorPageData = reduxState.errorPageData; return (
@@ -41,8 +39,7 @@ export class ErrorPage extends Component {
{I18NextService.i18n.t("error_page_admin_matrix", { instance: - this.isoData.site_res?.site_view.site.name ?? - "this instance", + reduxState.site_res?.site_view.site.name ?? "this instance", })}