PostMoney.js 4.55 KB
Newer Older
1
2
import React, { useState } from 'react';
import { View, Text, StyleSheet, Button } from 'react-native';
3
import { DEBUG, enablePromise, openDatabase } from 'react-native-sqlite-storage';
4
5
6
7
8
import InputBox from './components/InputBox';
import ButtonsForm from './components/ButtonsForm';
import SelectForm from './components/SelectForm';
import StyledButton from './components/StyledButton';

9
10
11
12
13
14
15
16
17
DEBUG(true);
enablePromise(true);

const db = openDatabase({
  name: 'MyMoney',
  location: 'default',
  createFromLocation: '~MyMoney.db', // android/src/main/assets/TestDB.db 파일을 위치 시킴
});

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const INIT_ASSET_TYPE = [
    {
        asset_id: '1',
        asset_name: '농협',
    },
    {
        asset_id: '2',
        asset_name: '신한',
    },
]

const INIT_CATEGORIES = [
    {
        category_id: '1',
        category_name: '식비',
    },
    {
        category_id: '2',
        category_name: '문화',
    },
    {
        category_id: '3',
        category_name: '교통비',
    },
    {
        category_id: '4',
        category_name: '기타',
    },
]

const PostMoney = () => {
    const [selectedIndex, setSelectedIndex] = useState(0)
50
    const [date, setDate] = useState('2021-07-09')
51
52
53
54
55
56
57
58
59
    const [contents, setContents] = useState('')
    const [price, setPrice] = useState(0)
    const [asset_type, setAsset_type] = useState(INIT_ASSET_TYPE)
    const [selected_asset_type, setSelected_asset_type] = useState(INIT_ASSET_TYPE[0])
    const [categories, setCategories] = useState(INIT_CATEGORIES)
    const [selected_cat, setSelected_cat] = useState(INIT_CATEGORIES[0])

    console.log('입력 데이터 : ', selectedIndex, contents, price, selected_asset_type, selected_cat)

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    const insertData = async () => {
        try {
            if (selectedIndex === 0) {type = '수입'}
            else if (selectedIndex === 1) {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)
        }
      }

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    return (
        <View>
            <View>
                <ButtonsForm
                    onPress={(index) => setSelectedIndex(index)}
                    selectedIndex={selectedIndex}
                    group={["수입", "지출", "이동"]} />
                <InputBox
                    inputTitle="내용"
                    placeholder="내용을 입력하세요"
                    onChangeText={
                        (contents) => setContents(contents)
                    }
                    maxLength={30}
                />
                <InputBox
                    inputTitle="금액"
                    placeholder="금액을 입력하세요"
                    onChangeText={
                        (price) => setPrice(price)
                    }
                    keyboardType="numeric"
                    maxLength={30}
                />
                <SelectForm
                    inputTitle="자산"
                    categories={asset_type}
                    selectedValue={selected_asset_type}
                    onValueChange={(itemValue, itemIndex) => setSelected_asset_type(itemValue)}
                />
                <SelectForm
                    inputTitle="구분"
                    categories={categories}
                    selectedValue={selected_cat}
                    onValueChange={(itemValue, itemIndex) => setSelected_cat(itemValue)}
                />
            </View>
            <View style={style.buttonRow}>
                <StyledButton
                    name="저장하기"
                    onPress={() => console.log('저장버튼')}
                    style={style.submitButton}
                />
                <StyledButton
                    name="취소"
                    onPress={() => console.log('취소버튼')}
                    style={style.cancelButton}
                />
            </View>
        </View>
    )
}

const style = StyleSheet.create({
    Font: {
        fontSize: 24
    },
    buttonRow: {
        flexDirection: 'row',
        alignItems: "center",
        marginHorizontal: 10,
        marginVertical: 3,
    },
    submitButton: {
        flex: 3,
        height: 50,
    },
    cancelButton: {
        flex: 1,
        height: 50,
    }
});

export default PostMoney;