Skip to content

Commit

Permalink
Move disabledForSearch calculation inside StopPlaceMarker component
Browse files Browse the repository at this point in the history
  • Loading branch information
testower committed Dec 18, 2024
1 parent a4bf61e commit 33846cc
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 8 deletions.
5 changes: 0 additions & 5 deletions src/components/Map/MarkerList.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ class MarkerList extends React.Component {
missingCoordinatesMap,
handleSetCompassBearing,
disabled,
disabledForSearch,
intl,
showExpiredStops,
isEditingStop,
Expand Down Expand Up @@ -229,7 +228,6 @@ class MarkerList extends React.Component {
isMultimodalChild={false}
isGroupMember={true}
disabled={disabled}
disabledForSearch={disabledForSearch}
handleDragEnd={() => {}}
active={true}
stopType={member.stopPlaceType}
Expand Down Expand Up @@ -288,7 +286,6 @@ class MarkerList extends React.Component {
isMultimodal={false}
isMultimodalChild={true}
disabled={disabled}
disabledForSearch={disabledForSearch}
handleDragEnd={handleDragEnd}
active={false}
stopType={child.stopPlaceType}
Expand Down Expand Up @@ -379,7 +376,6 @@ class MarkerList extends React.Component {
hasExpired={marker.hasExpired}
isGroupMember={marker.isMemberOfGroup}
handleCreateGroup={this.handleCreateGroup.bind(this)}
disabledForSearch={disabledForSearch}
allowConnectToAdjacentStop={false}
/>,
);
Expand Down Expand Up @@ -628,7 +624,6 @@ const mapStateToProps = (state) => ({
showExpiredStops: state.stopPlace.showExpiredStops,
disabled: !getStopPermissions(state.stopPlace.current).canEdit,
stopPlace: state.stopPlace.current,
disabledForSearch: !getStopPermissions(state.stopPlace.current).canEdit,
newStopIsMultiModal: state.user.newStopIsMultiModal,
currentStopIsMultiModal: getIn(
state.stopPlace,
Expand Down
13 changes: 12 additions & 1 deletion src/components/Map/StopPlaceMarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import PropTypes from "prop-types";
import React from "react";
import ReactDOM from "react-dom/server";
import { Marker, Popup } from "react-leaflet";
import { connect } from "react-redux";
import {
getStopPermissions,
isStopFromSearch,
} from "../../utils/permissionsUtils";
import PopupButton from "../Map/PopupButton";
import CustomMarkerIcon from "./CustomMarkerIcon";
import { shallowCompareStopPlaceMarker as shallowCompare } from "./shallowCompare/";
Expand Down Expand Up @@ -232,4 +237,10 @@ class StopPlaceMarker extends React.Component {
}
}

export default StopPlaceMarker;
const mapStateToProps = (state, ownProps) => ({
disabledForSearch:
isStopFromSearch(state, ownProps.id) &&
!getStopPermissions(state.stopPlace.current).canEdit,
});

export default connect(mapStateToProps)(StopPlaceMarker);
4 changes: 2 additions & 2 deletions src/containers/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Helmet } from "react-helmet";
import { IntlProvider } from "react-intl";
import { useDispatch } from "react-redux";
import { UserActions } from "../actions";
import { updateAuth } from "../actions/UserActions";
import { fetchUserPermissions, updateAuth } from "../actions/UserActions";
import { useAuth } from "../auth/auth";
import Header from "../components/Header";
import SnackbarWrapper from "../components/SnackbarWrapper";
Expand All @@ -46,7 +46,7 @@ const App = ({ children }) => {
useEffect(() => {
dispatch(updateAuth(auth));
if (!auth.isLoading) {
dispatch(UserActions.fetchUserPermissions());
dispatch(fetchUserPermissions());
}
}, [auth]);

Expand Down
9 changes: 9 additions & 0 deletions src/utils/permissionsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,12 @@ export const getStopPermissions = (stopPlace) => {
}
return buildAllowanceInfoForStopPlace(stopPlace);
};

export const isStopFromSearch = (state, stopId) => {
if (!stopId) return false;

return (
state.stopPlace.searchResults?.some((result) => result.id === stopId) ||
state.stopPlace.activeSearchResult?.id === stopId
);
};
76 changes: 76 additions & 0 deletions src/utils/permissionsUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getLegalStopPlaceTypes,
getLegalSubmodesForStopPlace,
getStopPermissions,
isStopFromSearch,
} from "./permissionsUtils";

describe("getLegalStopPlaceTypes", () => {
Expand Down Expand Up @@ -319,3 +320,78 @@ describe("getStopPermissions", () => {
});
});
});

describe("isStopFromSearch", () => {
const mockStopId = "NSR:StopPlace:123";
const baseState = {
stopPlace: {
searchResults: [],
activeSearchResult: null,
},
};

it("returns false when stopId is null", () => {
expect(isStopFromSearch(baseState, null)).toBe(false);
});

it("returns false when stopId is undefined", () => {
expect(isStopFromSearch(baseState, undefined)).toBe(false);
});

it("returns false when stop is not in search results and not active result", () => {
expect(isStopFromSearch(baseState, mockStopId)).toBe(false);
});

it("returns true when stop is the active search result", () => {
const state = {
...baseState,
stopPlace: {
...baseState.stopPlace,
activeSearchResult: { id: mockStopId },
},
};
expect(isStopFromSearch(state, mockStopId)).toBe(true);
});

it("returns true when stop is in search results", () => {
const state = {
...baseState,
stopPlace: {
...baseState.stopPlace,
searchResults: [{ id: mockStopId }],
},
};
expect(isStopFromSearch(state, mockStopId)).toBe(true);
});

it("returns true when stop is both in search results and active result", () => {
const state = {
...baseState,
stopPlace: {
...baseState.stopPlace,
searchResults: [{ id: mockStopId }],
activeSearchResult: { id: mockStopId },
},
};
expect(isStopFromSearch(state, mockStopId)).toBe(true);
});

it("handles undefined searchResults gracefully", () => {
const state = {
stopPlace: {
activeSearchResult: null,
},
};
expect(isStopFromSearch(state, mockStopId)).toBe(false);
});

it("returns false for a different stop id", () => {
const state = {
stopPlace: {
searchResults: [{ id: mockStopId }],
activeSearchResult: { id: mockStopId },
},
};
expect(isStopFromSearch(state, "NSR:StopPlace:456")).toBe(false);
});
});

0 comments on commit 33846cc

Please sign in to comment.