PostMoney.js 6.77 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
import { getDate } from './utils/dateFunction'
10

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const INIT_ASSETSTYPE = {
    id: 0,
    value: '',
}

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

const INIT_SUBCATEGORY = {
    id: 0,
    value: '',
    foreign_id: 0,
}
26

27
const PostMoney = ({navigation}) => {
28
    const [selectedIndex, setSelectedIndex] = useState(0)
29
    const [date, setDate] = useState(getDate())
30
31
    const [contents, setContents] = useState('')
    const [price, setPrice] = useState(0)
32
    const [asset_type, setAsset_type] = useState([])
33
    const [selected_asset_type, setSelected_asset_type] = useState(INIT_ASSETSTYPE)
34
    const [selected_deposit_asset_type, setSelected_deposit_asset_type] = useState(INIT_ASSETSTYPE)
35
    const [categories, setCategories] = useState([])
36
37
38
    const [selected_cat, setSelected_cat] = useState(INIT_CATEGORY)
    const [subcategories, setSubcategories] = useState([])
    const [selected_subcat, setSelected_subcat] = useState(INIT_SUBCATEGORY)
39

40
41
42
43
44
45
    useEffect(() => {
        loadCat()
        loadSubCat()
        loadAssetType()
        initData()
    }, [selectedIndex])
46

47
48
49
50
51
52
53
54
    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
55

56
57
58
59
60
61
62
63
64
    console.log(
        'type: ', selectedIndex,
        '| date: ', date,
        '| contents: ', contents,
        '| price: ', price,
        '| selected_asset_type: ', selected_asset_type.id,
        (selectedIndex === 2 ? { ['selected_deposit_asset_type']: selected_deposit_asset_type.id } : null),
        '| selected_cat: ', selected_cat.id,
        '| selected_subcat: ', selected_subcat.id)
65

66
67
    const insertData = async () => {
        try {
68
            let type = selectedIndex + 1;
69
            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
70
            console.log(result)
71
72
73
74
75
76
77
        } catch (error) {
            console.log('error in insert data', error)
        }
    }

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

    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)
        }
    }

96
97
    const loadAssetType = async () => {
        try {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
98
99
            const assetsTypeArray = await moneyApi.selectAssetsType()
            setAsset_type(assetsTypeArray);
100
        } catch (error) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
101
            console.log('error in load assets type ( postMoney.js )', error)
102
        }
103
104
    }

105
106
107
108
109
110
111
112
    const onUpdateCatPress = () => {
        navigation.navigate('EditOption', selectedIndex+1)
    }

    const onUpdateAssetPress = () => {
        navigation.navigate('EditOption', 0)
    }

113
114
115
116
117
118
119
    return (
        <View>
            <View>
                <ButtonsForm
                    onPress={(index) => setSelectedIndex(index)}
                    selectedIndex={selectedIndex}
                    group={["수입", "지출", "이동"]} />
120
121
122
123
124
                <DatePicker
                    inputTitle="날짜"
                    date={date}
                    setDate={setDate}
                />
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
                <InputBox
                    inputTitle="내용"
                    placeholder="내용을 입력하세요"
                    onChangeText={
                        (contents) => setContents(contents)
                    }
                    maxLength={30}
                />
                <InputBox
                    inputTitle="금액"
                    placeholder="금액을 입력하세요"
                    onChangeText={
                        (price) => setPrice(price)
                    }
                    keyboardType="numeric"
                    maxLength={30}
                />
                <SelectForm
143
                    inputTitle={selectedIndex === 2 ? "출금" : "자산"}
144
145
146
                    placeholder="자산 선택"
                    data={asset_type}
                    selectedData={selected_asset_type}
147
                    onValueChange={(asset) => setSelected_asset_type(asset)}
148
                    onUpdateDataPress={onUpdateAssetPress}
149
                />
150
151
152
153
154
155
156
157
158
                {selectedIndex === 2 &&
                    <SelectForm
                        inputTitle="입금"
                        placeholder="자산 선택"
                        data={asset_type}
                        selectedData={selected_deposit_asset_type}
                        onValueChange={(deposit_asset) => setSelected_deposit_asset_type(deposit_asset)}
                        onUpdateDataPress={onUpdateAssetPress}
                    />}
159
160
                <SelectForm
                    inputTitle="구분"
161
162
163
                    placeholder="카테고리 선택"
                    data={categories}
                    selectedData={selected_cat}
164
                    onValueChange={(cat) => setSelected_cat(cat)}
165
166
                    subData={subcategories}
                    selectedSubData={selected_subcat}
167
                    onSubValueChange={(subcat) => setSelected_subcat(subcat)}
168
                    onUpdateDataPress={onUpdateCatPress}
169
170
171
172
173
                />
            </View>
            <View style={style.buttonRow}>
                <StyledButton
                    name="저장하기"
174
                    onPress={insertData}
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
                    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;