~thecashewtrader/frensviewer

02d4c0c1b6410ce4eeccef3496e673971b2adb33 — thecashewtrader 1 year, 1 month ago 2c0867c
Add autocomplete
3 files changed, 36 insertions(+), 2 deletions(-)

M src/routes/+page.svelte
A src/routes/api/websites/+server.ts
M src/routes/mutual/+page.svelte
M src/routes/+page.svelte => src/routes/+page.svelte +26 -1
@@ 1,11 1,23 @@
<script lang="ts">
	import { browser } from '$app/environment';
	import { goto } from '$app/navigation';

	let website: string;
	let websites: string[] = [];

	const openWebsite = () => {
		goto(`/website/${website}`);
	};

	const initialiseWebsites = async () => {
		const res = await fetch('/api/websites');
		const json = await res.json();
		websites = json;
	};

	if (browser) {
		initialiseWebsites();
	}
</script>

<svelte:head>


@@ 16,6 28,19 @@

<h2 class="h2">Open A Website</h2>
<form on:submit|preventDefault={openWebsite} class="form">
	<input class="input" type="text" bind:value={website} placeholder="example.com" required />
	<input
		list="websites"
		class="input"
		type="text"
		bind:value={website}
		placeholder="example.com"
		required
	/>
	<input class="btn" type="submit" value="Open" />
</form>

<datalist id="websites">
	{#each websites as website}
		<option value={website} />
	{/each}
</datalist>

A src/routes/api/websites/+server.ts => src/routes/api/websites/+server.ts +9 -0
@@ 0,0 1,9 @@
import { json, type RequestHandler } from '@sveltejs/kit';

export const GET: RequestHandler = async ({ fetch }) => {
	const res = await fetch('https://friends-txt.netlify.app/websites.txt');
	const text = await res.text();
	const websites = text.split('\n').filter((friend) => friend != '');

	return json(websites);
};

M src/routes/mutual/+page.svelte => src/routes/mutual/+page.svelte +1 -1
@@ 31,7 31,7 @@
		const possibleMutualFriends = await res.json();
		// This prevents an older metadata fetch from overwriting a newer "bare" (without metadata) fetch
		if ($page.url.toString() == initialURL && possibleMutualFriends.length > 1) {
		  mutualFriends = possibleMutualFriends
			mutualFriends = possibleMutualFriends;
		}
	};