Skip to content

Commit

Permalink
[Fix #305] Fixed not fetching data when using oidc auth
Browse files Browse the repository at this point in the history
  • Loading branch information
LaChope committed May 18, 2024
1 parent 09517db commit b1a09fa
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/contexts/AppBarContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { System } from "@models/systemModel";
import * as systemService from "@services/systemService";
import { useSnackbar } from "@hooks/useSnackbar";
import { axiosSource } from "@services/utils/axiosUtils";
import { useLoggedUser } from "@hooks/useLoggedUser";
import { useUserAuth } from "@hooks/useUserAuth";

interface AppBarTitleContextType {
appBarTitle: string;
Expand Down Expand Up @@ -43,7 +43,7 @@ export const AppBarProvider = ({ children }: AppBarTitleProviderProps) => {
const location = useLocation();
const { t } = useTranslation();
const [showSnackbar] = useSnackbar();
const [loggedUser] = useLoggedUser();
const loggedUser = useUserAuth();
const errorMessage = t("common.defaultErrorMsg");

useEffect(() => {
Expand All @@ -65,10 +65,10 @@ export const AppBarProvider = ({ children }: AppBarTitleProviderProps) => {
.catch((reason) => showSnackbar(reason, errorMessage));
};

if (loggedUser.authenticated) fetchSystems();
if (loggedUser) fetchSystems();

return () => axiosSource.cancel("SystemsProvider - unmounting");
}, []);
}, [useUserAuth]);

const getTitleFromPathname = (pathname: string): string => {
const parts = pathname.split("/");
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useFailureModesTables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as failureModesTableService from "@services/failureModesTableService";
import { SnackbarType, useSnackbar } from "./useSnackbar";
import { filter, findIndex } from "lodash";
import { FailureModesTable, UpdateFailureModesTable } from "@models/failureModesTableModel";
import { useLoggedUser } from "./useLoggedUser";
import { useUserAuth } from "@hooks/useUserAuth";

type failureModesTableContextType = [
FailureModesTable[],
Expand All @@ -26,7 +26,7 @@ export const useFailureModesTables = () => {
export const FailureModesTablesProvider = ({ children }: ChildrenProps) => {
const [_tables, _setTables] = useState<FailureModesTable[]>([]);
const [showSnackbar] = useSnackbar();
const [loggedUser] = useLoggedUser();
const loggedUser = useUserAuth();

useEffect(() => {
const fetchTables = async () => {
Expand All @@ -36,7 +36,7 @@ export const FailureModesTablesProvider = ({ children }: ChildrenProps) => {
.catch((reason) => showSnackbar(reason, SnackbarType.ERROR));
};

if (loggedUser.authenticated) fetchTables();
if (loggedUser) fetchTables();
return () => axiosSource.cancel("FailureModesTablesProvider - unmounting");
}, []);

Expand Down
7 changes: 3 additions & 4 deletions src/hooks/useFaultTrees.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { axiosSource } from "@services/utils/axiosUtils";
import { ChildrenProps } from "@utils/hookUtils";
import { SnackbarType, useSnackbar } from "@hooks/useSnackbar";
import { filter, findIndex } from "lodash";
import { useLoggedUser } from "./useLoggedUser";
import { useUserAuth } from "@hooks/useUserAuth";

type faultTreeContextType = [
FaultTree[],
Expand All @@ -26,7 +26,7 @@ export const useFaultTrees = () => {
export const FaultTreesProvider = ({ children }: ChildrenProps) => {
const [_faultTrees, _setFaultTrees] = useState<FaultTree[]>([]);
const [showSnackbar] = useSnackbar();
const [loggedUser] = useLoggedUser();
const loggedUser = useUserAuth();

useEffect(() => {
const fetchFaultTrees = async () => {
Expand All @@ -36,8 +36,7 @@ export const FaultTreesProvider = ({ children }: ChildrenProps) => {
.catch((reason) => showSnackbar(reason, SnackbarType.ERROR));
};

if (loggedUser.authenticated) fetchFaultTrees();

if (loggedUser) fetchFaultTrees();
return () => axiosSource.cancel("FaultTreesProvider - unmounting");
}, []);

Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useSystems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { axiosSource } from "@services/utils/axiosUtils";
import { ChildrenProps } from "@utils/hookUtils";
import { SnackbarType, useSnackbar } from "@hooks/useSnackbar";
import { filter, findIndex } from "lodash";
import { useLoggedUser } from "./useLoggedUser";
import { useUserAuth } from "@hooks/useUserAuth";

type systemContextType = [
System[],
Expand All @@ -26,7 +26,7 @@ export const useSystems = () => {
export const SystemsProvider = ({ children }: ChildrenProps) => {
const [_systems, _setSystems] = useState<System[]>([]);
const [showSnackbar] = useSnackbar();
const [loggedUser] = useLoggedUser();
const loggedUser = useUserAuth();

useEffect(() => {
const fetchSystems = async () => {
Expand All @@ -36,7 +36,7 @@ export const SystemsProvider = ({ children }: ChildrenProps) => {
.catch((reason) => showSnackbar(reason, SnackbarType.ERROR));
};

if (loggedUser.authenticated) fetchSystems();
if (loggedUser) fetchSystems();

return () => axiosSource.cancel("SystemsProvider - unmounting");
}, []);
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/useUserAuth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useContext } from "react";
import { AuthContext } from "@oidc/OidcAuthWrapper";
import { useLoggedUser } from "@hooks/useLoggedUser";
import { isUsingOidcAuth } from "@utils/OidcUtils";

export const useUserAuth = () => {
const oidcAuthContext = useContext(AuthContext);
const [loggedUser] = useLoggedUser();

if (isUsingOidcAuth()) {
return oidcAuthContext.user !== null;
}
return loggedUser.authenticated;
};

0 comments on commit b1a09fa

Please sign in to comment.