PostMoney.js 6.8 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
    const insertData = async () => {
        try {
60
            let type = selectedIndex + 1;
61
62
63
64
65
66
67
            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)
68
69
70
71
72
73
74
        } catch (error) {
            console.log('error in insert data', error)
        }
    }

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

    const loadSubCat = async () => {
        try {
            const subCatArray = await moneyApi.selectSubCategories()
            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
    const onUpdateCatPress = () => {
101
        navigation.navigate('EditOption', selectedIndex + 1)
102
103
104
105
106
107
    }

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

108
    if (success) {
109
110
111
112
        initData()
        setSuccess(false)
    }

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
                <InputBox
                    inputTitle="내용"
                    placeholder="내용을 입력하세요"
128
                    value={contents}
129
130
131
132
133
134
135
136
                    onChangeText={
                        (contents) => setContents(contents)
                    }
                    maxLength={30}
                />
                <InputBox
                    inputTitle="금액"
                    placeholder="금액을 입력하세요"
137
                    value={price}
138
139
140
141
142
143
144
                    onChangeText={
                        (price) => setPrice(price)
                    }
                    keyboardType="numeric"
                    maxLength={30}
                />
                <SelectForm
145
                    inputTitle={selectedIndex === 2 ? "출금" : "자산"}
146
147
148
                    placeholder="자산 선택"
                    data={asset_type}
                    selectedData={selected_asset_type}
149
                    onValueChange={(asset) => setSelected_asset_type(asset)}
150
                    onUpdateDataPress={onUpdateAssetPress}
151
                />
152
153
154
155
156
157
158
159
160
                {selectedIndex === 2 &&
                    <SelectForm
                        inputTitle="입금"
                        placeholder="자산 선택"
                        data={asset_type}
                        selectedData={selected_deposit_asset_type}
                        onValueChange={(deposit_asset) => setSelected_deposit_asset_type(deposit_asset)}
                        onUpdateDataPress={onUpdateAssetPress}
                    />}
161
162
                <SelectForm
                    inputTitle="구분"
163
164
165
                    placeholder="카테고리 선택"
                    data={categories}
                    selectedData={selected_cat}
166
                    onValueChange={(cat) => setSelected_cat(cat)}
167
168
                    subData={subcategories}
                    selectedSubData={selected_subcat}
169
                    onSubValueChange={(subcat) => setSelected_subcat(subcat)}
170
                    onUpdateDataPress={onUpdateCatPress}
171
172
173
174
175
                />
            </View>
            <View style={style.buttonRow}>
                <StyledButton
                    name="저장하기"
176
                    onPress={insertData}
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
208
209
                    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;