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

PR untuk #27 #33

Open
wants to merge 3 commits into
base: main
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
16 changes: 16 additions & 0 deletions src/data/transport-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ export type TransportData = {
geoData: Accessor<GeoData>;
filter: Accessor<Filter>;

selectedEntry: Accessor<SelectedEntry | null>;
setSelectedEntry: (entry: SelectedEntry) => void;

rightPopover: Accessor<PopOverState | null>;
closeRightPopover: () => void;
setSelectedBusStop: (id: string) => void;
Expand Down Expand Up @@ -169,6 +172,15 @@ export type PopOverBusStop = {
};
export type PopOverState = PopOverBusStop | null;

export type SelectedEntry = {
category: "Bus Stop" | "Bus Route";
id: string;
name: string;
latitude?: number;
longitude?: number;
color?: string;
} | null;

export const TransportDataContext = createContext<TransportData>();

export const useTransportData = () => useContext(TransportDataContext)!;
Expand All @@ -190,6 +202,8 @@ export const TransportDataProvider: ParentComponent = (props) => {
selectedRouteIds: new Set(),
selectedRouteTypes: new Set(),
});
const [selectedEntry, setSelectedEntry] =
createSignal<SelectedEntry | null>(null);

onMount(async () => {
// Load route data
Expand Down Expand Up @@ -314,6 +328,8 @@ export const TransportDataProvider: ParentComponent = (props) => {
geoData,
filter,
rightPopover,
selectedEntry,
setSelectedEntry,
setSelectedBusStop: (id: string) => {
const data = tjData().data;
if (data === undefined) return;
Expand Down
130 changes: 120 additions & 10 deletions src/ui/home/map/map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,56 @@ import {
NavigationControl,
} from "maplibre-gl";
import { jakartaCoordinate } from "../../../constants";
import { useTransportData } from "../../../data/transport-data";
import { BusStop, useTransportData } from "../../../data/transport-data";
import { useSidebarState } from "../../../data/sidebar-state";

export const MapCanvas: Component = () => {
const { geoData, tjDataSource, setSelectedBusStop } = useTransportData();
const { geoData, tjDataSource, setSelectedBusStop, selectedEntry } =
useTransportData();
const { setIsExpanded } = useSidebarState();
const [libreMap, setLibreMap] = createSignal<Map | null>(null);

const updateBusStopSources = (map: Map, selectedStopId: string | null) => {
const busStopsSource = map.getSource(
"opentije_bus_stops",
) as GeoJSONSource;
const selectedStopSource = map.getSource(
"selected_bus_stop",
) as GeoJSONSource;

if (selectedStopId) {
const selectedStop = geoData().busStops.find(
(stop) => stop.id === selectedStopId,
);

if (selectedStop) {
selectedStopSource.setData({
type: "FeatureCollection",
features: [selectedStop.geoJson],
});

const filteredSameStop = geoData()
.busStops.filter((stop) => stop.id !== selectedStop.id)
.map((stop) => stop.geoJson);

busStopsSource.setData({
type: "FeatureCollection",
features: filteredSameStop,
});
}
} else {
selectedStopSource.setData({
type: "FeatureCollection",
features: [],
});

busStopsSource.setData({
type: "FeatureCollection",
features: geoData().busStops.map((stop) => stop.geoJson),
});
}
};

createEffect(() => {
const map = libreMap();
if (map === null) return;
Expand All @@ -35,6 +77,35 @@ export const MapCanvas: Component = () => {
route.routeToGeoJson(),
),
});

const entry = selectedEntry();
Copy link
Member

Choose a reason for hiding this comment

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

Bagian kode ini jalan terus ketika query search berubah. Step to reproduce

  1. Search halte Mega Kuningan
  2. Camera fly to bus stop Mega Kuningan
  3. Gerakin kamera ke GBK
  4. Ketik "Taman"
  5. Camera fly to bus stop Mega Kuningan lagi

if (entry && entry.category === "Bus Stop") {
updateBusStopSources(map, entry.id);

const selectedStop = geoData().busStops.find(
(stop) => stop.id === entry.id,
);

if (selectedStop) {
map.flyTo({
center: [selectedStop.longitude, selectedStop.latitude],
zoom: 16,
speed: 0.5,
curve: 2.5,
});
}
} else {
updateBusStopSources(map, null);
map.flyTo({
center: [
jakartaCoordinate.longitude,
jakartaCoordinate.latitude,
],
zoom: 12,
speed: 0.5,
curve: 2.5,
});
}
});

onMount(() => {
Expand Down Expand Up @@ -68,7 +139,7 @@ export const MapCanvas: Component = () => {
id: "opentije_bus_stops-label",
type: "symbol",
source: "opentije_bus_stops",
minzoom: 13,
minzoom: 15,
layout: {
"text-field": ["get", "name"],
"text-font": ["Noto Sans Regular"],
Expand All @@ -81,6 +152,7 @@ export const MapCanvas: Component = () => {
id: "opentije_bus_stops",
type: "circle",
source: "opentije_bus_stops",
minzoom: 12,
paint: {
"circle-color": ["get", "color"],
},
Expand All @@ -103,20 +175,58 @@ export const MapCanvas: Component = () => {
},
});

map.addSource("selected_bus_stop", {
type: "geojson",
data: {
type: "FeatureCollection",
features: [],
},
});

map.addLayer({
id: "selected_bus_stop",
type: "circle",
source: "selected_bus_stop",
paint: {
"circle-color": "#FF0000",
"circle-radius": 10,
},
});

map.addLayer({
id: "selected_bus_stop-label",
type: "symbol",
source: "selected_bus_stop",
layout: {
"text-field": ["get", "name"],
"text-font": ["Noto Sans Bold"],
"text-size": 20,
"text-variable-anchor": ["left", "right"],
"text-radial-offset": 0.8,
"text-justify": "auto",
},
paint: {
"text-color": "#000000",
"text-opacity": 1,
},
});

map.on("click", "opentije_bus_stops", (target) => {
const busStopId = target.features![0].properties.id;

setSelectedBusStop(busStopId);
updateBusStopSources(map, busStopId);
});

setLibreMap(map);
});
});

return <div
id="map"
class="map__canvas"
onClick={() => setIsExpanded(false)}
>

</div>;
return (
<div
id="map"
class="map__canvas"
onClick={() => setIsExpanded(false)}
></div>
);
};
26 changes: 22 additions & 4 deletions src/ui/home/sidebar/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,27 @@ type RouteProps = {
};

export const Route = ({ route }: RouteProps) => {
const { filter, setSelectedRouteId } = useTransportData();
const { filter, setSelectedRouteId, setSelectedEntry } = useTransportData();

const handleCheckboxChange = (e: Event) => {
const isChecked = (e.target as HTMLInputElement).checked;

setSelectedRouteId(route.id, isChecked);

if (isChecked) {
setSelectedEntry({
category: "Bus Route",
id: route.id,
name: route.fullName,
color: route.color,
});
} else {
const selectedRoutes = filter().selectedRouteIds;
if (selectedRoutes.size === 0) {
setSelectedEntry(null);
}
}
};

return (
<li class="routes__route">
Expand All @@ -18,9 +38,7 @@ export const Route = ({ route }: RouteProps) => {
<input
type="checkbox"
checked={filter().selectedRouteIds.has(route.id)}
onChange={(e) => {
setSelectedRouteId(route.id, e.target.checked);
}}
onChange={handleCheckboxChange}
/>
</label>
</li>
Expand Down
34 changes: 33 additions & 1 deletion src/ui/home/sidebar/sidebar.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
display: none;

&:checked + p {
background: #72BF78;
background: #72bf78;
color: white;
}
}
Expand Down Expand Up @@ -131,3 +131,35 @@
background: white;
}
}

.autocompleteList {
position: absolute;
background: white;
border: 1px solid #ccc;
width: 95%;
z-index: 1000;
list-style: none;
margin: 0;
padding: 0;
max-height: 500px;
overflow-y: auto;
filter: drop-shadow(0 0 1rem #00000040);
}

.autocompleteItem {
padding: 12px 20px;
cursor: pointer;
}

.autocompleteItem:hover {
background-color: #f0f0f0;
}

.autocompleteItem div {
display: flex;
gap: 12px;
}

.autocompleteItem div span:nth-child(1) {
color: grey;
}
Loading