Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix redux typescript #715

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/components/accessories/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createSelector } from "@reduxjs/toolkit";
import { Chart, registerables } from "chart.js";
import { useAppSelector } from "libraries/hooks/redux";
import React from "react";
Expand All @@ -10,12 +11,15 @@ import "./styles.scss";

Chart.register(...registerables);

const appSelector = createSelector(
(state) => state.main.authentication.data,
(userCredentials) => ({ userCredentials })
);

const Dashboard = () => {
const { t } = useTranslation();

const { userCredentials } = useAppSelector((state) => ({
userCredentials: state.main.authentication.data,
}));
const { userCredentials } = useAppSelector(appSelector);
Comment on lines +14 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alefalezza what is wrong with the actual implementation?


const breadcrumbMap = {
[t("nav.dashboard")]: "",
Expand Down
65 changes: 33 additions & 32 deletions src/components/activities/loginActivity/RedirectAfterLogin.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import { useAppSelector } from "libraries/hooks/redux";
import React, { useMemo } from "react";
import { Navigate, useLocation } from "react-router";
import { useLandingPageRoute } from "../../../libraries/hooks/useLandingPageRoute";
import { IRedirectAfterLogin } from "./types";

export const RedirectAfterLogin: React.FC<IRedirectAfterLogin> = ({
children,
}) => {
const location = useLocation();
const landingPageRoute = useLandingPageRoute();
const to = useMemo(
() => location.state?.from || landingPageRoute,
[landingPageRoute, location]
);

const state = useAppSelector((state) => state);

const status = useMemo(
() =>
["SUCCESS", "FAIL"].includes(state.main.settings.status!)
? state.main.authentication.status!
: state.main.settings.status!,
[state.main.settings.status, state.main.authentication.status]
);

if (status === "SUCCESS") {
return <Navigate to={to} />;
}

return <>{children}</>;
};
import { useAppSelector } from "libraries/hooks/redux";
import React, { useMemo } from "react";
import { Navigate, useLocation } from "react-router";
import { useLandingPageRoute } from "../../../libraries/hooks/useLandingPageRoute";
import { IMainState } from "../../../state/main";
import { IRedirectAfterLogin } from "./types";

export const RedirectAfterLogin: React.FC<IRedirectAfterLogin> = ({
children,
}) => {
const location = useLocation();
const landingPageRoute = useLandingPageRoute();
const to = useMemo(
() => location.state?.from || landingPageRoute,
[landingPageRoute, location]
);

const state: IMainState = useAppSelector((state) => state.main);

const status = useMemo(
() =>
["SUCCESS", "FAIL"].includes(state.settings.status!)
? state.authentication.status!
: state.settings.status!,
[state.settings.status, state.authentication.status]
);

if (status === "SUCCESS") {
return <Navigate to={to} />;
}

return <>{children}</>;
};
21 changes: 14 additions & 7 deletions src/libraries/hooks/useUserSettings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { useAppSelector } from "libraries/hooks/redux";

/**
* @returns list of all user settings
*/
export const useUserSettings = () =>
useAppSelector((state) => state.main.settings?.data || []);
import { createSelector } from "@reduxjs/toolkit";
import { useAppSelector } from "libraries/hooks/redux";
import { UserSettingDTO } from "../../generated";

const userSettingsSelector = createSelector(
(state) => state.main.settings?.data,
(settings) => settings || []
);

/**
* @returns list of all user settings
*/
export const useUserSettings = () =>
useAppSelector<any, UserSettingDTO[]>(userSettingsSelector);
9 changes: 7 additions & 2 deletions src/libraries/permissionUtils/usePermissions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { createSelector } from "@reduxjs/toolkit";
import { useAppSelector } from "libraries/hooks/redux";

const userPermissionSelector = createSelector(
(state) => state.main.authentication?.data?.permissions,
(permissions) => permissions || []
);

/**
* @returns list of all user permissions
*/
export const usePermissions = () =>
useAppSelector((state) => state.main.authentication?.data?.permissions || []);
export const usePermissions = () => useAppSelector(userPermissionSelector);
12 changes: 12 additions & 0 deletions src/mockServer/routes/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const settingsRoutes = (server) => {
server.namespace("/usersettings", () => {
server.get("/").intercept((req, res) => {
res
.status(200)
.json([{ id: 1, configName: "landing", configValue: "/" }]);
});
server.get("/dashboard").intercept((req, res) => {
res.status(200).json({}); // TODO: Add a response body from api. Missing api definition.
});
});
};
2 changes: 2 additions & 0 deletions src/mockServer/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { patientRoutes } from "./routes/patients";
import { permissionRoutes } from "./routes/permission";
import { pregnantTreatmentTypeRoutes } from "./routes/pregnantTreatmentType";
import { pricesRoutes } from "./routes/prices";
import { settingsRoutes } from "./routes/settings";
import { suppliersRoutes } from "./routes/suppliers";
import { therapyRoutes } from "./routes/therapies";
import { userGroupRoutes } from "./routes/userGroups";
Expand Down Expand Up @@ -80,6 +81,7 @@ export function makeServer() {
deliveryResultTypeRoutes(server);
permissionRoutes(server);
labExamRequestRoutes(server);
settingsRoutes(server);
});
return server;
}