Skip to content

Commit

Permalink
feat: edit custom airports
Browse files Browse the repository at this point in the history
  • Loading branch information
johanohly committed Jan 10, 2025
1 parent 8714856 commit 2d255ac
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<script lang="ts">
import autoAnimate from '@formkit/auto-animate';
import { SquarePen, X } from '@o7/icon/lucide';
import { X } from '@o7/icon/lucide';
import { toast } from 'svelte-sonner';
import CreateAirport from './CreateAirport.svelte';
import EditAirport from './EditAirport.svelte';
import { Confirm } from '$lib/components/helpers';
import { Button } from '$lib/components/ui/button';
Expand Down Expand Up @@ -52,9 +53,7 @@
</p>
</div>
<div class="flex items-center gap-2">
<Button variant="outline" size="icon">
<SquarePen size="20" />
</Button>
<EditAirport {airport} />
<Confirm
onConfirm={async () => deleteAirport(airport.code)}
title="Remove Airport"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<script lang="ts">
import { SquarePen } from '@o7/icon/lucide';
import { toast } from 'svelte-sonner';
import { defaults, type Infer, superForm } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import AirportFormFields from '$lib/components/modals/settings/pages/data-page/airport/AirportFormFields.svelte';
import { Button } from '$lib/components/ui/button';
import * as Dialog from '$lib/components/ui/dialog';
import * as Form from '$lib/components/ui/form';
import type { Airport } from '$lib/db/types';
import { trpc } from '$lib/trpc';
import { airportSchema } from '$lib/zod/airport';
let {
airport,
}: {
airport: Airport;
} = $props();
let open = $state(false);
const form = superForm(
defaults<Infer<typeof airportSchema>>(
{ ...airport, iata: airport.iata || '' },
zod(airportSchema),
),
{
dataType: 'json',
id: Math.random().toString(36).substring(7),
validators: zod(airportSchema),
onUpdated({ form }) {
if (form.message) {
if (form.message.type === 'success') {
trpc.flight.list.utils.invalidate();
open = false;
return void toast.success(form.message.text);
}
toast.error(form.message.text);
}
},
},
);
const { enhance } = form;
</script>

<Dialog.Root bind:open>
<Dialog.Trigger>
{#snippet child({ props })}
<Button variant="outline" size="icon" {...props}>
<SquarePen size="20" />
</Button>
{/snippet}
</Dialog.Trigger>
<Dialog.Content
preventScroll={false}
interactOutsideBehavior="ignore"
class="max-h-full overflow-y-auto max-w-lg"
>
<h2>Edit Airport</h2>
<form
method="POST"
action="/api/airport/save/form"
use:enhance
class="grid gap-4"
>
<AirportFormFields {form} />
<Form.Button>Save</Form.Button>
</form>
</Dialog.Content>
</Dialog.Root>
8 changes: 6 additions & 2 deletions src/lib/server/utils/airport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from 'zod';

import { db } from '$lib/db';
import { findAirportsPrimitive } from '$lib/db/queries';
import type { Airport, CreateAirport, User } from '$lib/db/types';
import type { Airport, CreateAirport } from '$lib/db/types';
import type { ErrorActionResult } from '$lib/utils/forms';
import type { airportSchema } from '$lib/zod/airport';

Expand Down Expand Up @@ -41,7 +41,11 @@ export const validateAndSaveAirport = async (
return { success: false, type: 'path', path, message } as const;
};

const airport = { ...data, custom: true };
const airport = {
...data,
iata: data.iata !== '' ? data.iata : null,
custom: true,
};

const code = airport.code;
const existingAirport = await getAirport(code);
Expand Down

0 comments on commit 2d255ac

Please sign in to comment.