PostMoney.js 7.31 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
const INIT_ASSETSTYPE = {
12
    id: 1,
13
14
15
16
    value: '',
}

const INIT_CATEGORY = {
17
    id: 1,
18
19
20
21
    value: '',
}

const INIT_SUBCATEGORY = {
22
    id: 1,
23
24
25
    value: '',
    foreign_id: 0,
}
26

27
const PostMoney = ({navigation}) => {
28
    const [selectedIndex, setSelectedIndex] = useState(0)
29
    const [date, setDate] = useState(getDate())
30
    const [contents, setContents] = useState('')
31
    const [price, setPrice] = useState('')
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
    
    const [success, setSuccess] = useState(false)
41

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

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

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

68
69
    const insertData = async () => {
        try {
70
            let type = selectedIndex + 1;
71
72
73
74
75
76
77
            if (type === 3) {
                await moneyApi.insertMoney([type, date, contents, -price, selected_asset_type.id, selected_cat.id, selected_subcat.id])
                await moneyApi.insertMoney([type, date, contents, price, selected_deposit_asset_type.id, selected_cat.id, selected_subcat.id])
            } else {
                await moneyApi.insertMoney([type, date, contents, price, selected_asset_type.id, selected_cat.id, selected_subcat.id])
            }
            setSuccess(true)
78
79
80
81
82
83
84
        } catch (error) {
            console.log('error in insert data', error)
        }
    }

    const loadCat = async () => {
        try {
85
            const catArray = await moneyApi.selectCategories(selectedIndex + 1)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
86
87
            console.log('catload', catArray)
            setCategories(catArray);
88
        } catch (error) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
89
            console.log('error in load categories ( postMoney.js )', error)
90
91
        }
    }
92
93
94
95
96
97
98
99
100
101
102

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

103
104
    const loadAssetType = async () => {
        try {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
105
106
            const assetsTypeArray = await moneyApi.selectAssetsType()
            setAsset_type(assetsTypeArray);
107
        } catch (error) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
108
            console.log('error in load assets type ( postMoney.js )', error)
109
        }
110
111
    }

112
113
114
115
116
117
118
119
    const onUpdateCatPress = () => {
        navigation.navigate('EditOption', selectedIndex+1)
    }

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

120
121
122
123
124
    if(success){
        initData()
        setSuccess(false)
    }

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