UpdatePage.js 7.32 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
import React, { useState, useEffect } from 'react';
Choi Ga Young's avatar
Choi Ga Young committed
2
import { View, StyleSheet, Keyboard, TouchableWithoutFeedback } from 'react-native';
Choi Ga Young's avatar
Choi Ga Young committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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';
import updateApi from './db/updatePage.api';

const INIT_ASSETSTYPE = {
  id: 1,
  value: '',
}

const INIT_CATEGORY = {
  id: 1,
  value: '',
}

const INIT_SUBCATEGORY = {
  id: 1,
  value: '',
  foreign_id: 0,
}


function updatePage({ navigation, route }) {
  const [selectedIndex, setSelectedIndex] = useState(route.params[1].type - 1)
  const [date, setDate] = useState(route.params[0])
  const [contents, setContents] = useState(route.params[1].contents)
  const [price, setPrice] = useState(`${route.params[1].price}`)
  const [asset_type, setAsset_type] = useState([])
  const [selected_asset_type, setSelected_asset_type] = useState(INIT_ASSETSTYPE)
  const [selected_deposit_asset_type, setSelected_deposit_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)

Soo Hyun Kim's avatar
Soo Hyun Kim committed
41
  const [disabled, setDisabled] = useState(true)
Choi Ga Young's avatar
Choi Ga Young committed
42
43
44
45
46
47
48
49
50
  const [success, setSuccess] = useState(false)

  useEffect(() => {
    loadCat()
    loadSubCat()
    loadAssetType()
    initData()
  }, [selectedIndex])

Soo Hyun Kim's avatar
Soo Hyun Kim committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  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])
Choi Ga Young's avatar
Choi Ga Young committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

  const initData = () => {
    setSelected_asset_type(INIT_ASSETSTYPE)
    setSelected_cat(INIT_CATEGORY)
    setSelected_subcat(INIT_SUBCATEGORY)
  }

  const loadCat = async () => {
    try {
      const catArray = await moneyApi.selectCategories(selectedIndex + 1)
      setCategories(catArray);
    } catch (error) {
      console.log('error in load categories ( postMoney.js )', error)
    }
  }

  const loadSubCat = async () => {
    try {
      const subCatArray = await moneyApi.selectSubCategories()
      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)
    }
  }

  const deleteData = async () => {
    try {
Choi Ga Young's avatar
Choi Ga Young committed
102
103
      if (selectedIndex === 2) {
        await updateApi.delData({ findId: [route.params[1].id, route.params[1].id + 1] })
Choi Ga Young's avatar
Choi Ga Young committed
104
105
106
107
108
109
110
111
112
113
114
115
      } else {
        await updateApi.delData({ findId: [route.params[1].id] })
      }
      initData()
      setSuccess(true)
    } catch (error) {
      console.log('error in deleteData', error)
    }
  }

  const changeData = async () => {
    try {
Choi Ga Young's avatar
Choi Ga Young committed
116
      if (selectedIndex === 2) {
Choi Ga Young's avatar
Choi Ga Young committed
117
        await updateApi.updateData({ findId: route.params[1].id }, [date, contents, -price, selected_cat.id, selected_subcat.id, selected_asset_type.id, selectedIndex + 1])
Choi Ga Young's avatar
Choi Ga Young committed
118
        await updateApi.updateData({ findId: route.params[1].id + 1 }, [date, contents, price, selected_cat.id, selected_subcat.id, selected_deposit_asset_type.id, selectedIndex + 1])
Choi Ga Young's avatar
Choi Ga Young committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
      } else {
        await updateApi.updateData({ findId: route.params[1].id }, [date, contents, price, selected_cat.id, selected_subcat.id, selected_asset_type.id, selectedIndex + 1])
      }
      setSuccess(true)
    } catch (error) {
      console.log('error in change Data', error)
    }
  }

  const onUpdateCatPress = () => {
    navigation.navigate('EditOption', selectedIndex + 1)
  }

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

  if (success) {
Choi Ga Young's avatar
Choi Ga Young committed
137
138
139
    setTimeout(() => {
      navigation.goBack()
    }, 500)
Choi Ga Young's avatar
Choi Ga Young committed
140
  }
Choi Ga Young's avatar
Choi Ga Young committed
141
  
Choi Ga Young's avatar
Choi Ga Young committed
142
  return (
Choi Ga Young's avatar
Choi Ga Young committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    <TouchableWithoutFeedback onPress={() => {
      Keyboard.dismiss();
    }} >
      <View style={{ flex: 1 }}>
        <View>
          {selectedIndex === 2 ? null : <ButtonsForm
            onPress={(index) => { setSelectedIndex(index); Keyboard.dismiss(); }}
            selectedIndex={selectedIndex}
            group={["수입", "지출"]} />}

          <DatePicker
            inputTitle="날짜"
            date={date}
            setDate={setDate}
          />
          <InputBox
            inputTitle="내용"
            value={contents}
            onChangeText={
              (contents) => setContents(contents)
            }
            maxLength={30}
          />
          <InputBox
            inputTitle="금액"
            value={price}
            onChangeText={
              (price) => setPrice(price)
            }
            keyboardType="numeric"
            maxLength={30}
          />
Choi Ga Young's avatar
Choi Ga Young committed
175
          <SelectForm
Choi Ga Young's avatar
Choi Ga Young committed
176
            inputTitle={selectedIndex === 2 ? "출금" : "자산"}
Choi Ga Young's avatar
Choi Ga Young committed
177
178
            placeholder="자산 선택"
            data={asset_type}
Choi Ga Young's avatar
Choi Ga Young committed
179
180
            selectedData={selected_asset_type}
            onValueChange={(asset) => setSelected_asset_type(asset)}
Choi Ga Young's avatar
Choi Ga Young committed
181
            onUpdateDataPress={onUpdateAssetPress}
Choi Ga Young's avatar
Choi Ga Young committed
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
          />
          {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={changeData}
            style={style.submitButton}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
209
            disabled={disabled}
Choi Ga Young's avatar
Choi Ga Young committed
210
211
212
213
214
215
216
217
218
219
220
221
          />
          <StyledButton
            name="삭제"
            onPress={deleteData}
            style={style.delButton}
          />
          <StyledButton
            name="취소"
            onPress={() => navigation.goBack()}
            style={style.cancelButton}
          />
        </View>
Choi Ga Young's avatar
Choi Ga Young committed
222
      </View>
Choi Ga Young's avatar
Choi Ga Young committed
223
    </TouchableWithoutFeedback>
Choi Ga Young's avatar
Choi Ga Young committed
224
  )
Choi Ga Young's avatar
Choi Ga Young committed
225

Choi Ga Young's avatar
Choi Ga Young committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
};

const style = StyleSheet.create({
  Font: {
    fontSize: 24
  },
  buttonRow: {
    flexDirection: 'row',
    alignItems: "center",
    marginHorizontal: 10,
    marginVertical: 3,
  },
  submitButton: {
    flex: 2,
    height: 50,
  },
  delButton: {
    flex: 1,
    height: 50,
  },
  cancelButton: {
    flex: 1,
    height: 50,
  }
});

export default updatePage;