feat: initial uploads and single study space view
Co-Authored-By: Alex Ling <al443@ic.ac.uk>
This commit is contained in:
@@ -3,57 +3,19 @@
|
||||
import defaultImg from "$lib/assets/study_space.png";
|
||||
import { invalidate } from "$app/navigation";
|
||||
import type { Table } from "$lib";
|
||||
import crossUrl from "$lib/assets/cross.svg";
|
||||
import Navbar from "$lib/components/Navbar.svelte";
|
||||
|
||||
const { data } = $props();
|
||||
const { studySpaces, supabase } = $derived(data);
|
||||
|
||||
const blankStudySpace = {
|
||||
description: "",
|
||||
building_address: "",
|
||||
location: ""
|
||||
};
|
||||
|
||||
let studySpaceData = $state<Omit<Table<"study_spaces">, "id" | "created_at" | "updated_at">>({
|
||||
...blankStudySpace
|
||||
});
|
||||
let fileInput = $state<HTMLInputElement>();
|
||||
|
||||
async function uploadStudySpace() {
|
||||
const imageFile = fileInput?.files?.[0];
|
||||
if (!imageFile) return alert("Please select an image file.");
|
||||
|
||||
const { data: studySpaceInsert, error: studySpaceError } = await supabase
|
||||
.from("study_spaces")
|
||||
.insert(studySpaceData)
|
||||
.select()
|
||||
.single();
|
||||
if (studySpaceError)
|
||||
return alert(`Error uploading study space: ${studySpaceError.message}`);
|
||||
|
||||
const { data: imgUpload, error: imageError } = await supabase.storage
|
||||
.from("files_bucket")
|
||||
.upload(`public/${studySpaceInsert.id}-${imageFile.name}`, imageFile, {
|
||||
contentType: imageFile.type
|
||||
});
|
||||
if (imageError) return alert(`Error uploading image: ${imageError.message}`);
|
||||
|
||||
const { error: imageInsertError } = await supabase
|
||||
.from("study_space_images")
|
||||
.insert({
|
||||
study_space_id: studySpaceInsert.id,
|
||||
image_path: imgUpload.path
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
if (imageInsertError) return alert(`Error creating image: ${imageInsertError.message}`);
|
||||
|
||||
if (fileInput) fileInput.value = ""; // Clear the file input
|
||||
studySpaceData = { ...blankStudySpace }; // Reset the form data
|
||||
alert("Thank you for your contribution!");
|
||||
invalidate("db:study_spaces"); // Invalidate page data so that it refreshes
|
||||
}
|
||||
</script>
|
||||
|
||||
<Navbar>
|
||||
<a href="/space">
|
||||
<img src={crossUrl} alt="new" class="new-space" />
|
||||
</a>
|
||||
</Navbar>
|
||||
|
||||
<main>
|
||||
{#each studySpaces as studySpace (studySpace.id)}
|
||||
{@const imgUrl =
|
||||
@@ -62,7 +24,11 @@
|
||||
.from("files_bucket")
|
||||
.getPublicUrl(studySpace.study_space_images[0].image_path).data.publicUrl
|
||||
: defaultImg}
|
||||
<SpaceCard alt="Photo of {studySpace.description}" imgSrc={imgUrl}>
|
||||
<SpaceCard
|
||||
alt="Photo of {studySpace.description}"
|
||||
href="/space/{studySpace.id}"
|
||||
imgSrc={imgUrl}
|
||||
>
|
||||
{#snippet description()}
|
||||
<p>{studySpace.description}</p>
|
||||
{/snippet}
|
||||
@@ -70,37 +36,6 @@
|
||||
{/each}
|
||||
</main>
|
||||
|
||||
<form
|
||||
onsubmit={(e) => {
|
||||
e.preventDefault();
|
||||
uploadStudySpace();
|
||||
}}
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
name="description"
|
||||
bind:value={studySpaceData.description}
|
||||
placeholder="Study Space Description"
|
||||
required
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
name="location"
|
||||
bind:value={studySpaceData.location}
|
||||
placeholder="Study Space Location within building"
|
||||
required
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
name="building_address"
|
||||
bind:value={studySpaceData.building_address}
|
||||
placeholder="Building Address"
|
||||
required
|
||||
/>
|
||||
<input type="file" name="image" accept=".png, image/png" required bind:this={fileInput} />
|
||||
<button type="submit">Upload some Study Space!</button>
|
||||
</form>
|
||||
|
||||
<style>
|
||||
main {
|
||||
display: grid;
|
||||
@@ -109,10 +44,8 @@
|
||||
padding: 1rem;
|
||||
width: min(600px, 100vw);
|
||||
}
|
||||
form {
|
||||
max-width: 600px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
|
||||
.new-space {
|
||||
transform: rotate(45deg);
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user