|
- import Note from '../../models/Note'
- import Instance from '../utilities/Instance'
- type NoteInstance = Instance<typeof Note.rawAttributes>
-
- export const loadNotes = async () => {
- // const localNotes = window.localStorage.getItem('notes')
- const localNotes = null
- let theNotes: NoteInstance[]
- if (localNotes === null) {
- const notes = await window.fetch('/api/notes')
- theNotes = await notes.json()
- window.localStorage.setItem('notes', JSON.stringify(theNotes))
- } else {
- theNotes = JSON.parse(localNotes)
- }
- return theNotes
- .map(a => ({
- ...a,
- updatedAt: new Date(a.updatedAt as string),
- }))
- .sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime())
- }
-
- export const loadFolders = async () => {
- // const localFolders = window.localStorage.getItem('folders')
- const localFolders = null
- let theFolders: unknown[]
- if (localFolders === null) {
- const folders = await window.fetch('/api/folders')
- theFolders = await folders.json()
- window.localStorage.setItem('folders', JSON.stringify(theFolders))
- } else {
- theFolders = JSON.parse(localFolders)
- }
- return theFolders
- }
-
- export const saveNote = async params => {
- const {
- id,
- title,
- content,
- } = params
- const response = await window.fetch(`/api/notes/${id}`, {
- method: 'put',
- body: JSON.stringify({
- title,
- content,
- }),
- headers: {
- 'Content-Type': 'application/json',
- },
- })
-
- const body = await response.json()
- if (response.status !== 201 && response.status !== 200) {
- throw body
- }
- return body
- }
|