PostMoney.js 7.88 KB
Newer Older
1
import React, { useState, useEffect } from 'react';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
2
import { View, StyleSheet, Keyboard, TouchableWithoutFeedback } 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

Soo Hyun Kim's avatar
Soo Hyun Kim committed
40
41
    
    const [disabled, setDisabled] = useState(true)
42
    const [success, setSuccess] = useState(false)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
43
    
44
45
46
47
48
49
    useEffect(() => {
        loadCat()
        loadSubCat()
        loadAssetType()
        initData()
    }, [selectedIndex])
50

Soo Hyun Kim's avatar
Soo Hyun Kim committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    useEffect(() => {
        let isProper = false
        if (selectedIndex === 2) {
            isProper = [contents, price, selected_asset_type.value, selected_deposit_asset_type.value, selected_cat.value].every( elem => elem !== '')
        } else {
            isProper = [contents, price, selected_asset_type.value, selected_cat.value].every( elem => elem !== '')
        }

        if (isProper) {
            setDisabled(false)
        } else {
            setDisabled(true)
        }

    }, [contents, price, selected_asset_type, selected_deposit_asset_type, selected_cat, selected_subcat])

67
68
69
70
71
    const initData = () => {
        setSelected_asset_type(INIT_ASSETSTYPE)
        setSelected_cat(INIT_CATEGORY)
        setSelected_subcat(INIT_SUBCATEGORY)
    }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
72

73
74
    const insertData = async () => {
        try {
75
            let type = selectedIndex + 1;
76
77
78
79
80
81
82
            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)
83
84
85
86
87
88
89
        } catch (error) {
            console.log('error in insert data', error)
        }
    }

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

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

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

115
    const onUpdateCatPress = () => {
116
        navigation.navigate('EditOption', selectedIndex + 1)
117
118
119
120
121
122
    }

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

123
    if (success) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
124
        navigation.goBack()
125
126
    }

127
    return (
Soo Hyun Kim's avatar
Soo Hyun Kim committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
        <TouchableWithoutFeedback onPress={() => {
            Keyboard.dismiss();
        }}>
            <View style={{ flex: 1 }}>
                <View>
                    <ButtonsForm
                        onPress={(index) => {setSelectedIndex(index); Keyboard.dismiss();}}
                        selectedIndex={selectedIndex}
                        group={["수입", "지출", "이동"]} />
                    <DatePicker
                        inputTitle="날짜"
                        date={date}
                        setDate={setDate}
                    />
                    <InputBox
                        inputTitle="내용"
                        placeholder="내용을 입력하세요"
                        value={contents}
                        onChangeText={
                            (contents) => setContents(contents)
                        }
                        maxLength={30}
                    />
                    <InputBox
                        inputTitle="금액"
                        placeholder="금액을 입력하세요"
                        value={price}
                        onChangeText={
                            (price) => setPrice(price)
                        }
                        keyboardType="numeric"
                        maxLength={30}
                    />
161
                    <SelectForm
Soo Hyun Kim's avatar
Soo Hyun Kim committed
162
                        inputTitle={selectedIndex === 2 ? "출금" : "자산"}
163
164
                        placeholder="자산 선택"
                        data={asset_type}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
165
166
                        selectedData={selected_asset_type}
                        onValueChange={(asset) => setSelected_asset_type(asset)}
167
                        onUpdateDataPress={onUpdateAssetPress}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
168
169
170
171
172
173
174
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
                    />
                    {selectedIndex === 2 &&
                        <SelectForm
                            inputTitle="입금"
                            placeholder="자산 선택"
                            data={asset_type}
                            selectedData={selected_deposit_asset_type}
                            onValueChange={(deposit_asset) => setSelected_deposit_asset_type(deposit_asset)}
                            onUpdateDataPress={onUpdateAssetPress}
                        />}
                    <SelectForm
                        inputTitle="구분"
                        placeholder="카테고리 선택"
                        data={categories}
                        selectedData={selected_cat}
                        onValueChange={(cat) => setSelected_cat(cat)}
                        subData={subcategories}
                        selectedSubData={selected_subcat}
                        onSubValueChange={(subcat) => setSelected_subcat(subcat)}
                        onUpdateDataPress={onUpdateCatPress}
                    />
                </View>
                <View style={style.buttonRow}>
                    <StyledButton
                        name="저장하기"
                        onPress={insertData}
                        style={style.submitButton}
                        disabled={disabled}
                    />
                    <StyledButton
                        name="취소"
                        onPress={() => navigation.goBack()}
                        style={style.cancelButton}
                    />
                </View>
203
            </View>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
204
        </TouchableWithoutFeedback>
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    )
}

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;