Spaces:
Runtime error
Runtime error
update page state on room change
Browse files
frontend/src/lib/Buttons/RoomsSelector.svelte
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
<script lang="ts">
|
|
|
|
|
2 |
import Room from '$lib/Icons/Room.svelte';
|
3 |
import Pin from '$lib/Icons/Pin.svelte';
|
4 |
import People from '$lib/Icons/People.svelte';
|
@@ -34,6 +36,12 @@
|
|
34 |
async function refreshRooms() {
|
35 |
rooms = await fetch(PUBLIC_API_BASE + '/rooms').then((res) => res.json());
|
36 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
</script>
|
38 |
|
39 |
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
@@ -59,10 +67,7 @@
|
|
59 |
<!-- svelte-ignore a11y-invalid-attribute -->
|
60 |
<a
|
61 |
href="#"
|
62 |
-
on:click|preventDefault={() =>
|
63 |
-
$selectedRoomID = room.id;
|
64 |
-
collapsed = true;
|
65 |
-
}}
|
66 |
class="grid-row gap-2 hover:bg-gray-300
|
67 |
{room.id === $selectedRoomID ? 'text-green-600' : ''}"
|
68 |
>
|
|
|
1 |
<script lang="ts">
|
2 |
+
import { page } from '$app/stores';
|
3 |
+
|
4 |
import Room from '$lib/Icons/Room.svelte';
|
5 |
import Pin from '$lib/Icons/Pin.svelte';
|
6 |
import People from '$lib/Icons/People.svelte';
|
|
|
36 |
async function refreshRooms() {
|
37 |
rooms = await fetch(PUBLIC_API_BASE + '/rooms').then((res) => res.json());
|
38 |
}
|
39 |
+
function changeRoom(room: RoomResponse) {
|
40 |
+
$selectedRoomID = room.id;
|
41 |
+
collapsed = true;
|
42 |
+
$page.url.searchParams.set('roomid', room.room_id);
|
43 |
+
window.location.search = `?${$page.url.searchParams.toString()}`;
|
44 |
+
}
|
45 |
</script>
|
46 |
|
47 |
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
|
|
67 |
<!-- svelte-ignore a11y-invalid-attribute -->
|
68 |
<a
|
69 |
href="#"
|
70 |
+
on:click|preventDefault={() => changeRoom(room)}
|
|
|
|
|
|
|
71 |
class="grid-row gap-2 hover:bg-gray-300
|
72 |
{room.id === $selectedRoomID ? 'text-green-600' : ''}"
|
73 |
>
|
frontend/src/routes/+page.ts
CHANGED
@@ -2,13 +2,23 @@ import { PUBLIC_API_BASE } from '$env/static/public';
|
|
2 |
import type { RoomResponse } from '$lib/types';
|
3 |
import { selectedRoomID } from '$lib/store';
|
4 |
import { MAX_CAPACITY } from '$lib/constants';
|
5 |
-
|
6 |
export const prerender = true
|
7 |
export const ssr = false
|
8 |
-
|
|
|
|
|
9 |
const res = await fetch(PUBLIC_API_BASE + '/rooms');
|
10 |
const rooms: RoomResponse[] = await res.json();
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
return { rooms };
|
14 |
}
|
|
|
2 |
import type { RoomResponse } from '$lib/types';
|
3 |
import { selectedRoomID } from '$lib/store';
|
4 |
import { MAX_CAPACITY } from '$lib/constants';
|
5 |
+
import type { PageLoad } from './$types';
|
6 |
export const prerender = true
|
7 |
export const ssr = false
|
8 |
+
|
9 |
+
export const load: PageLoad = async ({ url }) => {
|
10 |
+
const roomidParam = url.searchParams.get('roomid');
|
11 |
const res = await fetch(PUBLIC_API_BASE + '/rooms');
|
12 |
const rooms: RoomResponse[] = await res.json();
|
13 |
+
|
14 |
+
if (roomidParam) {
|
15 |
+
const room = rooms.find(room => room.room_id === roomidParam);
|
16 |
+
if (room) {
|
17 |
+
selectedRoomID.set(room.id);
|
18 |
+
}
|
19 |
+
} else {
|
20 |
+
const room = rooms.find(room => room.users_count < MAX_CAPACITY) || null;
|
21 |
+
selectedRoomID.set(room ? room.id : null);
|
22 |
+
}
|
23 |
return { rooms };
|
24 |
}
|