2024-08-03 22:03:27 -04:00
|
|
|
<script lang="ts">
|
2024-08-04 09:56:58 -04:00
|
|
|
import type { Collection, Note } from '$lib/types';
|
2024-08-03 22:03:27 -04:00
|
|
|
import { createEventDispatcher } from 'svelte';
|
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
import { onMount } from 'svelte';
|
|
|
|
let modal: HTMLDialogElement;
|
|
|
|
|
2024-08-04 09:56:58 -04:00
|
|
|
export let note: Note | null = null;
|
|
|
|
export let collection: Collection;
|
|
|
|
|
|
|
|
let newNote = {
|
|
|
|
name: note?.name || '',
|
|
|
|
content: note?.content || '',
|
|
|
|
date: note?.date || '',
|
|
|
|
links: note?.links || [],
|
|
|
|
collection: collection.id,
|
|
|
|
is_public: collection.is_public
|
|
|
|
};
|
2024-08-03 22:03:27 -04:00
|
|
|
|
2024-08-04 09:56:58 -04:00
|
|
|
let initialName: string = note?.name || '';
|
2024-08-03 22:03:27 -04:00
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
modal = document.getElementById('my_modal_1') as HTMLDialogElement;
|
|
|
|
if (modal) {
|
|
|
|
modal.showModal();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function close() {
|
|
|
|
dispatch('close');
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleKeydown(event: KeyboardEvent) {
|
|
|
|
if (event.key === 'Escape') {
|
|
|
|
dispatch('close');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-04 09:56:58 -04:00
|
|
|
async function save() {
|
|
|
|
if (note && note.id) {
|
|
|
|
const res = await fetch(`/api/notes/${note.id}`, {
|
|
|
|
method: 'PATCH',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify(newNote)
|
|
|
|
});
|
|
|
|
if (res.ok) {
|
|
|
|
let data = await res.json();
|
|
|
|
if (data) {
|
|
|
|
dispatch('save', data);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.error('Failed to save note');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const res = await fetch(`/api/notes/`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify(newNote)
|
|
|
|
});
|
|
|
|
if (res.ok) {
|
2024-08-04 12:37:04 -04:00
|
|
|
let data = await res.json();
|
|
|
|
if (data) {
|
|
|
|
dispatch('create', data);
|
|
|
|
}
|
2024-08-04 09:56:58 -04:00
|
|
|
} else {
|
|
|
|
let data = await res.json();
|
|
|
|
console.error('Failed to save note', data);
|
|
|
|
console.error('Failed to save note');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-03 22:03:27 -04:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<dialog id="my_modal_1" class="modal">
|
|
|
|
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
|
|
|
|
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
|
|
|
|
<div class="modal-box" role="dialog" on:keydown={handleKeydown} tabindex="0">
|
|
|
|
<h3 class="font-bold text-lg">Note Editor</h3>
|
2024-08-04 09:56:58 -04:00
|
|
|
{#if initialName}
|
|
|
|
<p class="font-semibold text-md mb-2">Editing note {initialName}</p>
|
2024-08-03 22:03:27 -04:00
|
|
|
{/if}
|
|
|
|
|
2024-08-04 12:37:04 -04:00
|
|
|
<form on:submit|preventDefault>
|
2024-08-03 22:03:27 -04:00
|
|
|
<div class="form-control mb-2">
|
|
|
|
<label for="name">Name</label>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="name"
|
|
|
|
class="input input-bordered w-full max-w-xs"
|
2024-08-04 09:56:58 -04:00
|
|
|
bind:value={newNote.name}
|
2024-08-03 22:03:27 -04:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="form-control mb-2">
|
|
|
|
<label for="content">Date</label>
|
|
|
|
<input
|
|
|
|
type="date"
|
|
|
|
id="date"
|
|
|
|
name="date"
|
2024-08-04 12:37:04 -04:00
|
|
|
min={collection.start_date || ''}
|
|
|
|
max={collection.end_date || ''}
|
2024-08-04 09:56:58 -04:00
|
|
|
bind:value={newNote.date}
|
2024-08-03 22:03:27 -04:00
|
|
|
class="input input-bordered w-full max-w-xs mt-1"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="form-control mb-2">
|
|
|
|
<label for="content">Content</label>
|
2024-08-04 09:56:58 -04:00
|
|
|
<textarea
|
|
|
|
id="content"
|
|
|
|
class="textarea textarea-bordered"
|
|
|
|
bind:value={newNote.content}
|
|
|
|
rows="5"
|
2024-08-03 22:03:27 -04:00
|
|
|
></textarea>
|
|
|
|
</div>
|
2024-08-04 09:56:58 -04:00
|
|
|
{#if collection.is_public}
|
|
|
|
<p class="text-warning mb-1">
|
|
|
|
This note will be public because it is in a public collection.
|
|
|
|
</p>
|
|
|
|
{/if}
|
2024-08-03 22:03:27 -04:00
|
|
|
<button class="btn btn-primary" on:click={save}>Save</button>
|
2024-08-04 09:56:58 -04:00
|
|
|
<button class="btn btn-neutral" on:click={close}>Close</button>
|
2024-08-03 22:03:27 -04:00
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</dialog>
|