import React, { useState, useEffect } from 'react'; 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'; import DatePicker from './components/DatePicker'; import moneyApi from './db/postMoney.api'; const getDate = () => { var date = new Date(); return (String(date.toJSON()).split(/T/)[0]) } const INIT_ASSETSTYPE = { id: 0, value: '', } const INIT_CATEGORY = { id: 0, value: '', } const INIT_SUBCATEGORY = { id: 0, value: '', foreign_id: 0, } const PostMoney = () => { const [selectedIndex, setSelectedIndex] = useState(0) const [date, setDate] = useState(getDate()) const [contents, setContents] = useState('') const [price, setPrice] = useState(0) const [asset_type, setAsset_type] = useState([]) const [selected_asset_type, setSelected_asset_type] = useState(INIT_ASSETSTYPE) const [categories, setCategories] = useState([]) const [selected_cat, setSelected_cat] = useState(INIT_CATEGORY) const [subcategories, setSubcategories] = useState([]) const [selected_subcat, setSelected_subcat] = useState(INIT_SUBCATEGORY) useEffect(() => { loadCat() loadSubCat() loadAssetType() initData() }, [selectedIndex]) const initData = () => { setDate(getDate()) setContents('') setPrice(0) setSelected_asset_type(INIT_ASSETSTYPE) setSelected_cat(INIT_CATEGORY) setSelected_subcat(INIT_SUBCATEGORY) } console.log('type: ', selectedIndex, '| date: ', date, '| contents: ', contents, '| price: ', price, '| selected_asset_type: ', selected_asset_type.id, '| selected_cat: ', selected_cat.id, '| selected_subcat: ', selected_subcat.id) const insertData = async () => { try { let type = selectedIndex+1; const result = await moneyApi.insertMoney([type, date, contents, price, selected_asset_type.id, selected_cat.id, selected_subcat.id]) console.log(result) } catch (error) { console.log('error in insert data', error) } } const loadCat = async () => { try { const catArray = await moneyApi.selectCategories(selectedIndex+1) console.log('catload', catArray) setCategories(catArray); } catch (error) { console.log('error in load categories ( postMoney.js )', error) } } 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) } } const loadAssetType = async () => { try { const assetsTypeArray = await moneyApi.selectAssetsType() setAsset_type(assetsTypeArray); } catch (error) { console.log('error in load assets type ( postMoney.js )', error) } } return ( setSelectedIndex(index)} selectedIndex={selectedIndex} group={["수입", "지출", "이동"]} /> setContents(contents) } maxLength={30} /> setPrice(price) } keyboardType="numeric" maxLength={30} /> setSelected_asset_type(asset)} /> setSelected_cat(cat)} subData={subcategories} selectedSubData={selected_subcat} onSubValueChange={(subcat) => setSelected_subcat(subcat)} /> console.log('취소버튼')} style={style.cancelButton} /> ) } 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;