Skip to content

Commit b51fb78

Browse files
authored
DBC22-924: moved all camera and highway ordering to FE (#220)
1 parent b9ffb08 commit b51fb78

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

src/backend/apps/webcam/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
class WebcamAPI(CachedListModelMixin):
9-
queryset = Webcam.objects.all().order_by("highway", "highway_cam_order", "id")
9+
queryset = Webcam.objects.all()
1010
serializer_class = WebcamSerializer
1111
cache_key = CacheKey.WEBCAM_LIST
1212
cache_timeout = CacheTimeout.WEBCAM_LIST

src/frontend/src/Components/cameras/CameraList.js

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import Advisories from '../advisories/Advisories';
1414
// Styling
1515
import './CameraList.scss';
1616

17+
const collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
18+
1719
export default function CameraList() {
1820
// UseRef hooks
1921
const isInitialMount = useRef(true);
@@ -27,15 +29,17 @@ export default function CameraList() {
2729
const webcamResults = await getWebcams();
2830
const cameras = groupCameras(webcamResults);
2931

32+
// Sort cameras by highway number
33+
cameras.sort(function(a, b) {
34+
return collator.compare(a.highway, b.highway)
35+
});
36+
3037
setWebcams(cameras);
3138

3239
isInitialMount.current = false;
3340
};
3441

3542
const getDisplayedWebcams = () => {
36-
webcams.sort((a, b) => {
37-
return parseInt(a.highway, 10) - parseInt(b.highway, 10);
38-
});
3943
const res = webcams.slice(0, displayedWebcams.length + 7);
4044
setDisplayedWebcams(res);
4145
};
@@ -52,7 +56,7 @@ export default function CameraList() {
5256
useEffect(() => {
5357
if (!isInitialMount.current) { // Only run on updates
5458
getDisplayedWebcams();
55-
}
59+
}
5660
}, [webcams]);
5761

5862
useEffect(() => {
@@ -79,35 +83,48 @@ export default function CameraList() {
7983
};
8084

8185
const renderWebcams = () => {
82-
return Object.entries(mapDisplayedWebcams()).map(([highway, cameras]) => (
83-
<div className="highway-group" key={highway}>
84-
<Container>
85-
<div className="highway-title">
86-
<div className="highway-shield-box">
87-
{highwayShield(highway)}
86+
const groupedCams = mapDisplayedWebcams();
87+
const highwayKeys = Object.keys(groupedCams);
88+
89+
// Render camera groups by highway number
90+
highwayKeys.sort(collator.compare);
91+
92+
return highwayKeys.map((highway) => {
93+
const cameras = groupedCams[highway];
94+
95+
// Render cameras by highway_cam_order
96+
cameras.sort((a, b) => a.highway_cam_order - b.highway_cam_order);
97+
98+
return (
99+
<div className="highway-group" key={highway}>
100+
<Container>
101+
<div className="highway-title">
102+
<div className="highway-shield-box">
103+
{highwayShield(highway)}
104+
</div>
105+
106+
<div className="highway-name">
107+
<p className="highway-name__number">Highway {highway}</p>
108+
{highway === '1' &&
109+
<h2 className="highway-name__alias highway-name__alias--green">Trans Canada</h2>
110+
}
111+
{highway !== '1' &&
112+
<h2 className="highway-name__alias">Highway {highway}</h2>
113+
}
114+
</div>
88115
</div>
89116

90-
<div className="highway-name">
91-
<p className="highway-name__number">Highway {highway}</p>
92-
{highway === '1' &&
93-
<h2 className="highway-name__alias highway-name__alias--green">Trans Canada</h2>
94-
}
95-
{highway !== '1' &&
96-
<h2 className="highway-name__alias">Highway {highway}</h2>
97-
}
117+
<div className="webcam-group">
118+
{cameras.map((camera, id) => (
119+
<WebcamCard cameraData={camera} className="webcam" key={id}>
120+
</WebcamCard>
121+
))}
98122
</div>
99-
</div>
100-
101-
<div className="webcam-group">
102-
{cameras.map((camera, id) => (
103-
<WebcamCard cameraData={camera} className="webcam" key={id}>
104-
</WebcamCard>
105-
))}
106-
</div>
107-
</Container>
108-
</div>
109-
));
110-
};
123+
</Container>
124+
</div>
125+
);
126+
});
127+
}
111128

112129
return (
113130
<div className="camera-list">

0 commit comments

Comments
 (0)