This commit is contained in:
Philipp Dormann 2025-05-20 00:41:00 +02:00
parent 3842d8b104
commit 564a971c63
Signed by: philipp
GPG Key ID: 3BB9ADD52DCA4314
2 changed files with 624 additions and 362 deletions

View File

@ -1,390 +1,417 @@
<script> <script>
import { _ } from "svelte-i18n"; import { _ } from "svelte-i18n";
import { import {
DonationService, DonationService,
DonorService, DonorService,
RunnerService, RunnerService,
} from "@odit/lfk-client-js"; } from "@odit/lfk-client-js";
import Select from "svelte-select"; import Select from "svelte-select";
import toast from "svelte-french-toast"; import toast from "svelte-french-toast";
import { onMount } from "svelte"; import { onMount } from "svelte";
import VirtualSelect from "./VirtualSelect.svelte";
let runners = []; //
let donors = []; let options = ["Bulbasaur","Ivysaur","Venusaur","Charmander","Charmeleon","Charizard","Squirtle","Wartortle","Blastoise","Caterpie","Metapod","Butterfree","Weedle","Kakuna","Beedrill","Pidgey","Pidgeotto","Pidgeot","Rattata","Raticate","Spearow","Fearow","Ekans","Arbok","Pikachu","Raichu","Sandshrew","Sandslash","Nidoran♀","Nidorina","Nidoqueen","Nidoran♂","Nidorino","Nidoking","Clefairy","Clefable","Vulpix","Ninetales","Jigglypuff","Wigglytuff","Zubat","Golbat","Oddish","Gloom","Vileplume","Paras","Parasect","Venonat","Venomoth","Diglett","Dugtrio","Meowth","Persian","Psyduck","Golduck","Mankey","Primeape","Growlithe","Arcanine","Poliwag","Poliwhirl","Poliwrath","Abra","Kadabra","Alakazam","Machop","Machoke","Machamp","Bellsprout","Weepinbell","Victreebel","Tentacool","Tentacruel","Geodude","Graveler","Golem","Ponyta","Rapidash","Slowpoke","Slowbro","Magnemite","Magneton","Farfetch'd","Doduo","Dodrio","Seel","Dewgong","Grimer","Muk","Shellder","Cloyster","Gastly","Haunter","Gengar","Onix","Drowzee","Hypno","Krabby","Kingler","Voltorb","Electrode","Exeggcute","Exeggutor","Cubone","Marowak","Hitmonlee","Hitmonchan","Lickitung","Koffing","Weezing","Rhyhorn","Rhydon","Chansey","Tangela","Kangaskhan","Horsea","Seadra","Goldeen","Seaking","Staryu","Starmie","Mr. Mime","Scyther","Jynx","Electabuzz","Magmar","Pinsir","Tauros","Magikarp","Gyarados","Lapras","Ditto","Eevee","Vaporeon","Jolteon","Flareon","Porygon","Omanyte","Omastar","Kabuto","Kabutops","Aerodactyl","Snorlax","Articuno","Zapdos","Moltres","Dratini","Dragonair","Dragonite","Mewtwo","Mew","Chikorita","Bayleef","Meganium","Cyndaquil","Quilava","Typhlosion","Totodile","Croconaw","Feraligatr","Sentret","Furret","Hoothoot","Noctowl","Ledyba","Ledian","Spinarak","Ariados","Crobat","Chinchou","Lanturn","Pichu","Cleffa","Igglybuff","Togepi","Togetic","Natu","Xatu","Mareep","Flaaffy","Ampharos","Bellossom","Marill","Azumarill","Sudowoodo","Politoed","Hoppip","Skiploom","Jumpluff","Aipom","Sunkern","Sunflora","Yanma","Wooper","Quagsire","Espeon","Umbreon","Murkrow","Slowking","Misdreavus","Unown","Wobbuffet","Girafarig","Pineco","Forretress","Dunsparce","Gligar","Steelix","Snubbull","Granbull","Qwilfish","Scizor","Shuckle","Heracross","Sneasel","Teddiursa","Ursaring","Slugma","Magcargo","Swinub","Piloswine","Corsola","Remoraid","Octillery","Delibird","Mantine","Skarmory","Houndour","Houndoom"];
let runnerinfo = { id: 0, firstname: "", lastname: "" }; let selectedOption;
let donorinfo = { id: 0, firstname: "", lastname: "" };
let address = {
address1: "",
address2: "",
city: "",
postalcode: "",
country: "Germany",
};
let amount = null;
let address_checked = false;
let donor_create_new = false;
let last_created = null;
RunnerService.runnerControllerGetAll() function handleSelect(event) {
.then((val) => { selectedOption = event.detail;
runners = val.map((r) => { console.log('Selected:', selectedOption);
return { label: getRunnerLabel(r), value: r };
});
})
.catch((err) => {
console.log("error fetching runners:", err);
});
function loadDonors() {
DonorService.donorControllerGetAll()
.then((val) => {
donors = val.map((r) => {
return { label: getRunnerLabel(r), value: r };
});
console.log("refreshed donors");
setTimeout(() => {
loadDonors;
}, 30000);
})
.catch((err) => {
console.log("error fetching donors:", err);
});
} }
loadDonors(); //
const getRunnerLabel = (option) => let runners = [];
option.firstname + " " + (option.middlename || "") + " " + option.lastname+" [#"+option.id+"]"; let donors = [];
let runnerinfo = { id: 0, firstname: "", lastname: "" };
let donorinfo = { id: 0, firstname: "", lastname: "" };
let address = {
address1: "",
address2: "",
city: "",
postalcode: "",
country: "Germany",
};
let amount = null;
let address_checked = false;
let donor_create_new = false;
let last_created = null;
const filterRunners = (label, filterText, option) => { RunnerService.runnerControllerGetAll()
if (filterText.startsWith("#")) { .then((val) => {
return option.value.id == parseInt(filterText.replace("#", "")); runners = val.map((r) => {
} return { label: getRunnerLabel(r), value: r };
return ( });
label.toLowerCase().includes(filterText.toLowerCase()) || })
option.value.toString().startsWith(filterText.toLowerCase()) .catch((err) => {
); console.log("error fetching runners:", err);
}; });
function resetAll() { function loadDonors() {
runnerinfo = { id: 0, firstname: "", lastname: "" }; DonorService.donorControllerGetAll()
donorinfo = { id: 0, firstname: "", lastname: "" }; .then((val) => {
amount = 0; donors = val.map((r) => {
address_checked = false; return { label: getRunnerLabel(r), value: r };
donor_create_new = false; });
const clears = document.querySelectorAll(".clearSelect"); console.log("refreshed donors");
clears.forEach(c => { setTimeout(() => {
c.click(); loadDonors;
}); }, 30000);
} })
.catch((err) => {
console.log("error fetching donors:", err);
});
}
loadDonors();
onMount(() => { const getRunnerLabel = (option) =>
document.querySelector("#wrapper_runner_select input").focus(); option.firstname +
}) " " +
(option.middlename || "") +
" " +
option.lastname +
" [#" +
option.id +
"]";
const filterRunners = (label, filterText, option) => {
if (filterText.startsWith("#")) {
return option.value.id == parseInt(filterText.replace("#", ""));
}
return (
label.toLowerCase().includes(filterText.toLowerCase()) ||
option.value.toString().startsWith(filterText.toLowerCase())
);
};
function resetAll() {
runnerinfo = { id: 0, firstname: "", lastname: "" };
donorinfo = { id: 0, firstname: "", lastname: "" };
amount = 0;
address_checked = false;
donor_create_new = false;
const clears = document.querySelectorAll(".clearSelect");
clears.forEach((c) => {
c.click();
});
}
onMount(() => {
document.querySelector("#wrapper_runner_select input").focus();
});
</script> </script>
<div class="p-4"> <div class="p-4">
<h3 class="text-3xl font-bold">{$_("fast_donation_create")}</h3> <h3 class="text-3xl font-bold">{$_("fast_donation_create")}</h3>
<!-- --> <!-- -->
<div> <div>
<div class="w-full space-y-4 mb-6"> <div class="w-full space-y-4 mb-6">
{#if last_created} {#if last_created}
<div class="mt-4 p-3 bg-green-50 border border-green-200 rounded-md"> <div class="mt-4 p-3 bg-green-50 border border-green-200 rounded-md">
<p class="text-black"> <p class="text-black">
{$_("last-created-donation")}: #{last_created.id}: {last_created.amountPerDistance / {$_("last-created-donation")}: #{last_created.id}: {last_created.amountPerDistance /
100} € für {getRunnerLabel(last_created.runner)} von {getRunnerLabel( 100} € für {getRunnerLabel(last_created.runner)} von {getRunnerLabel(
last_created.donor last_created.donor
)} )}
</p> </p>
</div> </div>
{/if} {/if}
<!-- Runner Selection --> <!-- -->
<div id="wrapper_runner_select"> <VirtualSelect
<h4 class="text-xl font-semibold">{$_("runner")}</h4> {options}
<Select bind:selected={selectedOption}
containerClasses="rounded-md mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 border bg-gray-50 text-neutral-800 rounded-md p-2" placeholder="Search fruits..."
itemFilter={(label, filterText, option) => on:select={handleSelect}
filterRunners(label, filterText, option)} />
items={runners} {#if selectedOption}
showChevron={true} <p class="mt-4 text-lg">Selected: {selectedOption}</p>
placeholder={$_("search-for-runner-by-name-or-id")} {/if}
noOptionsMessage={$_("no-runners-found")} <!-- -->
on:select={(selectedValue) => {
runnerinfo = selectedValue.detail.value;
document.querySelector("#donation_amount_eur").focus();
}}
on:clear={() => (runnerinfo = { id: 0, firstname: "", lastname: "" })}
/>
</div>
<!-- Amount Input --> <!-- Runner Selection -->
<div> <div id="wrapper_runner_select">
<h4 class="text-xl font-semibold">{$_("amount-per-kilometer")}</h4> <h4 class="text-xl font-semibold">{$_("runner")}</h4>
<div class="mt-1 flex rounded-md shadow-sm"> <Select
<input containerClasses="rounded-md mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 border bg-gray-50 text-neutral-800 rounded-md p-2"
autocomplete="off" itemFilter={(label, filterText, option) =>
class:border-red-500={!amount > 0} filterRunners(label, filterText, option)}
class:focus:border-red-500={!amount > 0} items={runners}
class:focus:ring-red-500={!amount > 0} showChevron={true}
bind:value={amount} placeholder={$_("search-for-runner-by-name-or-id")}
on:keydown={(e)=> noOptionsMessage={$_("no-runners-found")}
{ on:select={(selectedValue) => {
if(e.key==="Enter"){ runnerinfo = selectedValue.detail.value;
e.preventDefault(); document.querySelector("#donation_amount_eur").focus();
document.querySelector("#button_existing_donor").focus(); }}
} on:clear={() => (runnerinfo = { id: 0, firstname: "", lastname: "" })}
}} />
type="number" </div>
step="0.01"
id="donation_amount_eur"
name="donation_amount_eur"
class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2"
placeholder="z.B. 1,50"
/>
<span
class="inline-flex items-center px-3 rounded-r-md border border-neutral-300 bg-neutral-50 text-neutral-500 text-sm"
>€</span
>
</div>
</div>
<!-- Donor Selection --> <!-- Amount Input -->
<div> <div>
<h4 class="text-xl font-semibold">{$_("donor")}</h4> <h4 class="text-xl font-semibold">{$_("amount-per-kilometer")}</h4>
<div class="mt-1 flex rounded-md shadow-sm">
<input
autocomplete="off"
class:border-red-500={!amount > 0}
class:focus:border-red-500={!amount > 0}
class:focus:ring-red-500={!amount > 0}
bind:value={amount}
on:keydown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
document.querySelector("#button_existing_donor").focus();
}
}}
type="number"
step="0.01"
id="donation_amount_eur"
name="donation_amount_eur"
class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2"
placeholder="z.B. 1,50"
/>
<span
class="inline-flex items-center px-3 rounded-r-md border border-neutral-300 bg-neutral-50 text-neutral-500 text-sm"
>€</span
>
</div>
</div>
<!-- Donor Type Toggle --> <!-- Donor Selection -->
<div class="mb-2"> <div>
<div class="flex border rounded-md overflow-hidden shadow-sm"> <h4 class="text-xl font-semibold">{$_("donor")}</h4>
<button
on:keydown={(e)=>
{
if(e.key==="ArrowRight"){
e.preventDefault();
document.querySelector("#button_new_donor").focus();
document.querySelector("#button_new_donor").click();
}
}}
id="button_existing_donor"
class:bg-indigo-600={!donor_create_new}
class:text-white={!donor_create_new}
class="py-2 px-4 w-1/2 transition-colors"
on:click={() => {
donor_create_new = false;
donorinfo = { id: 0, firstname: "", lastname: "" };
}}
>
{$_("existing-donor")}
</button>
<button
on:keydown={(e)=>
{
if(e.key==="ArrowLeft"){
e.preventDefault();
document.querySelector("#button_existing_donor").focus();
document.querySelector("#button_existing_donor").click();
}
}}
id="button_new_donor"
class={`py-2 px-4 w-1/2 transition-colors ${donor_create_new ? "bg-indigo-600 text-white" : "bg-gray-100 text-gray-700"}`}
on:click={() => {
donor_create_new = true;
donorinfo = { id: 0, firstname: "", lastname: "" };
}}
>
{$_("new-donor")}
</button>
</div>
</div>
{#if !donor_create_new} <!-- Donor Type Toggle -->
<Select <div class="mb-2">
containerClasses="rounded-md mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 border bg-gray-50 text-neutral-800 rounded-md p-2" <div class="flex border rounded-md overflow-hidden shadow-sm">
itemFilter={(label, filterText, option) => <button
filterRunners(label, filterText, option)} on:keydown={(e) => {
items={donors} if (e.key === "ArrowRight") {
showChevron={true} e.preventDefault();
placeholder={$_("search-for-donor")} document.querySelector("#button_new_donor").focus();
noOptionsMessage={$_("no-donors-found")} document.querySelector("#button_new_donor").click();
on:select={(selectedValue) => { }
donorinfo = selectedValue.detail.value; }}
}} id="button_existing_donor"
on:clear={() => class:bg-indigo-600={!donor_create_new}
(donorinfo = { id: 0, firstname: "", lastname: "" })} class:text-white={!donor_create_new}
/> class="py-2 px-4 w-1/2 transition-colors"
{:else} on:click={() => {
<div class="space-y-3"> donor_create_new = false;
<!-- First Name --> donorinfo = { id: 0, firstname: "", lastname: "" };
<div> }}
<label >
for="firstname" {$_("existing-donor")}
class="block text-sm font-medium text-gray-700" </button>
> <button
{$_("first-name")} on:keydown={(e) => {
</label> if (e.key === "ArrowLeft") {
<input e.preventDefault();
type="text" document.querySelector("#button_existing_donor").focus();
id="firstname" document.querySelector("#button_existing_donor").click();
bind:value={donorinfo.firstname} }
class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2" }}
placeholder={$_("first-name")} id="button_new_donor"
/> class={`py-2 px-4 w-1/2 transition-colors ${donor_create_new ? "bg-indigo-600 text-white" : "bg-gray-100 text-gray-700"}`}
</div> on:click={() => {
donor_create_new = true;
donorinfo = { id: 0, firstname: "", lastname: "" };
}}
>
{$_("new-donor")}
</button>
</div>
</div>
<!-- Last Name --> {#if !donor_create_new}
<div> <Select
<label containerClasses="rounded-md mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 border bg-gray-50 text-neutral-800 rounded-md p-2"
for="lastname" itemFilter={(label, filterText, option) =>
class="block text-sm font-medium text-gray-700" filterRunners(label, filterText, option)}
> items={donors}
{$_("last-name")} showChevron={true}
</label> placeholder={$_("search-for-donor")}
<input noOptionsMessage={$_("no-donors-found")}
type="text" on:select={(selectedValue) => {
id="lastname" donorinfo = selectedValue.detail.value;
bind:value={donorinfo.lastname} }}
class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2" on:clear={() =>
placeholder={$_("last-name")} (donorinfo = { id: 0, firstname: "", lastname: "" })}
/> />
</div> {:else}
<div class="space-y-3">
<!-- First Name -->
<div>
<label
for="firstname"
class="block text-sm font-medium text-gray-700"
>
{$_("first-name")}
</label>
<input
type="text"
id="firstname"
bind:value={donorinfo.firstname}
class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2"
placeholder={$_("first-name")}
/>
</div>
<!-- Address Checkbox --> <!-- Last Name -->
<div class="flex items-start mt-4"> <div>
<div class="flex items-center h-5"> <label
<input for="lastname"
id="address_check" class="block text-sm font-medium text-gray-700"
type="checkbox" >
bind:checked={address_checked} {$_("last-name")}
class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded" </label>
/> <input
</div> type="text"
<div class="ml-3 text-sm"> id="lastname"
<label for="address_check" class="font-medium text-gray-700"> bind:value={donorinfo.lastname}
{$_("receipt-needed")} class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2"
</label> placeholder={$_("last-name")}
</div> />
</div> </div>
{#if address_checked} <!-- Address Checkbox -->
<!-- Address Fields --> <div class="flex items-start mt-4">
<div <div class="flex items-center h-5">
class="space-y-3 mt-3 p-3 border border-gray-200 rounded-md bg-gray-50" <input
> id="address_check"
<div> type="checkbox"
<label bind:checked={address_checked}
for="address1" class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded"
class="block text-sm font-medium text-gray-700" />
> </div>
{$_("address")} <div class="ml-3 text-sm">
</label> <label for="address_check" class="font-medium text-gray-700">
<input {$_("receipt-needed")}
type="text" </label>
id="address1" </div>
bind:value={address.address1} </div>
class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2"
/>
</div>
<div> {#if address_checked}
<label <!-- Address Fields -->
for="address2" <div
class="block text-sm font-medium text-gray-700" class="space-y-3 mt-3 p-3 border border-gray-200 rounded-md bg-gray-50"
> >
{$_("apartment-suite-etc")} <div>
</label> <label
<input for="address1"
type="text" class="block text-sm font-medium text-gray-700"
id="address2" >
bind:value={address.address2} {$_("address")}
class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2" </label>
/> <input
</div> type="text"
id="address1"
bind:value={address.address1}
class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2"
/>
</div>
<div class="grid grid-cols-2 gap-3"> <div>
<div> <label
<label for="address2"
for="postalcode" class="block text-sm font-medium text-gray-700"
class="block text-sm font-medium text-gray-700" >
> {$_("apartment-suite-etc")}
{$_("zip-postal-code")} </label>
</label> <input
<input type="text"
type="text" id="address2"
id="postalcode" bind:value={address.address2}
bind:value={address.postalcode} class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2"
class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2" />
/> </div>
</div>
<div> <div class="grid grid-cols-2 gap-3">
<label <div>
for="city" <label
class="block text-sm font-medium text-gray-700" for="postalcode"
> class="block text-sm font-medium text-gray-700"
{$_("city")} >
</label> {$_("zip-postal-code")}
<input </label>
type="text" <input
id="city" type="text"
bind:value={address.city} id="postalcode"
class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2" bind:value={address.postalcode}
/> class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2"
</div> />
</div> </div>
</div>
{/if}
</div>
{/if}
</div>
<!-- Submit Button -->
<div class="mt-6">
<button
id="submit_button"
type="button"
class="w-full inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:bg-gray-400 disabled:cursor-not-allowed"
disabled={!amount > 0 ||
!runnerinfo.id ||
(!donorinfo.id && !donor_create_new) ||
(donor_create_new &&
(!donorinfo.firstname || !donorinfo.lastname)) ||
(donor_create_new &&
address_checked &&
(!address.address1 || !address.city || !address.postalcode))}
on:click={async () => {
if (donor_create_new) {
donorinfo = await DonorService.donorControllerPost({
firstname: donorinfo.firstname,
lastname: donorinfo.lastname,
receiptNeeded: address_checked,
...(address_checked ? { address: address } : {}),
});
}
DonationService.donationControllerPostDistance({ <div>
donor: donorinfo.id, <label
runner: runnerinfo.id, for="city"
amountPerDistance: amount * 100, class="block text-sm font-medium text-gray-700"
}) >
.then((data) => { {$_("city")}
last_created = data; </label>
toast.success($_("donation-created-successfully")); <input
resetAll(); type="text"
loadDonors(); id="city"
}) bind:value={address.city}
.catch((err) => { class="focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-l-md sm:text-sm border-neutral-300 border bg-neutral-50 text-neutral-800 p-2"
console.error("Error creating donation:", err); />
toast.error($_("error-creating-donation")); </div>
}); </div>
}} </div>
> {/if}
{$_("create")} </div>
</button> {/if}
</div> </div>
</div> <!-- Submit Button -->
</div> <div class="mt-6">
<button
id="submit_button"
type="button"
class="w-full inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:bg-gray-400 disabled:cursor-not-allowed"
disabled={!amount > 0 ||
!runnerinfo.id ||
(!donorinfo.id && !donor_create_new) ||
(donor_create_new &&
(!donorinfo.firstname || !donorinfo.lastname)) ||
(donor_create_new &&
address_checked &&
(!address.address1 || !address.city || !address.postalcode))}
on:click={async () => {
if (donor_create_new) {
donorinfo = await DonorService.donorControllerPost({
firstname: donorinfo.firstname,
lastname: donorinfo.lastname,
receiptNeeded: address_checked,
...(address_checked ? { address: address } : {}),
});
}
DonationService.donationControllerPostDistance({
donor: donorinfo.id,
runner: runnerinfo.id,
amountPerDistance: amount * 100,
})
.then((data) => {
last_created = data;
toast.success($_("donation-created-successfully"));
resetAll();
loadDonors();
})
.catch((err) => {
console.error("Error creating donation:", err);
toast.error($_("error-creating-donation"));
});
}}
>
{$_("create")}
</button>
</div>
</div>
</div>
</div> </div>
<style> <style>
:global(:root) { :global(:root) {
--sv-bg: #ffffff; --sv-bg: #ffffff;
} }
</style> </style>

View File

@ -0,0 +1,235 @@
<script>
import { createEventDispatcher, onMount } from "svelte";
// Props
export let options = [];
export let selected = null;
export let placeholder = "Search options...";
// Internal state
let searchTerm = "";
let filteredOptions = options;
let isOpen = false;
let container;
let visibleItems = [];
let startIndex = 0;
let itemHeight = 40; // Fixed height for each option (in pixels)
let visibleCount = 10; // Number of items to render (adjusted based on container height)
let focusedIndex = -1; // Track the focused option index (-1 means no focus)
const dispatch = createEventDispatcher();
// Filter options based on search term
$: {
filteredOptions = searchTerm
? options.filter((option) =>
option.toLowerCase().includes(searchTerm.toLowerCase())
)
: options;
// Reset scroll and focus when filtered options change
startIndex = 0;
focusedIndex = -1;
updateVisibleItems();
}
// Update visible items based on scroll position
function updateVisibleItems() {
if (!container) return;
const scrollTop = container.scrollTop;
startIndex = Math.floor(scrollTop / itemHeight);
const endIndex = Math.min(
startIndex + visibleCount,
filteredOptions.length
);
visibleItems = filteredOptions.slice(startIndex, endIndex);
}
// Handle scroll event
function handleScroll() {
updateVisibleItems();
}
// Calculate visible item count based on container height
function updateVisibleCount() {
if (container) {
visibleCount = Math.ceil(container.clientHeight / itemHeight) + 2; // Buffer of 2 items
updateVisibleItems();
}
}
// Handle option selection
function selectOption(option) {
selected = option;
isOpen = false;
searchTerm = "";
focusedIndex = -1;
dispatch("onSelected", option);
}
// Toggle dropdown
function toggleDropdown() {
isOpen = !isOpen;
if (isOpen) {
// Update visible count when dropdown opens
setTimeout(updateVisibleCount, 0); // Ensure container is rendered
focusedIndex = -1; // Reset focus when opening
}
}
// Handle click outside to close dropdown
function handleClickOutside(event) {
if (!event.target.closest(".select-container")) {
isOpen = false;
focusedIndex = -1;
}
}
// Handle input focus to open dropdown
function handleInputFocus() {
isOpen = true;
}
// Handle keyboard navigation
function handleKeydown(event, index) {
if (!isOpen) return;
if (event.key === "ArrowDown") {
event.preventDefault();
if (focusedIndex < filteredOptions.length - 1) {
focusedIndex += 1;
scrollToFocusedItem();
}
} else if (event.key === "ArrowUp") {
event.preventDefault();
if (focusedIndex > 0) {
focusedIndex -= 1;
scrollToFocusedItem();
} else if (focusedIndex === -1 && filteredOptions.length > 0) {
focusedIndex = 0;
scrollToFocusedItem();
}
} else if (event.key === "Enter" && index >= 0) {
event.preventDefault();
selectOption(filteredOptions[index]);
}
}
// Scroll to the focused item
function scrollToFocusedItem() {
if (!container || focusedIndex < 0) return;
const itemTop = focusedIndex * itemHeight;
const itemBottom = itemTop + itemHeight;
const containerTop = container.scrollTop;
const containerBottom = containerTop + container.clientHeight;
if (itemTop < containerTop) {
container.scrollTop = itemTop;
} else if (itemBottom > containerBottom) {
container.scrollTop = itemBottom - container.clientHeight;
}
updateVisibleItems();
}
// Initialize container size observer
onMount(() => {
if (container) {
const resizeObserver = new ResizeObserver(updateVisibleCount);
resizeObserver.observe(container);
return () => resizeObserver.disconnect();
}
});
</script>
<svelte:window on:click={handleClickOutside} />
<div class="select-container relative w-full">
<!-- Select element with inline search -->
<div
class="border rounded-md px-3 py-2 bg-white shadow-sm flex items-center"
role="combobox"
aria-expanded={isOpen}
>
<input
type="text"
bind:value={searchTerm}
placeholder={selected || placeholder}
class="w-full bg-transparent focus:outline-none text-gray-700"
on:focus={handleInputFocus}
on:keydown={(e) => {
if (e.key === "Enter" && !isOpen) {
toggleDropdown();
} else {
handleKeydown(e, focusedIndex);
}
}}
aria-label="Search and select an option"
/>
<svg
class="w-4 h-4 text-gray-500 transform {isOpen ? 'rotate-180' : ''}"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
on:click={toggleDropdown}
role="button"
tabindex="0"
on:keydown={(e) => e.key === "Enter" && toggleDropdown()}
aria-label="Toggle dropdown"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M19 9l-7 7-7-7"
/>
</svg>
</div>
<!-- Dropdown -->
{#if isOpen}
<div
class="absolute z-10 w-full mt-1 bg-white border rounded-md shadow-lg max-h-80 overflow-auto"
bind:this={container}
on:scroll={handleScroll}
role="listbox"
>
{#if filteredOptions.length > 0}
<!-- Virtualized list container -->
<div style="height: {filteredOptions.length * itemHeight}px;">
<div style="transform: translateY({startIndex * itemHeight}px);">
{#each visibleItems as item, i (item + "-" + (startIndex + i))}
<div
class="px-3 py-2 hover:bg-blue-100 cursor-pointer {selected ===
item
? 'bg-blue-50'
: ''} {focusedIndex === startIndex + i
? 'bg-blue-200 outline outline-2 outline-blue-500'
: ''}"
on:click={() => selectOption(item)}
on:keydown={(e) => handleKeydown(e, startIndex + i)}
role="option"
tabindex="0"
aria-selected={selected === item}
>
{item}
</div>
{/each}
</div>
</div>
{:else}
<div class="px-3 py-2 text-gray-500">No options found</div>
{/if}
</div>
{/if}
</div>
<style>
/* Ensure Tailwind classes handle additional styling */
:global(.select-container input:focus) {
border-color: #3b82f6; /* Tailwind's blue-500 */
}
:global([role="option"]:focus) {
outline: 2px solid #3b82f6;
outline-offset: -2px;
}
</style>