Untitled

 avatar
unknown
plain_text
3 years ago
2.8 kB
6
Indexable
/* eslint-disable no-undef */
import React, { useEffect, useState, useCallback } from "react";
import { Toast, Page } from "@teer/ui";
import { useI18n } from "@cc/i18n";
import { useScope } from "@teer/utils";
import { useParams } from "react-router-dom";
import { removeParamsFromURLString, removeRequiredAndOptionalParamsFromURLString } from "@cc/util/cvUtil";
import CustomizationHomeTile from "../components/CustomizationHomeTile";

// Parent landing
const CustomNavPage = () => {
  const scope = useScope();
  const { nav } = useParams();
  const [pageTitle, setPageTitle] = useState();
  const [navChildren, setNavChildren] = useState();
  const [error, setError] = useState();
  const { t } = useI18n([
    "subscriptions",
    "messagesJs"
  ]);
  const acTitle = t("label.adminConsole");

  const redirectToDefaultPage = useCallback(() => {
    let { defaultPath } = cv.nav;
    if (defaultPath) {
      defaultPath = removeParamsFromURLString("_cid", defaultPath);
      defaultPath = removeParamsFromURLString("_cn", defaultPath);
    }
    if (!defaultPath.includes("subscriptions") && !defaultPath.includes("/reportDetails")) {
      defaultPath = removeRequiredAndOptionalParamsFromURLString(defaultPath);
    }

    window.location.hash = (defaultPath && !cv.isAdmin && !cv.isMspUser) ? "/uservms" : "/dashboard/commcell";
  }, []);

  // eslint-disable-next-line max-statements
  useEffect(() => {
    const routesForNav = scope?.cvNavigationFactory?.getRouteForState(nav) || window.getRouteForState || [];
    if (!routesForNav) {
      setError(true);
      Toast({
        type: "error",
        position: "top-right",
        text: t("warn.accessDeniedToPage")
      });
      redirectToDefaultPage();
      return;
    }
    if (!routesForNav?.children?.length) {
      setError(true);
      Toast({
        type: "error",
        position: "top-right",
        text: t("warn.noAppsToShow")
      });
      redirectToDefaultPage();
      return;
    }
    const newPageTitle = routesForNav.cvTitle || routesForNav.pageTitle || acTitle;
    window.document.title = newPageTitle;
    setPageTitle(newPageTitle);
    const formattedNavChildren = routesForNav?.children?.map((child) => {
      return {
        description: child?.description,
        title: child?.cvTitle,
        url: child?.url?.split("?")[0],
        svg: child?.svg // If a child isn't migrated to react then we won't have the icon available
      };
    });
    setNavChildren(formattedNavChildren);
  }, []);

  return (
    <Page id="Custom_Nav" title={pageTitle || t("label.nav.masterCustomization")}>
      {!error && <CustomizationHomeTile landingTiles={navChildren || []} /> }
    </Page>
  );
};

export default CustomNavPage;
Editor is loading...