Skip to content

Commit

Permalink
fixed visibility of vehicles of selected direction, typo in search th…
Browse files Browse the repository at this point in the history
…rough units
  • Loading branch information
hiba9201 committed Nov 5, 2023
1 parent 64c371c commit 99ac9fd
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 45 deletions.
8 changes: 7 additions & 1 deletion client/common/types/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { StrapiStop } from 'transport-common/types/strapi';

export interface State {
publicTransport: {
currentVehicle: Pick<Unit, 'num' | 'routeId' | 'routeDirection' | 'type'> | null;
currentVehicle:
| (Pick<Unit, 'num' | 'routeId' | 'routeDirection' | 'type'> & CurrentVehicleOptions)
| null;
currentStop: string | null;
currentRoute: Route & Pick<Unit, 'type' | 'routeDirection'> & CurrentRouteOptions;
stops: StrapiStop[];
Expand All @@ -14,10 +16,14 @@ export interface State {
};
}

export interface CurrentVehicleSettings {
shouldFilterByRouteDirection?: boolean;
}
export type CurrentVehiclePayload = State['publicTransport']['currentVehicle'];
export interface CurrentVehicleOptions {
shouldClear?: boolean;
shouldFlyTo?: boolean;
shouldFilterByRouteDirection?: boolean;
}
export type CurrentVehiclePayloadWithOptions = CurrentVehiclePayload & CurrentVehicleOptions;
export interface CurrentRouteOptions {
Expand Down
8 changes: 4 additions & 4 deletions client/components/Map/SearchBar/SearchBar.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export function searchThroughUnits(units: Unit[], searchText: string) {
return uniqBy(
units
.filter(
(troll) =>
troll.lastStation.toLowerCase().includes(searchText) ||
troll.firstStation.toLowerCase().includes(searchText) ||
troll.num.toLowerCase().includes(searchText),
(unit) =>
unit.lastStation.toLowerCase().includes(searchText) ||
unit.firstStation.toLowerCase().includes(searchText) ||
unit.num.toLowerCase().includes(searchText),
)
.sort((a, b) => a.firstStation.localeCompare(b.firstStation, 'ru', { numeric: true })),
uniqUnitsIteratee,
Expand Down
39 changes: 18 additions & 21 deletions client/components/Map/SearchBar/StopsResult/StopResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,28 @@ export function MapSearchBarStopResult({ type, stopId, title }: Stop) {
const allStops = useSelector((state: State) => state.publicTransport.stops);
const currentStop = useSelector((state: State) => state.publicTransport.currentStop);

const setSelectedStop = useCallback(
(stopId: string) => {
const stop = allStops.find((stopFullData) => stopFullData.attributes.stopId === stopId);
const setSelectedStop = useCallback(() => {
const stop = allStops.find((stopFullData) => stopFullData.attributes.stopId === stopId);

if (!stop) {
return;
}
if (!stop) {
return;
}

const { attributes: stopData } = stop;
const { attributes: stopData } = stop;

dispatch(
setCurrentStop({
currentStop: stopId,
}),
);
dispatch(
setCurrentStop({
currentStop: stopId,
}),
);

map.flyTo(stopData.coords, 15);
map.flyTo(stopData.coords, 15);

sidebarService.open({
component: <MapStopsSidebar type={stopData.type} name={stopData.title} />,
onClose: () => dispatch(setCurrentStop(null)),
});
},
[dispatch, allStops],
);
sidebarService.open({
component: <MapStopsSidebar type={stopData.type} name={stopData.title} />,
onClose: () => dispatch(setCurrentStop(null)),
});
}, [dispatch, allStops, stopId]);

const isSelected = useMemo(() => {
return currentStop === stopId;
Expand All @@ -56,7 +53,7 @@ export function MapSearchBarStopResult({ type, stopId, title }: Stop) {
className={cn(styles.MapSearchBarStopResult__wrapper, {
[styles.MapSearchBarStopResult__wrapper_selected]: isSelected,
})}
onClick={() => setSelectedStop(stopId)}
onClick={() => setSelectedStop()}
>
<img src={`/icons/${type === StopType.TrollBus ? 'bus-troll' : type}.svg`} alt="" />
<p className={cn(styles.MapSearchBarStopResult__text)}>{title}</p>
Expand Down
32 changes: 15 additions & 17 deletions client/components/Map/SearchBar/UnitResult/UnitResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@ export function MapSearchBarUnitResult(props: Unit) {
const dispatch = useDispatch<typeof store.dispatch>();
const currentVehicle = useSelector((state: State) => state.publicTransport.currentVehicle);

const setSelectedVehicle = useCallback(
(unit: Unit) => {
dispatch(
setCurrentVehicle({
num: unit.num,
routeDirection: unit.routeDirection,
type: unit.type,
routeId: unit.routeId,
shouldFlyTo: true,
}),
);

sidebarService.close();
},
[dispatch],
);
const setSelectedVehicle = useCallback(() => {
dispatch(
setCurrentVehicle({
num: props.num,
routeDirection: props.routeDirection,
type: props.type,
routeId: props.routeId,
shouldFlyTo: true,
shouldFilterByRouteDirection: true,
}),
);

sidebarService.close();
}, [dispatch, props.num, props.routeDirection, props.type, props.routeId]);

const isSelected = useMemo(() => {
return (
Expand All @@ -47,7 +45,7 @@ export function MapSearchBarUnitResult(props: Unit) {
className={cn(styles.MapSearchBarUnitResult__wrapper, {
[styles.MapSearchBarUnitResult__selected]: isSelected,
})}
onClick={() => setSelectedVehicle(props)}
onClick={() => setSelectedVehicle()}
>
<MapVehiclesRoute type={props.type} num={props.num} />
<p
Expand Down
9 changes: 8 additions & 1 deletion client/components/Map/Vehicles/MapVehicles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ export function MapVehicles({ type }: MapVehiclesProps) {
return true;
}

return currentVehicle.num === vehicle.num && currentVehicle.type === vehicle.type;
const isSameRoute =
currentVehicle.num === vehicle.num && currentVehicle.type === vehicle.type;

if (!currentVehicle.shouldFilterByRouteDirection) {
return isSameRoute;
}

return isSameRoute && currentVehicle.routeDirection === vehicle.routeDirection;
},
[currentStopVehicles, currentVehicle, currentStop, bounds],
);
Expand Down
6 changes: 5 additions & 1 deletion client/state/features/public-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const setCurrentVehicle = createAsyncThunk(
const {
shouldClear = true,
shouldFlyTo = false,
shouldFilterByRouteDirection = false,
...currentVehicle
} = currentVehiclePayload || {};

Expand All @@ -59,6 +60,7 @@ export const setCurrentVehicle = createAsyncThunk(
shouldFlyTo,
},
shouldClear,
shouldFilterByRouteDirection,
};
},
);
Expand Down Expand Up @@ -124,9 +126,11 @@ const publicTransportSlice = createSlice({
builder.addCase(
setCurrentVehicle.fulfilled,
(state, action: PayloadAction<SetCurrentVehiclePayload>) => {
const { currentVehicle, currentRoute, shouldClear } = action.payload;
const { currentVehicle, currentRoute, shouldClear, shouldFilterByRouteDirection } =
action.payload;

state.currentVehicle = currentVehicle;
state.currentVehicle.shouldFilterByRouteDirection = shouldFilterByRouteDirection;
state.currentRoute = currentRoute;

if (!currentRoute) {
Expand Down

1 comment on commit 99ac9fd

@ekbdev
Copy link

@ekbdev ekbdev commented on 99ac9fd Nov 5, 2023

Choose a reason for hiding this comment

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

Deploy preview for transport ready!

✅ Preview
https://transport-my66d9fx3-ekbdev.vercel.app
https://ekbdev-transport-feat-map-search.vercel.app

Built with commit 99ac9fd.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.