DeptPage.js 4.05 KB
Newer Older
YoonDongMin's avatar
YDm    
YoonDongMin committed
1
import React, { useState, useEffect } from 'react';
Choi Ga Young's avatar
Choi Ga Young committed
2
import { StyleSheet, View, FlatList, TouchableOpacity, Modal, TouchableWithoutFeedback, Keyboard } from 'react-native';
Choi Ga Young's avatar
Choi Ga Young committed
3
import TodoItem from './components/TodoItem';
YoonDongMin's avatar
DongM    
YoonDongMin committed
4
import Ionicons from 'react-native-vector-icons/Ionicons';
Choi Ga Young's avatar
Choi Ga Young committed
5
import ButtonsForm from './components/ButtonsForm'; 
6
import DeptForm from './screens/DeptForm';
YoonDongMin's avatar
YdM    
YoonDongMin committed
7
import { TabView } from 'react-native-elements';
YoonDongMin's avatar
YDm    
YoonDongMin committed
8
import deptApi from './db/deptPage.api';
YoonDongMin's avatar
부채    
YoonDongMin committed
9

YoonDongMin's avatar
YdM    
YoonDongMin committed
10
function DeptPage({ navigation }) {
Choi Ga Young's avatar
Choi Ga Young committed
11
12
  const [lend, setLend] = useState([]) 
  const [dept, setDept] = useState([]) 
YoonDongMin's avatar
DongM    
YoonDongMin committed
13
14
  const [selectedIndex, setSelectedIndex] = useState(0)
  const [modallOpen, setModallOpen] = useState(false);
YoonDongMin's avatar
YDm    
YoonDongMin committed
15

Choi Ga Young's avatar
Choi Ga Young committed
16
  const loadLend = async () => {
YoonDongMin's avatar
YDm    
YoonDongMin committed
17
18
19
20
21
22
23
24
25
    try {
      const deptArray = await deptApi.selectLoan("빌려준금액")

      setLend(deptArray);
    } catch (error) {
      console.log('error in load lend ( DeptPage.js )', error)
    }
  }

Choi Ga Young's avatar
Choi Ga Young committed
26
  const loadDept = async () => { 
YoonDongMin's avatar
YDm    
YoonDongMin committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    try {
      const deptArray = await deptApi.selectLoan("빌린금액")
      setDept(deptArray);
    } catch (error) {
      console.log('error in load lend ( DeptPage.js )', error)
    }
  }

  const loadLoan = async () => {
    loadLend()
    loadDept()
  }

  const addInfo = async (info) => {
    if (selectedIndex) {
      info['loan'] = "빌린금액"
    } else {
      info['loan'] = "빌려준금액"
    }
    await deptApi.insertDept(info)
Choi Ga Young's avatar
Choi Ga Young committed
47
    setModallOpen(false); 
YoonDongMin's avatar
YDm    
YoonDongMin committed
48
    loadLoan()
YoonDongMin's avatar
DongM    
YoonDongMin committed
49
50
  }

YoonDongMin's avatar
YDm    
YoonDongMin committed
51
52
53
  const onDeleteHandle = async (id) => {
    await deptApi.deleteDept(id)
    loadLoan()
YoonDongMin's avatar
DongM    
YoonDongMin committed
54
  }
YoonDongMin's avatar
부채    
YoonDongMin committed
55

Choi Ga Young's avatar
Choi Ga Young committed
56
  useEffect(() => { 
YoonDongMin's avatar
YDm    
YoonDongMin committed
57
58
59
    loadLoan()
  }, [])

YoonDongMin's avatar
DongM    
YoonDongMin committed
60
61
62
63
  return (
    <TouchableWithoutFeedback onPress={() => {
      Keyboard.dismiss();
    }}>
YoonDongMin's avatar
YdM    
YoonDongMin committed
64
      <View Style={style.container}>
YoonDongMin's avatar
DongM    
YoonDongMin committed
65
        <ButtonsForm
YoonDongMin's avatar
YDm    
YoonDongMin committed
66
67
68
          onPress={(index) => {
            setSelectedIndex(index)
          }}
YoonDongMin's avatar
DongM    
YoonDongMin committed
69
70
71
          selectedIndex={selectedIndex}
          group={["빌려준금액", "빌린금액"]} />

YoonDongMin's avatar
YdM    
YoonDongMin committed
72
73
74
75
76
        <View>
          <View style={{ width: '100%', height: '75%' }}>
            <TabView value={selectedIndex} onChange={setSelectedIndex} >
              <TabView.Item style={{ width: '100%', height: '100%' }}>
                <FlatList
YoonDongMin's avatar
YDm    
YoonDongMin committed
77
                  data={lend}
YoonDongMin's avatar
YdM    
YoonDongMin committed
78
                  renderItem={({ item }) => (
YoonDongMin's avatar
YDm    
YoonDongMin committed
79
80
                    <TouchableOpacity onPress={() => navigation.navigate('DeptDetails', { item: item, loadLoan: loadLend })}>
                      <TodoItem item={item} onDeleteHandle={() => onDeleteHandle(item.id)} />
YoonDongMin's avatar
YdM    
YoonDongMin committed
81
82
83
84
85
86
                    </TouchableOpacity>
                  )}
                />
              </TabView.Item>
              <TabView.Item style={{ width: '100%', height: '100%' }}>
                <FlatList
YoonDongMin's avatar
YDm    
YoonDongMin committed
87
                  data={dept}
YoonDongMin's avatar
YdM    
YoonDongMin committed
88
                  renderItem={({ item }) => (
YoonDongMin's avatar
YDm    
YoonDongMin committed
89
90
                    <TouchableOpacity onPress={() => navigation.navigate('DeptDetails', { item: item, loadLoan: loadDept })}>
                      <TodoItem item={item} onDeleteHandle={() => onDeleteHandle(item.id)} />
YoonDongMin's avatar
YdM    
YoonDongMin committed
91
92
                    </TouchableOpacity>
                  )}
YoonDongMin's avatar
DongM    
YoonDongMin committed
93
                />
YoonDongMin's avatar
YdM    
YoonDongMin committed
94
95
              </TabView.Item>
            </TabView>
YoonDongMin's avatar
DongM    
YoonDongMin committed
96
          </View>
YoonDongMin's avatar
YdM    
YoonDongMin committed
97
98
99
          <Ionicons
            name='add'
            size={24}
100
            style={style.modalToggle} 
YoonDongMin's avatar
YdM    
YoonDongMin committed
101
102
103
104
105
106
107
108
109
110
111
112
            onPress={() => setModallOpen(true)}
          />
        </View>
        <View>
          <Modal visible={modallOpen} animationType='slide'>
            <View style={style.modalContent}>
              <Ionicons
                name='close'
                size={24}
                style={style.modalToggle}
                onPress={() => setModallOpen(false)}
              />
YoonDongMin's avatar
YDm    
YoonDongMin committed
113
              <DeptForm addInfo={addInfo} />
YoonDongMin's avatar
YdM    
YoonDongMin committed
114
115
            </View>
          </Modal>
YoonDongMin's avatar
DongM    
YoonDongMin committed
116
        </View>
YoonDongMin's avatar
YDm    
YoonDongMin committed
117
      </View>
YoonDongMin's avatar
YdM    
YoonDongMin committed
118
    </TouchableWithoutFeedback >
YoonDongMin's avatar
DongM    
YoonDongMin committed
119
  );
YoonDongMin's avatar
부채    
YoonDongMin committed
120
121
}

