delete note do be working

This commit is contained in:
dhain 2022-10-04 10:51:59 +02:00
parent b49a4c3ddf
commit 2eeda6fc5c
4 changed files with 18 additions and 23 deletions

View file

@ -1,4 +1,4 @@
import type {Note} from "../types"; import type {Note} from "./types";
export interface NoteRepository { export interface NoteRepository {
getNotes(): Promise<Note[]>; getNotes(): Promise<Note[]>;

View file

@ -1,4 +1,4 @@
import type {Note} from "../types"; import type {Note} from "./types";
import {parseCookies} from "nookies"; import {parseCookies} from "nookies";
import type {NoteRepository} from "./NoteRepository"; import type {NoteRepository} from "./NoteRepository";
@ -53,7 +53,7 @@ export class StrapiNoteRepository implements NoteRepository {
let requestInit: RequestInit = { let requestInit: RequestInit = {
method: method, method: method,
headers: { headers: {
authorization: StrapiNoteRepository.mockedGetAuthorizationHeader() authorization: StrapiNoteRepository.getAuthorizationHeader()
} }
}; };
if (body) { if (body) {

View file

@ -1,9 +1,5 @@
export interface Note { export interface Note {
id: number; id: number;
attributes: Attribute;
}
export interface Attribute {
title: string; title: string;
content: string; content: string;

View file

@ -2,8 +2,11 @@
import type {Note} from "../models/types"; import type {Note} from "../models/types";
import {onMount} from "svelte"; import {onMount} from "svelte";
import {bearerFetch, jwt} from "../models/PomeloUtils"; import {bearerFetch, jwt} from "../models/PomeloUtils";
import type {NoteRepository} from "../models/NoteRepository";
import {StrapiNoteRepository} from "../models/StrapiNoteRepository";
const endpoint = "/notes"; const endpoint = "/notes";
const noteRepo: NoteRepository = StrapiNoteRepository.getInstance();
// //
// //:TODO TEMP!!! // //:TODO TEMP!!!
@ -14,11 +17,9 @@
let notes: Note[]; let notes: Note[];
onMount(async () => { onMount(async () => {
const response = await bearerFetch(endpoint, jwt); notes = await noteRepo.getNotes();
let data = await response.json();
notes = data.data;
notes.forEach(note => { notes.forEach(note => {
note.attributes.lastViewed = new Date(note.attributes.lastViewed); note.lastViewed = new Date(note.lastViewed);
}); });
console.log(notes); console.log(notes);
}); });
@ -53,16 +54,13 @@
*/ */
function addNote(title: string) { function addNote(title: string) {
const date = new Date(); const date = new Date();
const newNoteId: number = (notes.length == 0) ? 0 : notes[notes.length - 1].id + 1 const note: Partial<Note> = {
const note: Note = {
id: newNoteId,
attributes: {
title: title, title: title,
content: "",
lastViewed: date lastViewed: date
}
}; };
notes.push(note);
// notes.push(note);
// noteRepo.createNote(note);
} }
/** /**
@ -82,6 +80,7 @@
*/ */
function deleteNote(note) { function deleteNote(note) {
notes = notes.filter(i => i !== note); notes = notes.filter(i => i !== note);
noteRepo.deleteNote(note.id);
} }
/** /**
@ -106,7 +105,7 @@
*/ */
function onNoteLiClick(note) { function onNoteLiClick(note) {
window.location = "/editor"; window.location = "/editor";
note.attributes.lastViewed = new Date(); note.lastViewed = new Date();
} }
</script> </script>
@ -141,10 +140,10 @@
<div class="row"> <div class="row">
<div class="col-10" on:click={() => onNoteLiClick(note)}> <div class="col-10" on:click={() => onNoteLiClick(note)}>
<div> <div>
{note.attributes.title} {note.title}
</div> </div>
<div class="list-date-text"> <div class="list-date-text">
{note.attributes.lastViewed.toLocaleDateString()} {note.lastViewed.toLocaleDateString()}
</div> </div>
</div> </div>