11import { NoteType } from '../components/note/note-type' ;
22
33export async function getNotes ( ) {
4- const response = await fetch ( '/notes' ) ;
5- return await response . json ( ) ;
4+ const response = await fetch ( '/notes?_sort=updatedAt&_order=desc' ) ;
5+ const notes = await response . json ( ) ;
6+ return notes . map ( ( note :NoteType ) =>
7+ ( { ...note , createdAt :new Date ( note . createdAt ) , updatedAt :new Date ( note . updatedAt ) } ) )
8+
69}
710export async function addNote ( note :NoteType ) {
811const response = await fetch ( '/notes' , {
912method :'post' ,
1013headers :{ 'Content-Type' :'application/json' } ,
1114body :JSON . stringify ( note ) ,
1215} ) ;
13- return await response . json ( ) ;
16+ const noteFromDB = await response . json ( ) ;
17+ return { ...noteFromDB ,
18+ createdAt :new Date ( noteFromDB . createdAt ) ,
19+ updatedAt :new Date ( noteFromDB . updatedAt ) }
1420}
1521export async function deleteNote ( id :string ) {
1622const response = await fetch ( `/notes/${ id } ` , {
@@ -24,5 +30,8 @@ export async function updateNote(id: string, note: NoteType) {
2430headers :{ 'Content-Type' :'application/json' } ,
2531body :JSON . stringify ( note ) ,
2632} ) ;
27- return await response . json ( ) ;
33+ const noteFromDB = await response . json ( ) ;
34+ return { ...noteFromDB ,
35+ createdAt :new Date ( noteFromDB . createdAt ) ,
36+ updatedAt :new Date ( noteFromDB . updatedAt ) }
2837}