DeptPage.js 5.08 KB
Newer Older
YoonDongMin's avatar
부채    
YoonDongMin committed
1
import React, { useState } from 'react';
YoonDongMin's avatar
DongM    
YoonDongMin committed
2
import { StyleSheet, Text, View, FlatList, TouchableOpacity, Modal, Alert, TouchableWithoutFeedback, Keyboard } from 'react-native';
YoonDongMin's avatar
부채    
YoonDongMin committed
3
4
import Header from './components/header';
import AddTodo from './components/addTodo';
YoonDongMin's avatar
DongM    
YoonDongMin committed
5
6
7
8
9
10
11
12
import TodoItem from './components/todoItem';
import InfoForm from './screens/infoForm';
import { globalStyles } from './styles/global';
import Ionicons from 'react-native-vector-icons/Ionicons';
import Infodetails from './screens/InfoDetails'
import ButtonsForm from './components/ButtonsForm';
import StyledButton from './components/StyledButton';
import { NavigationContainer } from '@react-navigation/native';
YoonDongMin's avatar
부채    
YoonDongMin committed
13
14


YoonDongMin's avatar
DongM    
YoonDongMin committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export default function DeptPage({ navigation }) {
  const [selectedIndex, setSelectedIndex] = useState(0)
  const [modallOpen, setModallOpen] = useState(false);
  const [todos, setTodos] = useState([
    { date: '7/10', person: '수빈이', money: '100만원', remaied_money: '10만원남음', key: '1' },
    { date: '7/10', person: '수현이', money: '100만원', remaied_money: '10만원남음', key: '2' },
    { date: '7/10', person: '가영이', money: '100만원', remaied_money: '10만원남음', key: '3' }
  ]);

  const addInfo = (info) => {
    info.key = Math.random().toString(); //앞에 key를 받아올수있도록 생성
    setTodos((currentInfos) => {
      return [info, ...currentInfos]; //새로운 정보와 지금까지 정보를 합친다
    });
    setModallOpen(false); //modal이 보여지지 않게
  }

  const pressHandler = (key) => {
    setTodos((prevTodos) => {
      return prevTodos.filter(todo => todo.key != key);
    });
  }
YoonDongMin's avatar
부채    
YoonDongMin committed
37
38


YoonDongMin's avatar
DongM    
YoonDongMin committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

  // const [modalOpen, setModalOpen] = useState(false);
  // //이 함수로 내용 추가를 함 Addtodo함수에서 이용
  // const submitHandler = (text) => {
  //   //alert를 사용하기위해 조건문 지정
  //   if (text.length > 3) {
  //     setTodos((prevTodos) => {
  //       return [
  //         { text: text, key: Math.random().toString() },
  //         ...prevTodos  //앞의 todos에 추가하여 내용을 더 추가함
  //       ];
  //     });
  //   } else { //3글자 보다 작으면 alert
  //     Alert.alert('OOPs!', '3글짜 이상!', [
  //       { text: 'Understood', onPress: () => console.log('alert closed') }//understood 버튼 누르면 alert꺼짐
  //     ]);
  //   }

  // }
  const insertData = async () => {
    try {
      if (selectedIndex === 0) { type = '빌려준금액' }
      else { type = '빌린금액' }

      (await db).transaction((tx) => {
        console.log("데이터 삽입하기");
        tx.executeSql('INSERT INTO Money (type, date, contents, price, asset_type, category) VALUES (?,?);',
          [expense, income],
          () => { console.log("삽입 성공"); },
          (error) => console.log(error))
      })
    } catch (error) {
      console.log('error in insert data', error)
YoonDongMin's avatar
부채    
YoonDongMin committed
72
    }
YoonDongMin's avatar
DongM    
YoonDongMin committed
73
74
75
  }


YoonDongMin's avatar
부채    
YoonDongMin committed
76

YoonDongMin's avatar
DongM    
YoonDongMin committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  return (



    <TouchableWithoutFeedback onPress={() => {
      Keyboard.dismiss();
    }}>
      <View Style={styles.container}>
        <Header />
        <ButtonsForm
          onPress={(index) => setSelectedIndex(index)}
          selectedIndex={selectedIndex}
          group={["빌려준금액", "빌린금액"]} />

        <View style={styles.list}>
          <FlatList
            data={todos}
            renderItem={({ item }) => (
              <TouchableOpacity onPress={() => navigation.navigate('InfoDetails', item)}>
                <TodoItem item={item} pressHandler={pressHandler} />
              </TouchableOpacity>
            )}
          />
          <View style={styles.content}>
            <Modal visible={modallOpen} animationType='slide'>
              <View style={style.modalContent}>
                <Ionicons
                  name='close'
                  size={24}
                  style={style.modalToggle}
                  onPress={() => setModallOpen(false)}
                />
                <InfoForm addInfo={addInfo} />

              </View>
            </Modal>
            <Ionicons
              name='add'
              size={24}
              style={{ ...style.modalToggle, ...style.modalClose }} //...은 중괄호를 풀어서 합치려고 이용함
              onPress={() => setModallOpen(true)}
            />
          </View>
        </View>
      </View>
    </TouchableWithoutFeedback>
  );
YoonDongMin's avatar
부채    
YoonDongMin committed
124
125
126
}


YoonDongMin's avatar
DongM    
YoonDongMin committed
127

YoonDongMin's avatar
부채    
YoonDongMin committed
128
129
//content는 내용이 추가되면 계속 커진다
const styles = StyleSheet.create({
YoonDongMin's avatar
DongM    
YoonDongMin committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  container: {
    flex: 1,
    backgroundColor: '#fff',
  },
  content: {
    padding: 40,
  },
  list: {
    marginTop: 20, //top에서부터 거리
  }
});

const style = StyleSheet.create({
  modalToggle: {
    marginBottom: 10,
    borderWidth: 1,
    borderColor: 'grey', //grey
    padding: 10,
    borderRadius: 10,
    alignSelf: 'center', //위치를 center로
  },
  modalClose: {
    alignSelf: 'center',
    alignItems: 'flex-end',
    marginTop: 130,
    marginBottom: 20,
  },
  modalContent: {
    flex: 1, //이후 유용한 키보드를 추가하려고 ex)dismissing keyboard
  },

  TextInput: {
    borderColor: 'skyblue',
    height: 40,
    margin: 12,
    borderWidth: 1
  },
  Font: {
    fontSize: 24
  }
YoonDongMin's avatar
부채    
YoonDongMin committed
170
});