MemoPage.js 3.25 KB
Newer Older
YoonDongMin's avatar
YDm    
YoonDongMin committed
1
import React, { useEffect, useState } from 'react';
2
import { StyleSheet, View, FlatList, TouchableOpacity, Modal, TouchableWithoutFeedback, Keyboard } from 'react-native';
YoonDongMin's avatar
YDm    
YoonDongMin committed
3
import TodoItem from './components/TodoItem';
YoonDongMin's avatar
YdM    
YoonDongMin committed
4
5
import Ionicons from 'react-native-vector-icons/Ionicons';
import MemoForm from './screens/MemoForm';
YoonDongMin's avatar
YDm    
YoonDongMin committed
6
import memoApi from './db/memoPage.api';
YoonDongMin's avatar
YdM    
YoonDongMin committed
7
8

function MemoPage({ navigation }) {
9
    const [memos, setMemos] = useState([]);
YoonDongMin's avatar
YdM    
YoonDongMin committed
10
11
    const [modalOpen, setModalOpen] = useState(false);

YoonDongMin's avatar
YDm    
YoonDongMin committed
12
13
14
15
16
17
18
    const loadMemos = async () => {
        try {
            const memoArray = await memoApi.selectMemo()
            setMemos(memoArray);
        } catch (error) {
            console.log('error in load memos ( MemoPage.js )', error)
        }
YoonDongMin's avatar
YdM    
YoonDongMin committed
19
20
    }

YoonDongMin's avatar
YDm    
YoonDongMin committed
21
22
23
24
    const addInfo = async (info) => {
        await memoApi.insertMemo(info)
        setModalOpen(false);
        loadMemos();
YoonDongMin's avatar
YdM    
YoonDongMin committed
25
    }
YoonDongMin's avatar
YDm    
YoonDongMin committed
26
27
28
29
30
31
32
33
34

    const onDeleteHandle = async (id) => {
        await memoApi.deleteMemo(id)
        loadMemos();
    }

    useEffect(() => {
        loadMemos()
    }, [])
YoonDongMin's avatar
YdM    
YoonDongMin committed
35
36
37
38
39

    return (
        <TouchableWithoutFeedback onPress={() => {
            Keyboard.dismiss();
        }}>
Choi Ga Young's avatar
Choi Ga Young committed
40
            <View Style={style.container}> 
YoonDongMin's avatar
YDm    
YoonDongMin committed
41
42
43
44
45
46
47
48
49
50
51
52
                <View style={{ width: '100%', height: '95%' }} >
                    <View style={{ width: '100%', height: '80%' }}>
                        <View >
                            <FlatList
                                data={memos}
                                renderItem={({ item }) => (
                                    <TouchableOpacity onPress={() => navigation.navigate('MemoDetails', {item : item, loadMemos : loadMemos})}>
                                        <TodoItem item={item} onDeleteHandle={() => onDeleteHandle(item.id)} />
                                    </TouchableOpacity>
                                )}
                            />
                        </View>
YoonDongMin's avatar
YdM    
YoonDongMin committed
53
54
55
56
                    </View>
                    <Ionicons
                        name='add'
                        size={24}
57
                        style={style.modalToggle}
YoonDongMin's avatar
YdM    
YoonDongMin committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
                        onPress={() => setModalOpen(true)}
                    />
                </View>
                <View>
                    <Modal visible={modalOpen} animationType='slide'>
                        <View style={style.modalContent}>
                            <Ionicons
                                name='close'
                                size={24}
                                style={style.modalToggle}
                                onPress={() => setModalOpen(false)}
                            />
                            <MemoForm addInfo={addInfo} />
                        </View>
                    </Modal>
                </View>
            </View>
        </TouchableWithoutFeedback >
    );
}

const style = StyleSheet.create({
    container: {
        flex: 1,
        width: '100%',
    },
    modalToggle: {
        borderWidth: 1,
Choi Ga Young's avatar
Choi Ga Young committed
86
        borderColor: 'gray',
YoonDongMin's avatar
YdM    
YoonDongMin committed
87
88
        padding: 5,
        borderRadius: 10,
Choi Ga Young's avatar
Choi Ga Young committed
89
        alignSelf: 'center',
YoonDongMin's avatar
YdM    
YoonDongMin committed
90
91
92
    },

    modalContent: {
93
        flex: 1,
YoonDongMin's avatar
YdM    
YoonDongMin committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    },

    TextInput: {
        borderColor: 'skyblue',
        height: 40,
        margin: 12,
        borderWidth: 1
    },
    Font: {
        fontSize: 24
    }
});

export default MemoPage;