Monorepo containing core modules of Zeichen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.5 KiB

  1. import Note from '../../models/Note'
  2. import Instance from '../utilities/Instance'
  3. type NoteInstance = Instance<typeof Note.rawAttributes>
  4. export const loadNotes = async () => {
  5. // const localNotes = window.localStorage.getItem('notes')
  6. const localNotes = null
  7. let theNotes: NoteInstance[]
  8. if (localNotes === null) {
  9. const notes = await window.fetch('/api/notes')
  10. theNotes = await notes.json()
  11. window.localStorage.setItem('notes', JSON.stringify(theNotes))
  12. } else {
  13. theNotes = JSON.parse(localNotes)
  14. }
  15. return theNotes
  16. .map(a => ({
  17. ...a,
  18. updatedAt: new Date(a.updatedAt as string),
  19. }))
  20. .sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime())
  21. }
  22. export const loadFolders = async () => {
  23. // const localFolders = window.localStorage.getItem('folders')
  24. const localFolders = null
  25. let theFolders: unknown[]
  26. if (localFolders === null) {
  27. const folders = await window.fetch('/api/folders')
  28. theFolders = await folders.json()
  29. window.localStorage.setItem('folders', JSON.stringify(theFolders))
  30. } else {
  31. theFolders = JSON.parse(localFolders)
  32. }
  33. return theFolders
  34. }
  35. export const saveNote = async params => {
  36. const {
  37. id,
  38. title,
  39. content,
  40. } = params
  41. const response = await window.fetch(`/api/notes/${id}`, {
  42. method: 'put',
  43. body: JSON.stringify({
  44. title,
  45. content,
  46. }),
  47. headers: {
  48. 'Content-Type': 'application/json',
  49. },
  50. })
  51. const body = await response.json()
  52. if (response.status !== 201 && response.status !== 200) {
  53. throw body
  54. }
  55. return body
  56. }