From 774113edda1b9219001ef4adab3f4e19c46bcecc Mon Sep 17 00:00:00 2001
From: piotrruss <mail@pruss.it>
Date: Sat, 21 May 2022 18:42:30 +0100
Subject: refactor

---
 components/Edit.jsx | 60 ++++++++---------------------------------------------
 1 file changed, 9 insertions(+), 51 deletions(-)

(limited to 'components/Edit.jsx')

diff --git a/components/Edit.jsx b/components/Edit.jsx
index 3ca7b32..7818637 100644
--- a/components/Edit.jsx
+++ b/components/Edit.jsx
@@ -1,62 +1,20 @@
 import { StyleSheet, Text, TextInput , View } from 'react-native';
 import { useState, useEffect } from 'react'
+import { handleGetNote, handleCreateNote, handleEditNote } from '../helpers';
 import Menu from './Menu'
 
-const Edit = ({ edit, setEdit, session, setSession, showError }) => {
+const Edit = ({ edit: note, setEdit, session, setSession, showError }) => {
   const [saving, setSaving] = useState()
-  const [title, setTitle] = useState(edit ? edit.title : '')
+  const [title, setTitle] = useState(note ? note.title : '')
   const [content, setContent] = useState()
 
-  const fetchNote = async () => {
-    try {
-      const response = await fetch(`https://apps.pruss.it/api/notes/${edit.noteId}`, {
-        method: 'GET', headers: { 'Cookie': session.cookies },
-      })
-      const { content } = await response.json()
-      setContent(content)
-    } catch(e) {
-      showError('Error while fetching note')
-      setEdit(null)
-    }
-  }
-
-  const saveNote = async () => {
-    try {
-      setSaving(true)
-      await fetch(`https://apps.pruss.it/api/notes/${edit._id}`, {
-        method: 'PUT',
-        headers: { 'Content-Type': 'plain/text; charset=utf-8', 'Cookie': session.cookies },
-        body: JSON.stringify({ title, noteId: edit.noteId, content })
-      })
-      setSaving(false)
-      setEdit(null)
-    } catch(e) {
-      showError('Error while saving note')
-      setSaving(false)
-      setEdit(null)
-    }
-  }
-
-  createNote = async () => {
-    try {
-      setSaving(true)
-      await fetch(`https://apps.pruss.it/api/notes`, {
-        method: 'POST',
-        headers: { 'Content-Type': 'plain/text; charset=utf-8', 'Cookie': session.cookies },
-        body: JSON.stringify({ title, content })
-      })
-      setSaving(false)
-      setEdit(null)
-    } catch(e) {
-      showError('Error while saving note')
-      setSaving(false)
-      setEdit(null)
-    }
-  }
+  const saveNote = () => note._id
+    ? handleEditNote({ note, title, content, setSaving, setEdit, session, showError })
+    : handleCreateNote({ title, content, setSaving, setEdit, session, showError })
 
   useEffect(() => {
-    if (edit?._id) {
-      fetchNote()
+    if (note?._id) {
+      handleGetNote({ note, setContent, setEdit, session, showError })
     } else {
       setContent('')
     }
@@ -69,7 +27,7 @@ const Edit = ({ edit, setEdit, session, setSession, showError }) => {
         setSession={setSession}
         showError={showError}
         setEdit={setEdit}
-        saveNote={edit._id ? saveNote : createNote}
+        saveNote={saveNote}
       />
       <View style={styles.container}>
         <TextInput
-- 
cgit v1.2.3

