-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
297 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/lib/components/modals/visited-countries/EditVisitedCountry.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<script lang="ts"> | ||
import { Modal } from '$lib/components/ui/modal'; | ||
import type { VisitedCountryStatus } from '$lib/db/types'; | ||
import { Label } from '$lib/components/ui/label'; | ||
import { House, Gift, Plane } from '@o7/icon/lucide'; | ||
import { Tour } from '@o7/icon/material'; | ||
import { type Country, countryFromNumeric } from '$lib/utils/data/countries'; | ||
import { Button } from '$lib/components/ui/button'; | ||
import { api, trpc } from '$lib/trpc'; | ||
import { toast } from 'svelte-sonner'; | ||
import { Textarea } from '$lib/components/ui/input/index.js'; | ||
import { cn } from '$lib/utils'; | ||
let { | ||
open = $bindable(), | ||
editingInfo, | ||
}: { | ||
open: boolean; | ||
editingInfo: { | ||
id: number; | ||
status: (typeof VisitedCountryStatus)[number] | null; | ||
note: string | null; | ||
} | null; | ||
} = $props(); | ||
let countryData: Country | undefined = $state(undefined); | ||
let status: (typeof VisitedCountryStatus)[number] | null = $state(null); | ||
let note: string = $state(''); | ||
$effect(() => { | ||
if (!editingInfo) { | ||
status = null; | ||
note = ''; | ||
return; | ||
} | ||
countryData = countryFromNumeric(+editingInfo.id); | ||
status = editingInfo?.status; | ||
note = editingInfo.note ?? ''; | ||
}); | ||
const save = async () => { | ||
if (!countryData) return; | ||
const success = await api.visitedCountries.save.mutate({ | ||
code: countryData.numeric, | ||
status, | ||
note: note ?? undefined, | ||
}); | ||
if (success) { | ||
await trpc.visitedCountries.list.utils.invalidate(); | ||
open = false; | ||
} else { | ||
toast.error('Failed to save'); | ||
} | ||
}; | ||
</script> | ||
|
||
<Modal bind:open> | ||
<h2 class="text-lg font-bold max-md:mb-2">{countryData?.name}</h2> | ||
<div class="grid grid-cols-2 gap-2"> | ||
{@render statusRadioItem('visited', 'Visited')} | ||
{@render statusRadioItem('lived', 'Lived')} | ||
{@render statusRadioItem('wishlist', 'Wishlist')} | ||
{@render statusRadioItem('layover', 'Layover')} | ||
</div> | ||
<Textarea bind:value={note} placeholder="Note" class="w-full mt-2" /> | ||
<Button | ||
onclick={save} | ||
disabled={(editingInfo?.status === status && editingInfo?.note === note) || | ||
(!status && !editingInfo?.status)} | ||
> | ||
>Save | ||
</Button> | ||
</Modal> | ||
|
||
{#snippet statusRadioItem(value, label)} | ||
<Label | ||
onclick={() => { | ||
if (status === value) { | ||
status = null; | ||
} else { | ||
status = value; | ||
} | ||
}} | ||
class={cn('w-full cursor-pointer', { | ||
'[&>div]:border-primary': value === status, | ||
})} | ||
> | ||
<div | ||
class="border-muted bg-popover hover:bg-accent items-center rounded-md border-2 p-4" | ||
> | ||
<div class="flex items-center justify-center gap-1"> | ||
{#if value === 'lived'} | ||
<House class="w-6 h-6" /> | ||
{:else if value === 'visited'} | ||
<Tour class="w-6 h-6" /> | ||
{:else if value === 'wishlist'} | ||
<Gift class="w-6 h-6" /> | ||
{:else if value === 'layover'} | ||
<Plane class="w-6 h-6" /> | ||
{/if} | ||
<span class="text-2xl font-bold">{label}</span> | ||
</div> | ||
</div> | ||
</Label> | ||
{/snippet} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
import { COUNTRIES } from '$lib/data/countries'; | ||
|
||
type Country = (typeof COUNTRIES)[number]; | ||
export type Country = (typeof COUNTRIES)[number]; | ||
|
||
export const countryFromAlpha = (alpha: string): Country | undefined => { | ||
return COUNTRIES.find((country) => country.alpha === alpha); | ||
}; | ||
|
||
export const countryFromNumeric = (numeric: number): Country | undefined => { | ||
return COUNTRIES.find((country) => country.numeric === numeric); | ||
}; |
Oops, something went wrong.