All files / client/src/app AppRoutes.tsx

92.15% Statements 47/51
29.72% Branches 11/37
100% Functions 12/12
91.3% Lines 42/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302                                                          356x 356x 356x 356x 356x 356x 356x 356x 356x 356x 356x 356x 356x 356x 356x     356x 356x 356x     356x 356x 356x 356x     356x 356x 356x     356x 476x   1639x 1639x 1639x 1639x 1639x 476x 1639x   476x 1639x     476x                                 476x                                         476x                             476x                                                                                             461x 347x                                                                                                                                                                                                                                                      
import { ReactElement } from "react";
import { Navigate, Route, Routes } from "react-router";
import { useTranslation } from "react-i18next";
import { IconName } from "@fortawesome/free-solid-svg-icons";
import { AllProgramItemsView } from "client/views/all-program-items/AllProgramItemsView";
import { ProgramItemView } from "client/views/program-item/ProgramItemView";
import { LoginView } from "client/views/login/LoginView";
import { MyProgramItemsView } from "client/views/my-program-items/MyProgramItemsView";
import { RegistrationView } from "client/views/registration/RegistrationView";
import { AdminView } from "client/views/admin/AdminView";
import { LogoutView } from "client/views/logout/LogoutView";
import { GroupView } from "client/views/group/GroupView";
import { HelperView } from "client/views/helper/HelperView";
import { useAppSelector } from "client/utils/hooks";
import { config } from "shared/config";
import { isAdmin, isAdminOrHelper } from "client/utils/checkUserGroup";
import { AboutView } from "client/views/about/AboutView";
import { FaqView } from "client/views/about/FaqView";
import { Tabs } from "client/components/Tabs";
import { EventLog } from "client/views/event-log/EventLog";
import { ProfileView } from "client/views/profile/ProfileView";
import { InstructionsView } from "client/views/about/InstructionsView";
import { KompassiLoginCallback } from "client/components/KompassiLoginCallback";
import { LoginProvider } from "shared/config/eventConfigTypes";
import { KompassiLogoutCallback } from "client/components/KompassiLogoutCallback";
import { AuthEndpoint } from "shared/constants/apiEndpoints";
import { AdmissionTicketView } from "client/views/admission-ticket/AdmissionTicketView";
import { FinalizeRegistration } from "client/views/login/components/FinalizeRegistration";
 
export enum AppRoute {
  ROOT = "/",
  PROGRAM = "/program",
  PROGRAM_ITEM = "/program/item",
  HELPER = "/helper",
  ADMIN = "/admin",
  ADMIN_LOGIN = "/admin/login",
  PROFILE = "/profile",
  REGISTRATION = "/registration",
  LOGIN = "/login",
  LOGOUT = "/logout",
  ABOUT = "/about",
  NOTIFICATIONS = "/notifications",
  KOMPASSI_LOGOUT_CALLBACK = "/kompassi-logout-callback",
  ANY = "/*",
}
 
export enum ProgramTab {
  MY_PROGRAM = "/program/myprogram",
  PROGRAM_LIST = "/program/list",
}
 
export enum AboutTab {
  HELP = "/about/help",
  FAQ = "/about/faq",
  ABOUT = "/about/about",
}
 
export enum ProfileTab {
  PROFILE = "/profile/profile",
  GROUP = "/profile/group",
}
 
