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/List.jsx | 71 ++++++-----------------------------------------------
 1 file changed, 8 insertions(+), 63 deletions(-)

(limited to 'components/List.jsx')

diff --git a/components/List.jsx b/components/List.jsx
index b7bdc07..b280d4a 100644
--- a/components/List.jsx
+++ b/components/List.jsx
@@ -1,8 +1,11 @@
-import { StyleSheet, Text, View, Pressable, Alert } from 'react-native'
+import { StyleSheet, Text } from 'react-native'
 import SwipeableFlatList from 'react-native-swipeable-list'
 import { useState, useEffect } from 'react'
+import { getList } from '../api'
 import Note from './Note'
 import Menu from './Menu'
+import RemoveNoteButton from './RemoveNoteButton'
+import { handleRemove } from "../helpers";
 
 const List = ({ session, setSession, showError, setEdit }) => {
   const [list, setList] = useState()
@@ -11,7 +14,7 @@ const List = ({ session, setSession, showError, setEdit }) => {
   const fetchNotes = async () => {
     setLoading(true)
     try {
-      const response = await fetch('https://apps.pruss.it/api/notes', { 'Cookie': session.cookies } )
+      const response = await getList({ session })
       const data = await response.json()
       setList(data)
     } catch(e) {
@@ -25,51 +28,6 @@ const List = ({ session, setSession, showError, setEdit }) => {
     fetchNotes()
   }, [])
 
-  const deleteNote = async (note) => {
-    setLoading(true)
-    try {
-      await fetch(`https://apps.pruss.it/api/notes/${note._id}`, {
-        method: 'DELETE', headers: { 'Cookie': session.cookies },
-      })
-      fetchNotes()
-    } catch(e) {
-      showError('Error while removing note')
-    } finally {
-      setLoading(false)
-    }
-  }
-
-  const deleteItem = note => {
-    Alert.alert(
-      'Are you sure?',
-      `Note "${note.title}" will be permanently removed`,
-      [
-        {
-          text: 'Remove',
-          onPress: () => deleteNote(note),
-          style: 'destructive',
-        },
-        {
-          text: 'Cancel',
-          onPress: () => {},
-          style: 'cancel',
-        },
-      ],
-    );
-  };
-
-  const removeBtn = (note) => {
-    return (
-      <View style={styles.buttonsContainer}>
-        <View style={styles.button}>
-          <Pressable onPress={() => deleteItem(note)}>
-            <Text style={styles.buttonText}>Delete</Text>
-          </Pressable>
-        </View>
-      </View>
-    );
-  };
-
   return (
     <>
       <Menu
@@ -90,8 +48,10 @@ const List = ({ session, setSession, showError, setEdit }) => {
               onRefresh={fetchNotes}
               refreshing={loading}
               maxSwipeDistance={80}
-              renderQuickActions={({item}) => removeBtn(item)}
               bounceFirstRowOnMount={false}
+              renderQuickActions={({item}) => RemoveNoteButton({
+                removeNote: () => handleRemove({ note: item, session, fetchNotes, setLoading, showError })
+              })}
             />
           )
       }
@@ -104,21 +64,6 @@ const styles = StyleSheet.create({
     color: 'white',
     padding: 20,
   },
-  button: {
-    width: 80,
-    alignItems: 'center',
-    justifyContent: 'center',
-    backgroundColor: 'red',
-  },
-  buttonText: {
-    fontWeight: 'bold',
-    color: 'white',
-  },
-  buttonsContainer: {
-    flex: 1,
-    flexDirection: 'row',
-    justifyContent: 'flex-end',
-  },
 });
 
 export default List;
-- 
cgit v1.2.3