YoonDongMin's avatar
YdM    
YoonDongMin committed
122
const style = StyleSheet.create({
YoonDongMin's avatar
YDm    
YoonDongMin committed
123
124
125
126
  Font: {
    fontFamily: 'GowunDodum-Regular'
  },

YoonDongMin's avatar
DongM    
YoonDongMin committed
127
  container: {
YoonDongMin's avatar
YDm    
YoonDongMin committed
128
    fontFamily: 'GowunDodum-Regular',
YoonDongMin's avatar
DongM    
YoonDongMin committed
129
    flex: 1,
YoonDongMin's avatar
YdM    
YoonDongMin committed
130
    width: '100%',
YoonDongMin's avatar
DongM    
YoonDongMin committed
131
132
133
  },
  modalToggle: {
    borderWidth: 1,
Choi Ga Young's avatar
Choi Ga Young committed
134
    borderColor: 'gray', 
YoonDongMin's avatar
YdM    
YoonDongMin committed
135
    padding: 5,
YoonDongMin's avatar
DongM    
YoonDongMin committed
136
    borderRadius: 10,
Choi Ga Young's avatar
Choi Ga Young committed
137
    alignSelf: 'center', 
YoonDongMin's avatar
DongM    
YoonDongMin committed
138
  },
YoonDongMin's avatar
YdM    
YoonDongMin committed
139

YoonDongMin's avatar
DongM    
YoonDongMin committed
140
  modalContent: {
Choi Ga Young's avatar
Choi Ga Young committed
141
    flex: 1, 
YoonDongMin's avatar
DongM    
YoonDongMin committed
142
  }
YoonDongMin's avatar
YDm    
YoonDongMin committed
143

YoonDongMin's avatar
YdM    
YoonDongMin committed
144
145
146
});

export default DeptPage;