diff --git a/src/lib/components/OpeningTimes.svelte b/src/lib/components/OpeningTimes.svelte new file mode 100644 index 0000000..fbd3242 --- /dev/null +++ b/src/lib/components/OpeningTimes.svelte @@ -0,0 +1,40 @@ + + +
{#if place.name} @@ -235,4 +258,24 @@ flex-direction: column; padding-top: 1rem; } + + .opening-entry { + display: grid; + grid-template-columns: auto 1fr; + gap: 0.75rem; + padding: 0.5rem 1.4rem; + align-items: center; + } + .opening-entry .day { + font-weight: bold; + color: #ffffff; + white-space: nowrap; + } + .opening-entry .times { + font-family: monospace; + background-color: rgba(255, 255, 255, 0.1); + padding: 0.25rem 0.5rem; + border-radius: 0.25rem; + color: #eaffeb; + } diff --git a/src/routes/space/[id]/edit/+page.server.ts b/src/routes/space/[id]/edit/+page.server.ts index 5ddf15d..78baabf 100644 --- a/src/routes/space/[id]/edit/+page.server.ts +++ b/src/routes/space/[id]/edit/+page.server.ts @@ -8,6 +8,12 @@ type StudySpaceData = Omit< > & { id?: string; building_location?: google.maps.places.PlaceResult; + opening_times?: { + day_of_week: number; + opens_at: string; + closes_at: string; + is_24_7: boolean; + }[]; }; export const load: PageServerLoad = async ({ params, locals: { supabase } }) => { @@ -34,6 +40,13 @@ export const load: PageServerLoad = async ({ params, locals: { supabase } }) => const studySpaceData = space as StudySpaceData & Partial; const images = studySpaceData.study_space_images || []; + const { data: hours, error: hoursErr } = await supabase + .from("study_space_hours") + .select("day_of_week, opens_at, closes_at, is_24_7") + .eq("study_space_id", params.id) + .order("day_of_week", { ascending: true }); + if (hoursErr) error(500, "Failed to load opening times"); + studySpaceData.opening_times = hours; delete studySpaceData.created_at; delete studySpaceData.updated_at; diff --git a/src/routes/space/[id]/edit/+page.svelte b/src/routes/space/[id]/edit/+page.svelte index e04eee2..5302a2c 100644 --- a/src/routes/space/[id]/edit/+page.svelte +++ b/src/routes/space/[id]/edit/+page.svelte @@ -11,7 +11,8 @@ wifiTags, powerOutletTags, volumeTags, - gmapsLoader + gmapsLoader, + daysOfWeek } from "$lib"; import { onMount } from "svelte"; import type { Json } from "$lib/database.js"; @@ -20,10 +21,25 @@ const { supabase } = $derived(data); const { space, images } = $derived(data); - const studySpaceData = $derived({ + const studySpaceData = $state({ + opening_times: daysOfWeek.map((_, index) => ({ + day_of_week: index, + opens_at: "", + closes_at: "", + is_24_7: false + })), ...space }); + $effect(() => { + if (!space) return; + const { opening_times, ...rest } = space; + Object.assign(studySpaceData, rest); + if (opening_times) { + studySpaceData.opening_times = opening_times; + } + }); + let scrollPosition = $state(0); const existingImages = $derived( Promise.all( @@ -46,11 +62,13 @@ if (!spaceImgs || spaceImgs.length < 1) return alert("Please select an image file."); if (!studySpaceData.building_location) return alert("Please select a building location."); + const { opening_times, ...spacePayload } = studySpaceData; + const { data: studySpaceInsert, error: studySpaceError } = await supabase .from("study_spaces") .upsert( { - ...studySpaceData, + ...spacePayload, building_location: studySpaceData.building_location as Json }, { @@ -102,6 +120,23 @@ .select(); if (imageInsertError) return alert(`Error creating image: ${imageInsertError.message}`); + const { error: deleteErr } = await supabase + .from("study_space_hours") + .delete() + .eq("study_space_id", studySpaceInsert.id); + if (deleteErr) return alert(`Error clearing old hours: ${deleteErr.message}`); + + const { error: hoursErr } = await supabase.from("study_space_hours").insert( + opening_times.map((h) => ({ + study_space_id: studySpaceInsert.id, + day_of_week: h.day_of_week, + opens_at: h.opens_at, + closes_at: h.closes_at, + is_24_7: h.is_24_7 + })) + ); + if (hoursErr) return alert(`Error saving opening times: ${hoursErr.message}`); + alert("Thank you for your contribution!"); // Redirect to the new study space page await goto(`/space/${studySpaceInsert.id}`, { @@ -161,6 +196,20 @@ }); spaceImgs = dt.files; }); + + // --- Helper functions for opening times --- + function toggle247(index: number) { + const ot = studySpaceData.opening_times[index]; + if (ot.is_24_7) { + ot.opens_at = "00:00"; + ot.closes_at = "00:00"; + } + } + + function updateTimes(index: number) { + const ot = studySpaceData.opening_times[index]; + ot.is_24_7 = ot.opens_at === "00:00" && ot.closes_at === "00:00"; + } @@ -254,6 +303,39 @@