<script>
  import { goto } from '$app/navigation';

	 let Err = null;
	 let data;

    console.log(data);
	// $: console.log(data)


 fetch(`/api/v1/list`)
	.then((res) => {
		console.log(`Got res: ${res}`)
		console.log(res)
		return res.json()
	})
	.then((jsonResp) => {
			console.log(`jsonResp: ${jsonResp}`)
			console.log(jsonResp)
		if (jsonResp.Code === 401) {
			console.log(`status code === 401: ${jsonResp.Code}`)
			window.localStorage.removeItem('loggedIn');
			window.localStorage.setItem('loggedIn', 'false');
			goto('/login');
		} else if (jsonResp.Code !== 200) {
			Err = `Received code: ${jsonResp.Code} message: ${jsonResp.Msg}`
		} else {
			window.localStorage.removeItem('loggedIn');
			window.localStorage.setItem('loggedIn', 'true');
			data = jsonResp.Data
		}
	})
	.catch((error) => {
		console.log(error)
		Err = `Encountered error: ${error}`;
	})


</script>

{#if data}
<ul>
    {#each data as paste (paste.id)}
    <li>
        <a href="/view/{paste.id}">
        ID: {paste.id} ({paste.size} bytes)
    </a>
    </li>
    {/each}
</ul>
{:else if Err}
{Err}
{:else}
	Loading....
{/if}
