<script>
import { onMount, createEventDispatcher } from "svelte";
import { fs_create_directory } from "../FilesystemAPI.svelte";
let dispatch = createEventDispatcher()

export let state;
let name_input;
let create_dir_name = ""
let create_dir = () => {
	dispatch("loading", true)

	let form = new FormData()
	form.append("type", "dir")

	fs_create_directory(
		state.bucket.id, state.base.path, create_dir_name,
	).then(resp => {
		create_dir_name = "" // Clear input field
	}).catch(err => {
		alert("Error: "+err)
	}).finally(() => {
		dispatch("done")
	})
}

onMount(() => { name_input.focus() })
</script>

<form class="create_dir highlight_dark" on:submit|preventDefault={create_dir}>
	<img src="/res/img/mime/folder.png" class="icon" alt="icon"/>
	<input class="dirname" type="text" style="width: 100%;" bind:this={name_input} bind:value={create_dir_name} />
	<input class="submit" type="submit" value="create"/>
</form>

<style>
.create_dir {
	display: flex;
	flex-direction: row;
}
.create_dir > img {
	height: 32px;
	width: 32px;
	flex: 0 0 auto;
}
.dirname {
	flex: 1 1 auto;
}
.submit {
	flex: 0 0 auto;
}
</style>
