Skip to content

Commit

Permalink
Add userPermissions query and replace isGuest logic
Browse files Browse the repository at this point in the history
  • Loading branch information
testower committed Dec 13, 2024
1 parent dd43862 commit 65f7ba9
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/actions/RolesActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createThunk } from ".";
import { getPolygons } from "./TiamatActions";
import { getPolygons, getUserPermissions } from "./TiamatActions";
import * as types from "./Types";

const getAdministrativeZoneIds = (roles) => {
Expand Down Expand Up @@ -52,3 +52,7 @@ export const updateAllowNewStopsEverywhere =
export const updateAuth = (auth) => (dispatch) => {
dispatch(createThunk(types.UPDATED_AUTH, auth));
};

export const fetchUserPermissions = () => (dispatch, getState) => {
dispatch(getUserPermissions());
};
11 changes: 11 additions & 0 deletions src/actions/TiamatActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
topopGraphicalPlacesReportQuery,
neighbourStopPlaceQuays,
getLocationPermissions,
getUserPermissionsQuery,
} from "../graphql/Tiamat/queries";
import mapToMutationVariables from "../modelUtils/mapToQueryVariables";

Expand Down Expand Up @@ -633,3 +634,13 @@ export const getLocationPermissionsForCoordinates = (longitude, latitude) => {
})(dispatch);
};
};

export const getUserPermissions = () => {
return async (dispatch, getState) => {
return handleQuery(getTiamatClient(), {
fetchPolicy: "no-cache",
query: getUserPermissionsQuery,
context: await getContext(getState().roles.auth),
})(dispatch);
};
};
7 changes: 3 additions & 4 deletions src/components/MainPage/SearchBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import {
Popover,
} from "@mui/material";
import TextField from "@mui/material/TextField";
import { isGuest } from "../../utils/roleUtils";

class SearchBox extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -367,11 +366,11 @@ class SearchBox extends React.Component {
topoiChips,
topographicalPlaces,
canEdit,
roleAssignments,
lookupCoordinatesOpen,
newStopIsMultiModal,
dataSource,
showFutureAndExpired,
isGuest,
} = this.props;
const { coordinatesDialogOpen, showMoreFilterOptions, loading } =
this.state;
Expand Down Expand Up @@ -686,7 +685,7 @@ class SearchBox extends React.Component {
formatMessage={formatMessage}
/>
) : null}
{!isGuest(roleAssignments) && (
{!isGuest && (
<div style={{ marginTop: 10 }}>
{isCreatingNewStop ? (
<NewStopPlace
Expand Down Expand Up @@ -787,10 +786,10 @@ const mapStateToProps = (state) => {
["allowanceInfoSearchResult", "canEdit"],
false,
),
roleAssignments: state.roles.auth.roleAssignments,
lookupCoordinatesOpen: state.user.lookupCoordinatesOpen,
newStopIsMultiModal: state.user.newStopIsMultiModal,
showFutureAndExpired: state.user.searchFilters.showFutureAndExpired,
isGuest: state.roles.isGuest,
};
};

Expand Down
10 changes: 8 additions & 2 deletions src/containers/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import { useDispatch } from "react-redux";
import Header from "../components/Header";
import { getTheme, getV0Theme } from "../config/themeConfig";
import SnackbarWrapper from "../components/SnackbarWrapper";
import { fetchPolygons, updateAuth } from "../actions/RolesActions";
import {
fetchPolygons,
fetchUserPermissions,
updateAuth,
} from "../actions/RolesActions";
import { useAppSelector } from "../store/hooks";
import configureLocalization from "../localization/localization";
import { UserActions } from "../actions";
Expand All @@ -49,9 +53,11 @@ const App = ({ children }) => {

useEffect(() => {
dispatch(updateAuth(auth));

if (auth.isAuthenticated) {
dispatch(fetchPolygons());
dispatch(fetchUserPermissions());
} else if (!auth.isLoading) {
dispatch(fetchUserPermissions());
}
}, [auth]);

Expand Down
9 changes: 9 additions & 0 deletions src/graphql/Tiamat/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,12 @@ export const getLocationPermissions = gql`
}
}
`;

export const getUserPermissionsQuery = gql`
query getUserPermissions {
userPermissions {
allowNewStopEverywhere
isGuest
}
}
`;
7 changes: 7 additions & 0 deletions src/reducers/rolesReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
export const initialState = {
auth: {},
fetchedPolygons: null,
isGuest: true,
allowNewStopEverywhere: false,
};

Expand All @@ -47,6 +48,12 @@ const rolesReducer = (state = initialState, action) => {
action.result.data.locationPermissions,
),
});
} else if (action.operationName === "getUserPermissions") {
return Object.assign({}, state, {
isGuest: action.result.data.userPermissions.isGuest,
allowNewStopEverywhere:
action.result.data.userPermissions.allowNewStopEverywhere,
});
} else {
return state;
}
Expand Down
15 changes: 13 additions & 2 deletions src/utils/roleUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,19 @@ const doesRoleGrantAccessToStop = (
return false;
};

export const isGuest = (roleAssignments) => {
return getEditStopRoles(roleAssignments).length === 0;
const getRolesFromTokenByType = (roleAssignments, type) => {
if (!roleAssignments) return [];

let roles = [];

roleAssignments.forEach((roleString) => {
let roleJSON = JSON.parse(roleString);
if (roleJSON.r === type) {
roles.push(roleJSON);
}
});

return roles;
};
export const getStopPlacesForSubmodes = (legalSubmodes) => {
let result = [];
Expand Down

0 comments on commit 65f7ba9

Please sign in to comment.