PostMoney.js 5.54 KB
Newer Older
1
import React, { useState, useEffect } from 'react';
2
3
4
5
6
import { View, Text, StyleSheet, Button } from 'react-native';
import InputBox from './components/InputBox';
import ButtonsForm from './components/ButtonsForm';
import SelectForm from './components/SelectForm';
import StyledButton from './components/StyledButton';
7
import DatePicker from './components/DatePicker';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
8
import moneyApi from './db/postMoney.api';
9
10
11
12
13
14
15

const getDate = () => {
    var date = new Date();
    return (String(date.toJSON()).split(/T/)[0])
}

const INIT_SUBCATEGORIES = [
16
    {
17
18
19
        id: 1,
        value: '간식',
        foreign_id: 1
20
21
    },
    {
22
23
24
        id: 2,
        value: '외식',
        foreign_id: 1
25
26
    },
    {
27
28
29
        id: 3,
        value: '배달',
        foreign_id: 1
30
31
    },
    {
32
33
34
        id: 4,
        value: '택시',
        foreign_id: 2
35
36
    },
    {
37
38
39
        id: 5,
        value: '영화',
        foreign_id: 3
40
41
    },
    {
42
43
44
        id: 6,
        value: '뮤지컬',
        foreign_id: 3
45
46
47
48
49
    },
]

const PostMoney = () => {
    const [selectedIndex, setSelectedIndex] = useState(0)
50
    const [date, setDate] = useState(getDate())
51
52
    const [contents, setContents] = useState('')
    const [price, setPrice] = useState(0)
53
54
55
56
57
58
    const [asset_type, setAsset_type] = useState([])
    const [selected_asset_type, setSelected_asset_type] = useState(0)
    const [categories, setCategories] = useState([])
    const [selected_cat, setSelected_cat] = useState(0)
    const [subcategories, setSubcategories] = useState(INIT_SUBCATEGORIES)
    const [selected_subcat, setSelected_subcat] = useState(0)
59

60
    console.log('type: ', selectedIndex, '| date: ', date, '| contents: ', contents, '| price: ', price, '| selected_asset_type: ', selected_asset_type, '| selected_cat: ', selected_cat, '| selected_subcat: ', selected_subcat)
61

62
63
    const insertData = async () => {
        try {
64
            let type = ''
Soo Hyun Kim's avatar
Soo Hyun Kim committed
65

66
67
68
69
            if (selectedIndex === 0) { type = '수입' }
            else if (selectedIndex === 1) { type = '지출' }
            else { type = '이동' }

Soo Hyun Kim's avatar
Soo Hyun Kim committed
70
71
            const result = await moneyApi.insertMoney([type, date, contents, price, selected_asset_type, selected_cat, selected_subcat])
            console.log(result)
72
73
74
75
76
77
78
        } catch (error) {
            console.log('error in insert data', error)
        }
    }

    const loadCat = async () => {
        try {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
79
80
81
            const catArray = await moneyApi.selectCategories()
            console.log('catload', catArray)
            setCategories(catArray);
82
        } catch (error) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
83
            console.log('error in load categories ( postMoney.js )', error)
84
85
        }
    }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
86
    
87
88
    const loadAssetType = async () => {
        try {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
89
90
            const assetsTypeArray = await moneyApi.selectAssetsType()
            setAsset_type(assetsTypeArray);
91
        } catch (error) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
92
            console.log('error in load assets type ( postMoney.js )', error)
93
        }
94
95
    }

Soo Hyun Kim's avatar
Soo Hyun Kim committed
96

97
98
99
100
    useEffect(() => {
        loadCat()
        loadAssetType()
    }, [])
101

102
103
104
105
106
107
108
    return (
        <View>
            <View>
                <ButtonsForm
                    onPress={(index) => setSelectedIndex(index)}
                    selectedIndex={selectedIndex}
                    group={["수입", "지출", "이동"]} />
109
110
111
112
113
                <DatePicker
                    inputTitle="날짜"
                    date={date}
                    setDate={setDate}
                />
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
                <InputBox
                    inputTitle="내용"
                    placeholder="내용을 입력하세요"
                    onChangeText={
                        (contents) => setContents(contents)
                    }
                    maxLength={30}
                />
                <InputBox
                    inputTitle="금액"
                    placeholder="금액을 입력하세요"
                    onChangeText={
                        (price) => setPrice(price)
                    }
                    keyboardType="numeric"
                    maxLength={30}
                />
                <SelectForm
                    inputTitle="자산"
133
134
135
136
                    placeholder="자산 선택"
                    data={asset_type}
                    selectedData={selected_asset_type}
                    onValueChange={(assetId) => setSelected_asset_type(assetId)}
137
138
139
                />
                <SelectForm
                    inputTitle="구분"
140
141
142
143
144
145
146
                    placeholder="카테고리 선택"
                    data={categories}
                    selectedData={selected_cat}
                    onValueChange={(catId) => setSelected_cat(catId)}
                    subData={subcategories}
                    selectedSubData={selected_subcat}
                    onSubValueChange={(subcatId) => setSelected_subcat(subcatId)}
147
148
149
150
151
                />
            </View>
            <View style={style.buttonRow}>
                <StyledButton
                    name="저장하기"
152
                    onPress={insertData}
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
                    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;