feat: Added filtering by optional tags in the main page. Created TagFilter... #40

Merged
tt2022 merged 8 commits from feat/filter-by-tags into master 2025-06-11 02:45:24 +00:00
19 changed files with 281 additions and 440 deletions
Showing only changes of commit 19d451fa8e - Show all commits

View File

@@ -0,0 +1,202 @@
<script lang="ts">
import crossUrl from "$lib/assets/cross.svg";
export let selectedTags: string[] = [];
export let tagFilter: string;
export let tagFilterElem;
export let filteredTags;
export let dropdownVisible: boolean;
export let deleteTag;
export let addTag;
</script>
<form>
<div class="tagDisplay">
{#each selectedTags as tagName (tagName)}
<button class="tag" onclick={deleteTag(tagName)} type="button">
{tagName}
<img src={crossUrl} alt="delete" /></button
>
{/each}
<input
type="text"
name="tagInput"
class="tagInput"
bind:value={tagFilter}
bind:this={tagFilterElem}
onfocus={() => {
dropdownVisible = true;
}}
onblur={() => {
dropdownVisible = false;
}}
onkeypress={(event) => {
if (event.key === "Enter") {
const tag = filteredTags[0];
if (tag) addTag(tag)();
}
}}
placeholder="Filter by tags..."
/>
{#if dropdownVisible}
<div class="tagDropdown">
{#each filteredTags as avaliableTag (avaliableTag)}
<button
class="avaliableTag"
onclick={addTag(avaliableTag)}
onmousedown={(e) => {
// Keep input focused
e.preventDefault();
e.stopPropagation();
}}
type="button"
>
{avaliableTag}
</button>
{/each}
</div>
{/if}
</div>
</form>
<style>
form {
display: flex;
flex-direction: column;
padding: 1.5rem;
gap: 0.5rem;
max-width: 32rem;
margin: 0 auto;
}
label {
color: #ffffff;
margin-top: 0.5rem;
}
.submit {
position: sticky;
display: flex;
flex-direction: column;
margin-top: 0.5rem;
bottom: 0;
margin-left: -0.5rem;
width: calc(100% + 1rem);
}
.tagDisplay {
display: flex;
gap: 0.4rem;
flex-wrap: wrap;
align-items: left;
justify-content: left;
position: relative;
width: 100%;
height: auto;
padding: 0.5rem;
border-radius: 0.5rem;
border: 2px solid #eaffeb;
background: none;
color: #eaffeb;
font-size: 1rem;
}
.tagInput {
width: 100%;
height: 100%;
background: none;
color: #eaffeb;
font-size: 1rem;
border: none;
outline: none;
}
::placeholder {
color: #859a90;
opacity: 1;
}
.tag {
display: flex;
align-items: center;
border-radius: 0.25rem;
background-color: #2e4653;
color: #eaffeb;
font-size: 0.9rem;
cursor: pointer;
border-width: 0rem;
}
.tag img {
width: 1rem;
height: 1rem;
margin-left: 0.2rem;
}
.tagDropdown {
width: 100%;
display: flex;
gap: 0.4rem;
flex-wrap: wrap;
position: absolute;
background-color: #2e4653;
box-shadow: 1px 1px 0.5rem rgba(0, 0, 0, 0.5);
border-radius: 0.5rem;
overflow-y: auto;
max-height: 10rem;
top: 100%;
left: 50%;
transform: translateX(-50%);
}
.avaliableTag {
width: 100%;
text-align: left;
background: none;
border: none;
color: #eaffeb;
font-size: 0.9rem;
margin: 0%;
padding: 0 0.8rem 0.4rem;
}
.avaliableTag:first-child {
padding-top: 0.6rem;
background-color: hsl(201, 26%, 60%);
}
.avaliableTag:last-child {
padding-bottom: 0.6rem;
}
.compulsoryTags {
display: grid;
gap: 0.4rem;
border-radius: 0.5rem;
background-color: none;
width: 100%;
font-size: 1rem;
grid-template-columns: repeat(3, 1fr);
}
.compulsoryContainer {
display: flex;
flex-direction: column;
align-items: left;
justify-content: top;
border-radius: 0.5rem;
background-color: none;
}
.compulsoryTagSelect {
width: 100%;
height: 100%;
padding: 0.5rem;
border-radius: 0.5rem;
border: 2px solid #eaffeb;
background: none;
color: #eaffeb;
font-size: 0.9rem;
text-align: left;
}
.compulsoryTagSelect option {
background-color: #2e4653;
color: #eaffeb;
}
</style>

View File

@@ -24,3 +24,5 @@ export const availableStudySpaceTags = [
export const volumeTags = ["Silent", "Quiet", "Some Noise", "Loud"]; export const volumeTags = ["Silent", "Quiet", "Some Noise", "Loud"];
export const wifiTags = ["Good WiFi", "Moderate WiFi", "Bad WiFi", "No WiFi"]; export const wifiTags = ["Good WiFi", "Moderate WiFi", "Bad WiFi", "No WiFi"];
export const powerOutletTags = ["Many Outlets", "Some Outlets", "No Outlets"]; export const powerOutletTags = ["Many Outlets", "Some Outlets", "No Outlets"];
export const allTags = [...availableStudySpaceTags, ...volumeTags, ...wifiTags, ...powerOutletTags]

View File

@@ -3,19 +3,69 @@
import defaultImg from "$lib/assets/study_space.png"; import defaultImg from "$lib/assets/study_space.png";
import crossUrl from "$lib/assets/cross.svg"; import crossUrl from "$lib/assets/cross.svg";
import Navbar from "$lib/components/Navbar.svelte"; import Navbar from "$lib/components/Navbar.svelte";
import TagFilter from "$lib/components/inputs/TagFilter.svelte";
import {availableStudySpaceTags} from "$lib"
const { data } = $props(); const { data } = $props();
const { studySpaces, supabase } = $derived(data); const { studySpaces, supabase } = $derived(data);
let selectedTags = $state<string[]>([])
let tagFilter = $state("");
let tagFilterElem = $state<HTMLInputElement>()
let filteredTags = $derived(
availableStudySpaceTags
.filter((tag) => tag.toLowerCase().includes(tagFilter.toLowerCase()))
.filter((tag) => availableStudySpaceTags.includes(tag))
)
let filteredStudySpaces = $derived(() =>
selectedTags.length == 0
? studySpaces
: studySpaces.filter(space =>
selectedTags.every(tag =>
(space.tags || []).includes(tag)
)
)
);
let dropdownVisible = $state(false)
function deleteTag(tagName: string) {
return () => {
selectedTags = selectedTags.filter((tag) => tag !== tagName);
};
}
function addTag(tagName: string) {
return () => {
if (!selectedTags.includes(tagName)) {
selectedTags.push(tagName);
}
tagFilter = "";
};
}
</script> </script>
<Navbar> <Navbar>
<a href="/space"> <a href="/space">
<img src={crossUrl} alt="new" class="new-space" /> <img src={crossUrl} alt="new" class="new-space" />
</a> </a>
</Navbar> </Navbar>
<main> <main>
{#each studySpaces as studySpace (studySpace.id)} <TagFilter
selectedTags={selectedTags}
tagFilter={tagFilter}
tagFilterElem={tagFilterElem}
filteredTags={filteredTags}
dropdownVisible={dropdownVisible}
deleteTag={deleteTag}
addTag={addTag}
/>
{#each studySpaces.filter(space =>
selectedTags.length === 0 ||
selectedTags.every(tag => (space.tags || []).includes(tag))
) as studySpace (studySpace.id)}
{@const imgUrl = {@const imgUrl =
studySpace.study_space_images.length > 0 studySpace.study_space_images.length > 0
? supabase.storage ? supabase.storage