Repository navigation
Refactored react_modal.js.jsx into modular functional TypeScript comp… #1822
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
Open
danielrafailov
wants to merge
2
commits into
Courseography:master
Choose a base branch
from
danielrafailov:refactor-react-modal
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,209
−1,041
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { JSX, useRef } from "react" | ||
| import L from "leaflet" | ||
| import { FeatureGroup, MapContainer, Marker, Popup, TileLayer } from "react-leaflet" | ||
| import { convertTimeToString } from "./utils" | ||
| import { BuildingGroup, LectureTimeframe } from "./types" | ||
|
|
||
| export interface CampusMapProps { | ||
| lecturesByBuilding: BuildingGroup[] | ||
| selectedLecTimeframes: LectureTimeframe[] | ||
| } | ||
|
|
||
| /** | ||
| * Interactive Leaflet campus map displaying markers for lecture locations. | ||
| * | ||
| * Plots building markers based on scheduled lecture coordinates, highlights selected | ||
| * buildings with custom icons, displays popups listing class meeting times, and provides | ||
| * a "Zoom to Fit" control to dynamically adjust map bounds to encompass all active markers. | ||
| * | ||
| * @param lecturesByBuilding Array of buildings with their coordinates and scheduled classes. | ||
| * @param selectedLecTimeframes Currently selected lectures to highlight on the map. | ||
| */ | ||
| export function CampusMap({ | ||
| lecturesByBuilding, | ||
| selectedLecTimeframes, | ||
| }: CampusMapProps): JSX.Element { | ||
| const mapRef = useRef<L.Map | null>(null) | ||
| const groupRef = useRef<L.FeatureGroup | null>(null) | ||
|
|
||
| const zoomToFit = (): void => { | ||
| const map = mapRef.current | ||
| const mapGroup = groupRef.current | ||
|
|
||
| if (!map || !mapGroup) return | ||
|
|
||
| // markerCount is the number of markers on the map. | ||
| // Only adjust the map bounds if there are multiple markers because the zoom will | ||
| // be strange for 1 marker. Note that the map will be centered on the single marker | ||
| const markerCount = mapGroup.getLayers().length | ||
|
|
||
| if (markerCount > 1) { | ||
| map.fitBounds(mapGroup.getBounds(), { padding: [30, 30] }) | ||
| } | ||
| } | ||
|
|
||
| const blueMarker = new L.Icon({ | ||
| iconUrl: "/static/res/ico/blue-marker.png", | ||
| shadowUrl: "/static/res/ico/shadow.png", | ||
| iconAnchor: [5, 35], | ||
| popupAnchor: [13, -35], | ||
| iconSize: [34, 35], | ||
| shadowSize: [50, 85], | ||
| shadowAnchor: [2, 90], | ||
| }) | ||
|
|
||
| const redMarker = new L.Icon({ | ||
| iconUrl: "/static/res/ico/red-marker.png", | ||
| shadowUrl: "/static/res/ico/shadow.png", | ||
| iconAnchor: [5, 35], | ||
| popupAnchor: [13, -35], | ||
| iconSize: [34, 35], | ||
| shadowSize: [50, 85], | ||
| shadowAnchor: [2, 90], | ||
| }) | ||
|
|
||
| const locationMarkers = lecturesByBuilding.map(building => { | ||
| if (building.lat === undefined || building.lng === undefined) { | ||
| return null | ||
| } | ||
|
|
||
| const description = building.days | ||
| .sort((dayA, dayB) => { | ||
| return dayA.dayOrder - dayB.dayOrder | ||
| }) | ||
| .map(day => { | ||
| const dayTimes = day.timeframes | ||
| .sort((timeA, timeB) => { | ||
| return timeA.startTime - timeB.startTime | ||
| }) | ||
| .map(time => ( | ||
| <li key={time.courseCode + "-" + time.startTime + "-" + time.endTime}> | ||
| {convertTimeToString(time.startTime) + | ||
| "-" + | ||
| convertTimeToString(time.endTime) + | ||
| ", " + | ||
| time.room + | ||
| ": " + | ||
| time.courseCode} | ||
| </li> | ||
| )) | ||
|
|
||
| return ( | ||
| <div key={day.dayString}> | ||
| <p>{day.dayString}</p> | ||
| {dayTimes} | ||
| </div> | ||
| ) | ||
| }) | ||
|
|
||
| let colouredMarker | ||
| const buildingInd = selectedLecTimeframes.findIndex( | ||
| lec => lec.location && lec.location.buildingCode === building.buildingCode | ||
| ) | ||
|
|
||
| colouredMarker = buildingInd == -1 ? blueMarker : redMarker | ||
|
|
||
| return ( | ||
| <Marker | ||
| key={building.buildingName} | ||
| icon={colouredMarker} | ||
| position={[building.lat, building.lng]} | ||
| > | ||
| <Popup key={building.postalCode}> | ||
| <p> | ||
| <b> {building.buildingName + " | " + building.buildingCode} </b> | ||
| </p> | ||
| <p> | ||
| <em>{building.address}</em> | ||
| </p> | ||
| {description} | ||
| </Popup> | ||
| </Marker> | ||
| ) | ||
| }) | ||
|
|
||
| const mapBounds = L.latLngBounds([]) | ||
|
|
||
| // If there are selected lectures, center the map based on the locations of those lectures, otherwise, center the map | ||
| // on Bahen | ||
| let center: L.LatLngExpression = [43.6596426, -79.3976676] | ||
|
|
||
| if (lecturesByBuilding.length) { | ||
| lecturesByBuilding.forEach(building => { | ||
| if (building.lat !== undefined && building.lng !== undefined) { | ||
| mapBounds.extend(L.latLng(building.lat, building.lng)) | ||
| } | ||
| }) | ||
|
|
||
| center = mapBounds.getCenter() | ||
| } | ||
|
|
||
| return ( | ||
| <div id="campus-map" className="col-md-8 col-6"> | ||
| <MapContainer | ||
| center={center} | ||
| zoom={16} | ||
| maxZoom={40} | ||
| attributionControl={true} | ||
| zoomControl={true} | ||
| doubleClickZoom={true} | ||
| scrollWheelZoom={true} | ||
| dragging={true} | ||
| ref={mapRef} | ||
| > | ||
| <TileLayer url="http://{s}.tile.osm.org/{z}/{x}/{y}.png" /> | ||
| <FeatureGroup ref={groupRef}>{locationMarkers}</FeatureGroup> | ||
| </MapContainer> | ||
| <button id="zoom-button" onClick={zoomToFit}> | ||
| Zoom to Fit | ||
| </button> | ||
| </div> | ||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the
JSX, use a type-only import (if it's only being used as a type annotation). This applies broadly across all files; please also review all imports to see if there are other type-only imports.