mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-20 13:29:37 +02:00
notes beta
This commit is contained in:
parent
3f900bc41a
commit
7c2b0e927d
6 changed files with 126 additions and 30 deletions
|
@ -1,15 +1,31 @@
|
|||
<script lang="ts">
|
||||
import type { Note } from '$lib/types';
|
||||
import type { Collection, Note } from '$lib/types';
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
const dispatch = createEventDispatcher();
|
||||
import { onMount } from 'svelte';
|
||||
let modal: HTMLDialogElement;
|
||||
|
||||
export let note: Note;
|
||||
export let note: Note | null = null;
|
||||
export let collection: Collection;
|
||||
|
||||
let editing: boolean = true;
|
||||
|
||||
console.log(collection);
|
||||
|
||||
let newNote = {
|
||||
name: note?.name || '',
|
||||
content: note?.content || '',
|
||||
date: note?.date || '',
|
||||
links: note?.links || [],
|
||||
collection: collection.id,
|
||||
is_public: collection.is_public
|
||||
};
|
||||
console.log(note);
|
||||
|
||||
export let startDate: string | null = null;
|
||||
export let endDate: string | null = null;
|
||||
|
||||
let initialName: string = note.name;
|
||||
let initialName: string = note?.name || '';
|
||||
|
||||
onMount(() => {
|
||||
modal = document.getElementById('my_modal_1') as HTMLDialogElement;
|
||||
|
@ -28,7 +44,40 @@
|
|||
}
|
||||
}
|
||||
|
||||
async function save() {}
|
||||
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) {
|
||||
dispatch('close');
|
||||
} else {
|
||||
let data = await res.json();
|
||||
console.error('Failed to save note', data);
|
||||
console.error('Failed to save note');
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<dialog id="my_modal_1" class="modal">
|
||||
|
@ -36,8 +85,8 @@
|
|||
<!-- 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>
|
||||
{#if initialName !== note.name}
|
||||
<p>Editing note {initialName}</p>
|
||||
{#if initialName}
|
||||
<p class="font-semibold text-md mb-2">Editing note {initialName}</p>
|
||||
{/if}
|
||||
|
||||
<form>
|
||||
|
@ -47,7 +96,7 @@
|
|||
type="text"
|
||||
id="name"
|
||||
class="input input-bordered w-full max-w-xs"
|
||||
bind:value={note.name}
|
||||
bind:value={newNote.name}
|
||||
/>
|
||||
</div>
|
||||
<div class="form-control mb-2">
|
||||
|
@ -58,18 +107,26 @@
|
|||
name="date"
|
||||
min={startDate || ''}
|
||||
max={endDate || ''}
|
||||
bind:value={note.date}
|
||||
bind:value={newNote.date}
|
||||
class="input input-bordered w-full max-w-xs mt-1"
|
||||
/>
|
||||
</div>
|
||||
<div class="form-control mb-2">
|
||||
<label for="content">Content</label>
|
||||
<textarea id="content" class="textarea textarea-bordered" bind:value={note.content} rows="5"
|
||||
<textarea
|
||||
id="content"
|
||||
class="textarea textarea-bordered"
|
||||
bind:value={newNote.content}
|
||||
rows="5"
|
||||
></textarea>
|
||||
</div>
|
||||
|
||||
<button class="btn btn-neutral" on:click={close}>Close</button>
|
||||
{#if collection.is_public}
|
||||
<p class="text-warning mb-1">
|
||||
This note will be public because it is in a public collection.
|
||||
</p>
|
||||
{/if}
|
||||
<button class="btn btn-primary" on:click={save}>Save</button>
|
||||
<button class="btn btn-neutral" on:click={close}>Close</button>
|
||||
</form>
|
||||
</div>
|
||||
</dialog>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue