PostMoney.js 7.21 KB
Newer Older
1
import React, { useState, useEffect } from 'react';
Choi Ga Young's avatar
Choi Ga Young committed
2
import { View, StyleSheet} from 'react-native';
3
4
5
6
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 { getDateStr } 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(getDateStr())
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
    const initData = () => {
Choi Ga Young's avatar
Choi Ga Young committed
50
        setDate(getDateStr())
51
        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
            setCategories(catArray);
87
        } catch (error) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
88
            console.log('error in load categories ( postMoney.js )', error)
89
90
        }
    }
91
92
93
94
95
96
97
98
99
100

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

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

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

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

118
    if (success) {
119
120
121
122
        initData()
        setSuccess(false)
    }

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