thomaswilson-sveltekit/src/components/games/ApiPasswordForm.svelte

51 lines
1.2 KiB
Svelte
Raw Normal View History

<script lang="ts">
2024-06-18 06:23:06 +00:00
import { writable } from "svelte/store";
import { onMount, onDestroy, createEventDispatcher } from "svelte";
2024-06-18 06:23:06 +00:00
const dispatch = createEventDispatcher<{ change: string }>();
2024-06-18 06:23:06 +00:00
let apiPassword = "";
let state: "edit" | "view" = "edit";
let unsubscribe: () => void;
2024-06-18 06:23:06 +00:00
function onSubmit() {
state = "view";
if (localStorage) {
localStorage.setItem("apiPassword", apiPassword);
}
dispatch("change", apiPassword);
}
2024-06-18 06:23:06 +00:00
function onEdit() {
state = "edit";
}
2024-06-18 06:23:06 +00:00
onMount(() => {
if (localStorage !== undefined) {
apiPassword = localStorage.getItem("apiPassword") || "";
}
2024-06-18 06:23:06 +00:00
if (apiPassword.length > 0) {
dispatch("change", apiPassword);
state = "view";
}
});
</script>
<section>
2024-06-18 06:23:06 +00:00
{#if apiPassword.length === 0}
<p>
To save things to the ledger you need to enter the password. Right now you
haven't set one.
</p>
{/if}
{#if state === "view"}
<button on:click={onEdit}>Edit Password</button>
{:else}
<form on:submit|preventDefault={onSubmit}>
<input type="text" bind:value={apiPassword} />
<input type="submit" value="Set Password" />
</form>
{/if}
</section>