<template>
    
</template>


<script setup lang="ts">
const { user, loggedIn } = useUserSession();

const toaster = useToast();
const saveStatus = ref<'idle' | 'pending' | 'success' | 'error'>('idle');

const { data: page, status, error } = await useLazyFetch(`/api/navigation`);

if(!loggedIn || (page.value && page.value.owner !== user.value?.id))
{
    useRouter().replace({ name: 'explore-path', params: { path: path.value } });
}

async function save(): Promise<void>
{
    saveStatus.value = 'pending';
    try {
        await $fetch(`/api/file`, {
            method: 'post',
            body: page.value,
            headers: {
                'Content-Type': 'application/json',
            },
        });
        saveStatus.value = 'success';

        toaster.clear('error');
        toaster.add({
            type: 'success', content: 'Contenu enregistré', timer: true, duration: 10000
        });

        useRouter().push({ name: 'explore-path', params: { path: path.value } });
    } catch(e: any) {
        toaster.add({
            type: 'error', content: e.message, timer: true, duration: 10000
        })
        saveStatus.value = 'error';
    }
}
</script>