export const AppRoutes = (): ReactElement => {
  const { t } = useTranslation();
 
  const appOpen = useAppSelector((state) => state.admin.appOpen);
  const loginProvider = useAppSelector((state) => state.admin.loginProvider);
  const loggedIn = useAppSelector((state) => state.login.loggedIn);
  const userGroup = useAppSelector((state) => state.login.userGroup);
  const kompassiId = useAppSelector((state) => state.login.kompassiId);
  const kompassiUsernameAccepted = useAppSelector(
    (state) => state.login.kompassiUsernameAccepted,
  );
  const emailNotificationPermitAsked = useAppSelector(
    (state) => state.login.emailNotificationPermitAsked,
  );
 
  const programTabs = [
    {
      headerText: t("pages.myProgram"),
      path: ProgramTab.MY_PROGRAM,
      element: <MyProgramItemsView />,
      icon: "dice" as IconName,
      "data-testid": "my-program-tab",
    },
    {
      headerText: t("pages.programList"),
      path: ProgramTab.PROGRAM_LIST,
      element: <AllProgramItemsView />,
      icon: "calendar-days" as IconName,
      "data-testid": "program-list-tab",
    },
  ];
 
  const aboutTabs = [
    {
      headerText: t("aboutView.instructions"),
      path: AboutTab.HELP,
      element: <InstructionsView />,
      icon: "person-chalkboard" as IconName,
    },
    {
      headerText: t("aboutView.faq"),
      path: AboutTab.FAQ,
      element: <FaqView />,
      icon: "question" as IconName,
    },
    {
      headerText: t("aboutView.about"),
      path: AboutTab.ABOUT,
      element: <AboutView />,
      icon: "info" as IconName,
    },
  ];
 
  const profileTabs = [
    {
      headerText: t("profileView.profileTab"),
      path: ProfileTab.PROFILE,
      element: <ProfileView />,
      icon: "user" as IconName,
    },
    {
      headerText: t("profileView.groupTab"),
      path: ProfileTab.GROUP,
      element: <GroupView />,
      icon: "users" as IconName,
    },
  ];
 
  if (!appOpen) {
    return (
      <Routes>
        {isAdmin(userGroup) && (
          <Route path={AppRoute.ADMIN} element={<AdminView />} />
        )}
        {isAdminOrHelper(userGroup) && (
          <Route path={AppRoute.HELPER} element={<HelperView />} />
        )}
        {loggedIn && (
          <>
            <Route
              path={AppRoute.PROGRAM}
              element={<Navigate replace to={ProgramTab.PROGRAM_LIST} />}
            />
            <Route
              path={ProgramTab.PROGRAM_LIST}
              element={<AllProgramItemsView />}
            />
            <Route
              path={`${AppRoute.PROGRAM_ITEM}/:programItemId`}
              element={<ProgramItemView />}
            />
            <Route path={AppRoute.NOTIFICATIONS} element={<EventLog />} />
            <Route path={AppRoute.PROFILE} element={<ProfileView />} />
          </>
        )}
        {!loggedIn && <Route path={AppRoute.LOGIN} element={<LoginView />} />}
        {!loggedIn && (
          <Route path={AppRoute.ADMIN_LOGIN} element={<LoginView />} />
        )}
        <Route path={AppRoute.LOGOUT} element={<LogoutView />} />
        <Route
          path={`${AppRoute.ABOUT}/*`}
          element={<Tabs tabContents={aboutTabs} />}
        />
        <Route
          path={AppRoute.ROOT}
          element={
            loggedIn ? <Navigate to={ProgramTab.PROGRAM_LIST} /> : <div />
          }
        />
        <Route path={AppRoute.ANY} element={<Navigate to={AppRoute.ROOT} />} />
      </Routes>
    );
  }
 
  if (loggedIn) {
    if (
      (kompassiId !== 0 && !kompassiUsernameAccepted) ||
      !emailNotificationPermitAsked
    ) {
      return (
        <Routes>
          <Route path={AppRoute.LOGOUT} element={<LogoutView />} />
          <Route
            path={AppRoute.ANY}
            element={
              <FinalizeRegistration
                kompassiUsernameAccepted={kompassiUsernameAccepted}
                emailNotificationPermitAsked={emailNotificationPermitAsked}
              />
            }
          />
          <Route
            path={AppRoute.KOMPASSI_LOGOUT_CALLBACK}
            element={<KompassiLogoutCallback />}
          />
        </Routes>
      );
    }
 
    return (
      <Routes>
        {isAdminOrHelper(userGroup) ? (
          <Route
            path={ProgramTab.PROGRAM_LIST}
            element={<AllProgramItemsView />}
          />
        ) : (
          <Route
            path={`${AppRoute.PROGRAM}/*`}
            element={<Tabs tabContents={programTabs} />}
          />
        )}
        <Route
          path={`${AppRoute.PROGRAM_ITEM}/:programItemId`}
          element={<ProgramItemView />}
        />
        <Route path={AppRoute.NOTIFICATIONS} element={<EventLog />} />
        {!isAdminOrHelper(userGroup) && config.event().enableGroups ? (
          <Route
            path={`${AppRoute.PROFILE}/*`}
            element={<Tabs tabContents={profileTabs} />}
          />
        ) : (
          <Route path={AppRoute.PROFILE} element={<ProfileView />} />
        )}
        {isAdminOrHelper(userGroup) && (
          <Route path={AppRoute.PROFILE} element={<ProfileView />} />
        )}
        {isAdmin(userGroup) && (
          <Route path={AppRoute.ADMIN} element={<AdminView />} />
        )}
        <Route path={AppRoute.LOGOUT} element={<LogoutView />} />
        <Route
          path={AppRoute.KOMPASSI_LOGOUT_CALLBACK}
          element={<KompassiLogoutCallback />}
        />
        {/* Login path is required for after login redirect to work */}
        <Route path={AppRoute.LOGIN} element={<LoginView />} />
        <Route path={AppRoute.ADMIN_LOGIN} element={<LoginView />} />
        {isAdminOrHelper(userGroup) && (
          <Route path={AppRoute.HELPER} element={<HelperView />} />
        )}
        <Route
          path={`${AppRoute.ABOUT}/*`}
          element={<Tabs tabContents={aboutTabs} />}
        />
        {isAdminOrHelper(userGroup) ? (
          <Route
            path={AppRoute.ROOT}
            element={<Navigate to={ProgramTab.PROGRAM_LIST} />}
          />
        ) : (
          <Route
            path={AppRoute.ROOT}
            element={<Navigate to={ProgramTab.MY_PROGRAM} />}
          />
        )}
        <Route
          path={`${AppRoute.PROGRAM_ITEM}/:programItemId/admission`}
          element={<AdmissionTicketView />}
        />
        <Route path={AppRoute.ANY} element={<Navigate to={AppRoute.ROOT} />} />
      </Routes>
    );
  }
 
  return (
    <Routes>
      <Route path={AppRoute.LOGIN} element={<LoginView />} />
      <Route path={AppRoute.ADMIN_LOGIN} element={<LoginView />} />
      {loginProvider !== LoginProvider.KOMPASSI && (
        <Route path={AppRoute.REGISTRATION} element={<RegistrationView />} />
      )}
      <Route
        path={AppRoute.PROGRAM}
        element={<Navigate replace to={ProgramTab.PROGRAM_LIST} />}
      />
      <Route path={ProgramTab.PROGRAM_LIST} element={<AllProgramItemsView />} />
      <Route
        path={`${AppRoute.PROGRAM_ITEM}/:programItemId`}
        element={<ProgramItemView />}
      />
      <Route
        path={`${AppRoute.ABOUT}/*`}
        element={<Tabs tabContents={aboutTabs} />}
      />
      <Route
        path={AppRoute.ROOT}
        element={<Navigate to={AppRoute.PROGRAM} />}
      />
      <Route
        path={AuthEndpoint.KOMPASSI_LOGIN_CALLBACK}
        element={<KompassiLoginCallback />}
      />
      <Route path={AppRoute.ANY} element={<Navigate to={AppRoute.LOGIN} />} />
    </Routes>
  );
};