PostMoney.js 5.76 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

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

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const INIT_ASSETSTYPE = {
    id: 0,
    value: '',
}

const INIT_CATEGORY = {
    id: 0,
    value: '',
}

const INIT_SUBCATEGORY = {
    id: 0,
    value: '',
    foreign_id: 0,
}
30
31
32

const PostMoney = () => {
    const [selectedIndex, setSelectedIndex] = useState(0)
33
    const [date, setDate] = useState(getDate())
34
35
    const [contents, setContents] = useState('')
    const [price, setPrice] = useState(0)
36
    const [asset_type, setAsset_type] = useState([])
37
    const [selected_asset_type, setSelected_asset_type] = useState(INIT_ASSETSTYPE)
38
    const [categories, setCategories] = useState([])
39
40
41
    const [selected_cat, setSelected_cat] = useState(INIT_CATEGORY)
    const [subcategories, setSubcategories] = useState([])
    const [selected_subcat, setSelected_subcat] = useState(INIT_SUBCATEGORY)
42

43
44
45
46
47
48
    useEffect(() => {
        loadCat()
        loadSubCat()
        loadAssetType()
        initData()
    }, [selectedIndex])
49

50
51
52
53
54
55
56
57
    const initData = () => {
        setDate(getDate())
        setContents('')
        setPrice(0)
        setSelected_asset_type(INIT_ASSETSTYPE)
        setSelected_cat(INIT_CATEGORY)
        setSelected_subcat(INIT_SUBCATEGORY)
    }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
58

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

61
62
63
64
    const insertData = async () => {
        try {
            let type = selectedIndex+1;
            const result = await moneyApi.insertMoney([type, date, contents, price, selected_asset_type.id, selected_cat.id, selected_subcat.id])
Soo Hyun Kim's avatar
Soo Hyun Kim committed
65
            console.log(result)
66
67
68
69
70
71
72
        } catch (error) {
            console.log('error in insert data', error)
        }
    }

    const loadCat = async () => {
        try {
73
            const catArray = await moneyApi.selectCategories(selectedIndex+1)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
74
75
            console.log('catload', catArray)
            setCategories(catArray);
76
        } catch (error) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
77
            console.log('error in load categories ( postMoney.js )', error)
78
79
        }
    }
80
81
82
83
84
85
86
87
88
89
90

    const loadSubCat = async () => {
        try {
            const subCatArray = await moneyApi.selectSubCategories()
            console.log('catload', subCatArray)
            setSubcategories(subCatArray);
        } catch (error) {
            console.log('error in load categories ( postMoney.js )', error)
        }
    }

91
92
    const loadAssetType = async () => {
        try {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
93
94
            const assetsTypeArray = await moneyApi.selectAssetsType()
            setAsset_type(assetsTypeArray);
95
        } catch (error) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
96
            console.log('error in load assets type ( postMoney.js )', error)
97
        }
98
99
    }

100
101
102
103
104
105
106
    return (
        <View>
            <View>
                <ButtonsForm
                    onPress={(index) => setSelectedIndex(index)}
                    selectedIndex={selectedIndex}
                    group={["수입", "지출", "이동"]} />
107
108
109
110
111
                <DatePicker
                    inputTitle="날짜"
                    date={date}
                    setDate={setDate}
                />
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
                <InputBox
                    inputTitle="내용"
                    placeholder="내용을 입력하세요"
                    onChangeText={
                        (contents) => setContents(contents)
                    }
                    maxLength={30}
                />
                <InputBox
                    inputTitle="금액"
                    placeholder="금액을 입력하세요"
                    onChangeText={
                        (price) => setPrice(price)
                    }
                    keyboardType="numeric"
                    maxLength={30}
                />
                <SelectForm
                    inputTitle="자산"
131
132
133
                    placeholder="자산 선택"
                    data={asset_type}
                    selectedData={selected_asset_type}
134
                    onValueChange={(asset) => setSelected_asset_type(asset)}
135
136
137
                />
                <SelectForm
                    inputTitle="구분"
138
139
140
                    placeholder="카테고리 선택"
                    data={categories}
                    selectedData={selected_cat}
141
                    onValueChange={(cat) => setSelected_cat(cat)}
142
143
                    subData={subcategories}
                    selectedSubData={selected_subcat}
144
                    onSubValueChange={(subcat) => setSelected_subcat(subcat)}
145
146
147
148
149
                />
            </View>
            <View style={style.buttonRow}>
                <StyledButton
                    name="저장하기"
150
                    onPress={insertData}
151
152
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
                    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;