William Nemenchainitial commit
86fb32e9/11/2022, 1:13:39 AM
.ts
TypeScript
(application/typescript)
// 3rd-party
import type { ViewContext } from "@ethicdevs/fastify-stream-react-views";
import type { FastifyReply, FastifyRequest } from "fastify";
// app
import type { CommonViewProps } from "../../types";
import { Const } from "../../const";

export const makeRequestHandler = {
  getter: () => {
    return (request: FastifyRequest, reply: FastifyReply) => {
      const { sectionSlug, pageSlug } = request.params as {
        sectionSlug?: string;
        pageSlug?: string;
      };

      return <T extends Record<string, unknown>>(
        viewName: string,
        props?: T & CommonViewProps,
        viewCtx?: ViewContext
      ) => {
        /*console.log(
          "----",
          request.namedViewsPathMap,
          request.pathNamedViewsMap
        );*/

        // const routerPath = request.routerPath.trim();
        // const viewNameFromPath = request.pathNamedViewsMap[routerPath];
        // const viewPathFromName = request.namedViewsPathMap[viewNameFromPath];
        // const viewNameFromPath2 = request.pathNamedViewsMap[viewPathFromName];

        /*console.log(
          "-> ",
          "viewName:",
          viewName,
          "routerPath:",
          routerPath,
          "viewPathFromName:",
          viewPathFromName,
          "viewNameFromPath:",
          viewNameFromPath,
          "viewNameFromPath2:",
          viewNameFromPath2
        );*/

        const viewProps: T & { commonProps: CommonViewProps } = {
          ...props,
          commonProps: {
            title: props?.title,
            currentSectionSlug: sectionSlug,
            currentPageSlug: pageSlug,
            menuDefinition: request.sectionsWithPages,
            themeScheme:
              (request.cookies?.["theme_scheme"]?.split(".")?.[0] ||
                Const.DEFAULT_THEME_SCHEME) === "light"
                ? "light"
                : "dark",
          },
        } as T & { commonProps: CommonViewProps };

        return reply.streamReactView(viewName, viewProps as any, viewCtx);
      };
    };
  },
};