<script>
    import { onMount, createEventDispatcher } from 'svelte';
    import { emit } from '@tauri-apps/api/event';
    import { invoke } from '@tauri-apps/api/tauri';
    
    export let appState;
    let error = null;

    const dispatch = createEventDispatcher();
    async function respond() {
        let response = {
            id: appState.currentRequest.id,
            approval: 'Approved',
        };

        try {
            await invoke('respond', {response});
            appState.currentRequest = null;
        }
        catch (e) {
            error = e;
        }

        window.setTimeout(() => dispatch('navigate', {target: 'Home'}), 3000);
    }

    onMount(respond);
</script>

{#if error}
    <div class="text-red-400">{error}</div>
{:else}
    <h1 class="text-4xl text-gray-300">Approved!</h1>
{/if}
