Skip to content

Commit 1c645d9

Browse files
added image carousel and made some fixes with UI colors
1 parent 8bab7ec commit 1c645d9

12 files changed

Lines changed: 2881 additions & 243 deletions

File tree

backend/package-lock.json

Lines changed: 465 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"homepage": "https://github.com/devsoc-unsw/Freerooms#readme",
2424
"dependencies": {
2525
"@graphql-typed-document-node/core": "^3.2.0",
26+
"axios": "^1.19.0",
27+
"cheerio": "^1.2.0",
2628
"cors": "^2.8.5",
2729
"date-fns": "^2.30.0",
2830
"date-fns-tz": "^2.0.0",
@@ -52,4 +54,4 @@
5254
"tsconfig-paths": "^4.2.0",
5355
"typescript": "^5.4.3"
5456
}
55-
}
57+
}

backend/src/dbInterface.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,22 @@ export const queryRoomUtilities = async (
194194
const variables = { roomId };
195195
return await doRequest<RoomUtilitiesRes>(query, variables);
196196
};
197+
198+
///////////////////////////////////////////////////////////////
199+
200+
type RoomIdsRes = {
201+
rooms: Array<{
202+
id: string
203+
}>
204+
}
205+
206+
export const queryRoomIds =
207+
async (): Promise<RoomIdsRes> => {
208+
209+
const query = `
210+
query rooms(order_by: {id: asc}) {
211+
id
212+
}
213+
`
214+
return await doRequest<RoomIdsRes>(query)
215+
}

backend/src/utils/getRoomPhoto.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import axios from "axios";
2+
import * as cheerio from "cheerio";
3+
import fs from "fs";
4+
import path from "path";
5+
import classrooms from "../classrooms.json";
6+
7+
const OUTPUT_PATH = path.join(__dirname, "../../../frontend/public/room-photos.json");
8+
9+
const CAMPUS_CODE = 0
10+
const BUILDING_CODE = 1
11+
12+
async function scrapeRoom(): Promise<string | null> {
13+
14+
const roomIds = classrooms.map((c) => c.id)
15+
const results: Record<string, string[]> = {}
16+
17+
for (const r of roomIds) {
18+
19+
console.log(`Trying for ${r}`)
20+
try {
21+
const s = r.split("-")
22+
23+
const building = (s[CAMPUS_CODE] + "-" + s[BUILDING_CODE]).toLowerCase()
24+
const room = r.toLowerCase()
25+
26+
const ROOM_URL = `https://www.learningenvironments.unsw.edu.au/physical-spaces/${building}/${room}`
27+
28+
const {data} = await axios.get(ROOM_URL);
29+
const $ = cheerio.load(data)
30+
31+
const images = $("div.field--name-field-media-image a").map((i, el) => $(el).attr("href")).get()
32+
console.log(images)
33+
results[r] = images
34+
} catch (error) {
35+
results[r] = []
36+
}
37+
38+
}
39+
40+
fs.writeFileSync(OUTPUT_PATH, JSON.stringify(results, null, 2))
41+
return null
42+
}
43+
44+
scrapeRoom()

backend/src/utils/room-photos.json

Lines changed: 1127 additions & 0 deletions
Large diffs are not rendered by default.

backend/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"outDir": "dist",
77
"strict": true,
88
"esModuleInterop": true,
9+
"resolveJsonModule": true,
910
"skipLibCheck": true,
1011
"forceConsistentCasingInFileNames": true,
1112
"paths": {

frontend/app/room/[room]/page.tsx

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"use client";
2-
2+
import { Swiper, SwiperSlide } from 'swiper/react';
3+
import { Autoplay, Navigation } from 'swiper/modules';
4+
import 'swiper/css';
5+
import 'swiper/css/navigation';
36
import translateRoomUsage from "@common/roomUsages";
47
import getSchoolDetails from "@common/schools";
58
import type { Booking, Room } from "@common/types";
@@ -69,7 +72,7 @@ export default function Page() {
6972
const [campus, grid] = room ? room.id.split("-") : ["", ""];
7073
const { building } = useBuilding(`${campus}-${grid}`);
7174
const { isFavourite, toggleFavourite } = useFavourites();
72-
75+
7376
return (
7477
<Container maxWidth="xl">
7578
<FeedbackButton />
@@ -92,13 +95,12 @@ export default function Page() {
9295
favourite={isFavourite(room.id)}
9396
onToggleFavourite={() => toggleFavourite(room.id)}
9497
/>
95-
<RoomImage
96-
src={
97-
roomParam in room_photos
98-
? `${(room_photos as Record<string, string>)[roomParam]}`
99-
: `/assets/building_photos/${campus}-${grid}.webp`
100-
}
101-
/>
98+
{ /* Here */ }
99+
<Carousel photos={
100+
roomParam in room_photos && room_photos[roomParam].length > 0
101+
? room_photos[roomParam]
102+
: [`/assets/building_photos/${campus}-${grid}.webp`]
103+
}/>
102104
<BookingCalendar events={adjustedBookings ?? []} roomID={room.id} />
103105
<RoomUtilityTags roomId={room?.id} />
104106
<RoomRating buildingID={building.id} roomID={room.id} />
@@ -306,6 +308,7 @@ const RoomPageHeader: React.FC<{
306308
);
307309
};
308310

311+
{ /* Replace this with Carousel */ }
309312
const RoomImage: React.FC<{ src: string }> = ({ src }) => {
310313
return (
311314
<Box
@@ -324,3 +327,43 @@ const RoomImage: React.FC<{ src: string }> = ({ src }) => {
324327
</Box>
325328
);
326329
};
330+
331+
const Carousel: React.FC<{ photos: string[] }> = ({ photos }) => {
332+
const hasMultiplePhotos = photos.length > 1;
333+
return (
334+
<Box
335+
sx={{
336+
maxWidth: "100%",
337+
height: 500,
338+
position: "relative",
339+
}}>
340+
<Swiper
341+
modules={[Autoplay, Navigation]}
342+
navigation={hasMultiplePhotos}
343+
autoplay={hasMultiplePhotos ? { delay: 5000, disableOnInteraction: false } : false}
344+
speed={600}
345+
loop={hasMultiplePhotos}
346+
spaceBetween={50}
347+
slidesPerView={1}
348+
onSlideChange={() => console.log('slide change')}
349+
onSwiper={(swiper) => console.log(swiper)}
350+
style={{
351+
height: "100%",
352+
"--swiper-navigation-color": "#ffffff"
353+
}}
354+
>
355+
{photos.map((p) => (
356+
<SwiperSlide style={{ position: "relative" }}>
357+
<Image
358+
src={p}
359+
alt="Room Image"
360+
fill
361+
style={{ objectFit: "cover", borderRadius: 10}}
362+
/>
363+
</SwiperSlide>
364+
))}
365+
</Swiper>
366+
</Box>
367+
368+
);
369+
}

frontend/components/RoomAvailability.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const RoomBoxSubheading = styled(Typography)<TypographyProps>(({ theme }) => ({
1515
}));
1616

1717
const roomStatusColor = {
18-
free: "#2AA300",
19-
busy: "#D30000",
18+
free: "#33FF33",
19+
busy: "#FF000C",
2020
soon: "#ffa600",
2121
};
2222

frontend/package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"react-flip-move": "^3.0.5",
4040
"react-redux": "^9.3.0",
4141
"sharp": "^0.35.0",
42+
"swiper": "^14.0.7",
4243
"swr": "^2.4.1",
4344
"ts-node": "^10.9.2",
4445
"usehooks-ts": "^3.0.0"

0 commit comments

Comments
 (0)