Feat/admin mode #42

Merged
al4423 merged 5 commits from feat/admin-mode into master 2025-06-11 18:00:55 +00:00
5 changed files with 182 additions and 33 deletions
Showing only changes of commit e2795ff257 - Show all commits

View File

@@ -4,20 +4,27 @@
import { onMount } from "svelte"; import { onMount } from "svelte";
interface Props { interface Props {
scrollPosition?: number;
urls?: string[]; urls?: string[];
ondelete?: (idx: number) => void; ondelete?: (idx: number) => void;
} }
const { urls = [], ondelete }: Props = $props(); let { scrollPosition = $bindable(0), urls = [], ondelete }: Props = $props();
let carousel = $state<HTMLDivElement>(); let carousel = $state<HTMLDivElement>();
let scrollPosition = $state(0); let currentPosition = $state(0);
let scrollWidth = $state(0); let scrollWidth = $state(0);
let clientWidth = $state(1); let clientWidth = $state(1);
function updateScroll() { function updateScroll() {
scrollPosition = carousel?.scrollLeft || 0; currentPosition = carousel?.scrollLeft || 0;
scrollWidth = carousel?.scrollWidth || 0; scrollWidth = carousel?.scrollWidth || 0;
clientWidth = carousel?.clientWidth || 1; clientWidth = carousel?.clientWidth || 1;
} }
$effect(() => {
carousel?.scrollTo({
left: scrollPosition * clientWidth,
behavior: "smooth"
});
});
onMount(() => { onMount(() => {
const id = setInterval(() => { const id = setInterval(() => {
if (carousel) { if (carousel) {
@@ -41,7 +48,7 @@
</div> </div>
{/each} {/each}
</div> </div>
{#if scrollPosition > clientWidth / 2} {#if currentPosition > clientWidth / 2}
<button <button
class="arrow left" class="arrow left"
onclick={(e) => { onclick={(e) => {
@@ -52,7 +59,7 @@
<img src={arrowRightUrl} alt="go to previous" /> <img src={arrowRightUrl} alt="go to previous" />
</button> </button>
{/if} {/if}
{#if scrollPosition < scrollWidth - clientWidth * 1.5} {#if currentPosition < scrollWidth - clientWidth * 1.5}
<button <button
class="arrow right" class="arrow right"
onclick={(e) => { onclick={(e) => {
@@ -64,7 +71,7 @@
</button> </button>
{/if} {/if}
<span class="position"> <span class="position">
{Math.round(scrollPosition / clientWidth) + 1} / {urls.length} {Math.round(currentPosition / clientWidth) + 1} / {urls.length}
</span> </span>
</div> </div>

View File

@@ -8,9 +8,17 @@
minHeight?: string; minHeight?: string;
files?: FileList; files?: FileList;
required?: boolean; required?: boolean;
scrollPosition?: number;
} }
let { name, height, minHeight, files = $bindable(), ...rest }: Props = $props(); let {
name,
height,
minHeight,
files = $bindable(),
scrollPosition = $bindable(),
...rest
}: Props = $props();
</script> </script>
<label <label
@@ -34,6 +42,7 @@
} }
files = dt.files; files = dt.files;
}} }}
bind:scrollPosition
/> />
{:else} {:else}
<div class="message"> <div class="message">

View File

@@ -51,7 +51,7 @@
</script> </script>
<Navbar> <Navbar>
<a href="/space"> <a href="/space/new/edit">
<img src={crossUrl} alt="new" class="new-space" /> <img src={crossUrl} alt="new" class="new-space" />
</a> </a>
</Navbar> </Navbar>

View File

@@ -0,0 +1,46 @@
import { error } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
import type { Table } from "$lib";
type StudySpaceData = Omit<
Table<"study_spaces">,
"id" | "created_at" | "updated_at" | "building_location_old" | "building_location"
> & {
id?: string;
building_location?: google.maps.places.PlaceResult;
};
export const load: PageServerLoad = async ({ params, locals: { supabase } }) => {
if (params.id === "new") {
return {
space: {
description: "",
building_location: undefined,
location: "",
tags: [],
volume: "",
power: "",
wifi: ""
} as StudySpaceData
};
}
const { data: space, error: err } = await supabase
.from("study_spaces")
.select("*, study_space_images(*)")
.eq("id", params.id)
.single();
if (err) error(500, "Failed to load study space");
const studySpaceData = space as StudySpaceData & Partial<typeof space>;
const images = studySpaceData.study_space_images || [];
delete studySpaceData.created_at;
delete studySpaceData.updated_at;
delete studySpaceData.study_space_images;
return {
space: studySpaceData as StudySpaceData,
images
};
};

View File

@@ -6,7 +6,6 @@
import crossUrl from "$lib/assets/cross.svg"; import crossUrl from "$lib/assets/cross.svg";
import Button from "$lib/components/Button.svelte"; import Button from "$lib/components/Button.svelte";
import Images from "$lib/components/inputs/Images.svelte"; import Images from "$lib/components/inputs/Images.svelte";
import type { Table } from "$lib";
import { import {
availableStudySpaceTags, availableStudySpaceTags,
wifiTags, wifiTags,
@@ -16,27 +15,32 @@
} from "$lib"; } from "$lib";
import { onMount } from "svelte"; import { onMount } from "svelte";
import type { Json } from "$lib/database.js"; import type { Json } from "$lib/database.js";
const { data } = $props(); const { data } = $props();
const { supabase } = $derived(data); const { supabase } = $derived(data);
const { space, images } = $derived(data);
const studySpaceData = $state({
...space
});
let scrollPosition = $state(0);
const existingImages = $derived(
Promise.all(
images?.map(async ({ image_path }) => {
const { data, error } = await supabase.storage
.from("files_bucket")
.download(image_path);
if (error) {
console.error(`Error downloading image ${image_path}:`, error);
return null;
}
return { data, name: image_path.split("/").pop() || "image", type: data.type };
}) || []
)
);
let spaceImgs = $state<FileList>(); let spaceImgs = $state<FileList>();
let uploading = $state(false); let uploading = $state(false);
let studySpaceData = $state<
Omit<
Table<"study_spaces">,
"id" | "created_at" | "updated_at" | "building_location_old" | "building_location"
> & {
building_location?: google.maps.places.PlaceResult;
}
>({
description: "",
building_location: undefined,
location: "",
tags: [],
volume: "",
power: "",
wifi: ""
});
async function uploadStudySpace() { async function uploadStudySpace() {
if (!spaceImgs || spaceImgs.length < 1) return alert("Please select an image file."); if (!spaceImgs || spaceImgs.length < 1) return alert("Please select an image file.");
@@ -44,10 +48,15 @@
const { data: studySpaceInsert, error: studySpaceError } = await supabase const { data: studySpaceInsert, error: studySpaceError } = await supabase
.from("study_spaces") .from("study_spaces")
.insert({ .upsert(
...studySpaceData, {
building_location: studySpaceData.building_location as Json ...studySpaceData,
}) building_location: studySpaceData.building_location as Json
},
{
onConflict: "id"
}
)
.select() .select()
.single(); .single();
if (studySpaceError) if (studySpaceError)
@@ -60,14 +69,27 @@
const imageFile = spaceImgs![i]; const imageFile = spaceImgs![i];
const resp = await supabase.storage const resp = await supabase.storage
.from("files_bucket") .from("files_bucket")
.upload(`public/${studySpaceInsert.id}-${imageFile.name}`, imageFile, { .upload(
contentType: imageFile.type `public/${studySpaceInsert.id}-${crypto.randomUUID()}-${imageFile.name}`,
}); imageFile,
{
contentType: imageFile.type
}
);
return resp; return resp;
}) })
); );
const imageError = imgUploads.find(({ error }) => error)?.error; const imageError = imgUploads.find(({ error }) => error)?.error;
if (imageError) return alert(`Error uploading image: ${imageError.message}`); if (imageError) return alert(`Error uploading image: ${imageError.message}`);
if (space.id) {
const { error: imageOverwriteError } = await supabase
.from("study_space_images")
.delete()
.eq("study_space_id", space.id);
if (imageOverwriteError)
return alert(`Error overwriting existing images: ${imageOverwriteError.message}`);
}
const { error: imageInsertError } = await supabase const { error: imageInsertError } = await supabase
.from("study_space_images") .from("study_space_images")
@@ -117,6 +139,10 @@
const loader = await gmapsLoader(); const loader = await gmapsLoader();
const places = await loader.importLibrary("places"); const places = await loader.importLibrary("places");
if (!addressInput) return console.error("Address input element not found"); if (!addressInput) return console.error("Address input element not found");
addressInput.value = studySpaceData.building_location?.formatted_address || "";
if (studySpaceData.building_location?.name) {
addressInput.value = `${studySpaceData.building_location.name}, ${addressInput.value}`;
}
const placeAutocomplete = new places.Autocomplete(addressInput, { const placeAutocomplete = new places.Autocomplete(addressInput, {
componentRestrictions: { country: "gb" } componentRestrictions: { country: "gb" }
}); });
@@ -124,6 +150,17 @@
studySpaceData.building_location = placeAutocomplete.getPlace(); studySpaceData.building_location = placeAutocomplete.getPlace();
}); });
}); });
onMount(async () => {
const images = await existingImages;
const dt = new DataTransfer();
images.forEach((response) => {
if (response) {
const file = new File([response.data], response.name, { type: response.type });
dt.items.add(file);
}
});
spaceImgs = dt.files;
});
</script> </script>
<Navbar> <Navbar>
@@ -140,7 +177,40 @@
uploading = false; uploading = false;
}} }}
> >
<Images name="study-space-image" minHeight="16rem" bind:files={spaceImgs} required /> <Images
name="study-space-image"
minHeight="16rem"
bind:files={spaceImgs}
bind:scrollPosition
required
/>
{#if spaceImgs?.length || 0 > 0}
<label class="additionalImages" for="additionalImages">
Add more images
<input
type="file"
name="additionalImages"
id="additionalImages"
multiple
accept=".png, .jpg, .jpeg, .svg"
onchange={function () {
const dt = new DataTransfer();
if (spaceImgs) {
for (let i = 0; i < spaceImgs.length; i++) {
dt.items.add(spaceImgs[i]);
}
}
if (this.files) {
for (let i = 0; i < this.files.length; i++) {
dt.items.add(this.files[i]);
}
}
spaceImgs = dt.files;
scrollPosition = dt.files.length - 1;
}}
/>
</label>
{/if}
<label for="location">Enter the name:</label> <label for="location">Enter the name:</label>
<Text <Text
@@ -205,6 +275,7 @@
dropdownVisible = false; dropdownVisible = false;
}} }}
onkeypress={(event) => { onkeypress={(event) => {
event.preventDefault();
if (event.key === "Enter") { if (event.key === "Enter") {
const tag = filteredTags[0]; const tag = filteredTags[0];
if (tag) addTag(tag)(); if (tag) addTag(tag)();
@@ -401,4 +472,20 @@
background-color: #2e4653; background-color: #2e4653;
color: #eaffeb; color: #eaffeb;
} }
.additionalImages {
padding: 0.5rem 1rem;
border-radius: 0.5rem;
background: linear-gradient(-83deg, #3fb095, #49bd85);
box-shadow: 0rem 0rem 0.5rem #182125;
color: #eaffeb;
border: none;
cursor: pointer;
text-align: center;
}
.additionalImages:focus {
outline: 2px solid #007bff;
}
.additionalImages input {
display: none;
}
</style> </style>