1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-23 14:59:36 +02:00

chore: Add delete functionality to NoteCard component

This commit is contained in:
Sean Morley 2024-08-04 12:37:04 -04:00
parent 7c2b0e927d
commit 89bf310340
3 changed files with 35 additions and 16 deletions

View file

@ -6,13 +6,25 @@
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
import Launch from '~icons/mdi/launch'; import Launch from '~icons/mdi/launch';
import FileDocumentEdit from '~icons/mdi/file-document-edit'; import TrashCan from '~icons/mdi/trash-can';
export let note: Note; export let note: Note;
function editNote() { function editNote() {
dispatch('edit', note); dispatch('edit', note);
} }
async function deleteNote() {
const res = await fetch(`/api/notes/${note.id}`, {
method: 'DELETE'
});
if (res.ok) {
addToast('success', 'Note deleted successfully');
dispatch('delete', note.id);
} else {
addToast('Failed to delete note', 'error');
}
}
</script> </script>
<div <div
@ -27,6 +39,12 @@
<button class="btn btn-neutral mb-2" on:click={editNote}> <button class="btn btn-neutral mb-2" on:click={editNote}>
<Launch class="w-6 h-6" />Open <Launch class="w-6 h-6" />Open
</button> </button>
<button
id="delete_adventure"
data-umami-event="Delete Adventure"
class="btn btn-warning"
on:click={deleteNote}><TrashCan class="w-6 h-6" />Delete</button
>
</div> </div>
</div> </div>
</div> </div>

View file

@ -8,10 +8,6 @@
export let note: Note | null = null; export let note: Note | null = null;
export let collection: Collection; export let collection: Collection;
let editing: boolean = true;
console.log(collection);
let newNote = { let newNote = {
name: note?.name || '', name: note?.name || '',
content: note?.content || '', content: note?.content || '',
@ -20,10 +16,6 @@
collection: collection.id, collection: collection.id,
is_public: collection.is_public 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 || '';
@ -70,7 +62,10 @@
body: JSON.stringify(newNote) body: JSON.stringify(newNote)
}); });
if (res.ok) { if (res.ok) {
dispatch('close'); let data = await res.json();
if (data) {
dispatch('create', data);
}
} else { } else {
let data = await res.json(); let data = await res.json();
console.error('Failed to save note', data); console.error('Failed to save note', data);
@ -89,7 +84,7 @@
<p class="font-semibold text-md mb-2">Editing note {initialName}</p> <p class="font-semibold text-md mb-2">Editing note {initialName}</p>
{/if} {/if}
<form> <form on:submit|preventDefault>
<div class="form-control mb-2"> <div class="form-control mb-2">
<label for="name">Name</label> <label for="name">Name</label>
<input <input
@ -105,8 +100,8 @@
type="date" type="date"
id="date" id="date"
name="date" name="date"
min={startDate || ''} min={collection.start_date || ''}
max={endDate || ''} max={collection.end_date || ''}
bind:value={newNote.date} bind:value={newNote.date}
class="input input-bordered w-full max-w-xs mt-1" class="input input-bordered w-full max-w-xs mt-1"
/> />

View file

@ -183,7 +183,7 @@
let isEditModalOpen: boolean = false; let isEditModalOpen: boolean = false;
let isTransportationEditModalOpen: boolean = false; let isTransportationEditModalOpen: boolean = false;
let isNoteModalOpen: boolean = false; let isNoteModalOpen: boolean = false;
let noteToEdit: Note; let noteToEdit: Note | null;
let newType: string; let newType: string;
@ -247,8 +247,6 @@
<NoteModal <NoteModal
note={noteToEdit} note={noteToEdit}
on:close={() => (isNoteModalOpen = false)} on:close={() => (isNoteModalOpen = false)}
startDate={collection.start_date}
endDate={collection.end_date}
{collection} {collection}
on:save={(event) => { on:save={(event) => {
notes = notes.map((note) => { notes = notes.map((note) => {
@ -260,6 +258,10 @@
isNoteModalOpen = false; isNoteModalOpen = false;
}} }}
on:close={() => (isNoteModalOpen = false)} on:close={() => (isNoteModalOpen = false)}
on:create={(event) => {
notes = [event.detail, ...notes];
isNoteModalOpen = false;
}}
/> />
{/if} {/if}
@ -387,6 +389,7 @@
on:click={() => { on:click={() => {
isNoteModalOpen = true; isNoteModalOpen = true;
newType = ''; newType = '';
noteToEdit = null;
}} }}
> >
Note</button Note</button
@ -523,6 +526,9 @@
noteToEdit = event.detail; noteToEdit = event.detail;
isNoteModalOpen = true; isNoteModalOpen = true;
}} }}
on:delete={(event) => {
notes = notes.filter((n) => n.id != event.detail);
}}
/> />
{/each} {/each}
{/if} {/if}