EditOption.js 8.13 KB
Newer Older
1
2
3
4
5
6
import React, { useEffect, useState } from 'react';
import { View, Text, StyleSheet, FlatList, Modal, Pressable } from 'react-native';
import editApi from './db/editOption.api';
import AntDesign from 'react-native-vector-icons/AntDesign';
import InputBox from './components/InputBox';
import StyledButton from './components/StyledButton';
7
import Accordion, { AccordionItem } from './components/Accordion';
8
9

const INIT_OPTION = { id: 0, value: '' }
10
const INIT_SUBOPTION = { id: 0, value: '', foreign_id: 0 }
11
12
13

const EditOption = ({ route }) => {
  console.log('catEdit: type_id ', route.params)
14
  const type = route.params ? 'category' : 'asset'
15
  const type_id = route.params
16

17
18
19
20
21
22
23
24
25
26
  const [options, setOptions] = useState([])
  const [option, setOption] = useState(INIT_OPTION)

  const [modalOpen, setModalOpen] = useState(false)
  const modalClose = () => { setModalOpen(false); setOption(INIT_OPTION) }

  const [error, setError] = useState("");
  const [loading, setLoading] = useState(false);

  useEffect(() => {
27
    loadOptions()
28
29
  }, [])

30
  const loadOptions = async () => {
31
    try {
32
33
34
35
36
37
38
      let optionArray = []
      if (type === 'asset') {
        optionArray = await editApi.selectAssetsType()
      } else if (type === 'category') {
        optionArray = await editApi.selectCategories(type_id)
      }
      setOptions(optionArray)
39
40
41
42
43
    } catch (error) {

    }
  }

44
  const handleUpdate = async () => {
45
    try {
46
47
48
49
50
51
52
53
54
55
56
      if (type === 'asset') {
        const res = await editApi.updateOption('assets_type', { id: option.id, name: 'assets', value: option.value })
        console.log(res)
      } else if (type === 'category') {
        if (option.foreign_id && option.foreign_id > 0) {
          const res = await editApi.updateOption('subcategories', { id: option.id, name: 'subcategory', value: option.value })
          return console.log(res)
        }
        const res = await editApi.updateOption('categories', { id: option.id, name: 'category', value: option.value })
        console.log(res)
      }
57
    } catch (error) {
58
59
60
      
    } finally {
      loadOptions()
61
62
63
64
      modalClose()
    }
  }

65
  const handleDelete = async (item) => {
66
    try {
67
68
69
70
71
72
73
74
75
76
77
      if (type === 'asset') {
        const res = await editApi.deleteOption('assets_type', { id: item.id, name: 'assets' })
        console.log(res)
      } else if (type === 'category') {
        if (item.foreign_id && item.foreign_id > 0) {
          const res = await editApi.deleteOption('subcategories', { id: item.id, name: 'subcategory'})
          return console.log(res)
        }
        const res = await editApi.deleteOption('categories', { id: item.id, name: 'category' })
        console.log(res)
      }
78
    } catch (error) {
79
80
81
      
    } finally {
      loadOptions()
82
83
84
    }
  }

85
  const handleAdd = async () => {
86
    try {
87
88
89
90
91
92
93
94
95
96
97
      if (type === 'asset') {
        const res = await editApi.addOption('assets_type', { name: 'assets', value: option.value })
        console.log(res)
      } else if (type === 'category') {
        if (option.foreign_id && option.foreign_id > 0) {
          const res = await editApi.addOption('subcategories', { name: 'subcategory', value: option.value, foreign_name: 'category', foreign_id: option.foreign_id })
          return console.log(res)
        }
        const res = await editApi.addOption('categories', { name: 'category', value: option.value, foreign_name: 'type', foreign_id: type_id })
        console.log(res)
      }
98
    } catch (error) {
99
100
101
      
    } finally {
      loadOptions()
102
103
104
105
      modalClose()
    }
  }

106
  const renderAssetItem = ({ item }) => (
107
108
109
110
111
112
113
    <View style={[style.flexRow, style.catBox]}>
      <View style={style.flexRow}>
        {item.deletable && <AntDesign name='minuscircle' style={style.cancelIcon} onPress={() => { handleDelete(item) }} />}
        <Text style={style.optionText} >
          {item.value}
        </Text>
      </View>
114
      {item.deletable && <AntDesign name='edit' style={style.icon} onPress={() => { setOption(item); setModalOpen(true) }} />}
115
116
117
    </View>
  );

118
119
120
121
122
123
124
125
126
127
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
  const renderCatItem = ({ item }) => (
    <Accordion
      title={item.value}
      left={item.deletable && <AntDesign name='closecircle' style={style.cancelIcon} onPress={() => { handleDelete(item) }} />}
      right={item.deletable && <AntDesign name='edit' style={style.icon} onPress={() => { setOption(item); setModalOpen(true) }} />}
      titleStyle={style.optionText}
      backgroundColor='lightgray'
    >
      {item.subOptions.length !== 0 &&
        <FlatList
          data={item.subOptions}
          renderItem={({ item }) => (
            <AccordionItem
              title={item.value}
              left={<AntDesign name='closecircle' style={style.cancelIcon} onPress={() => { handleDelete(item) }} />}
              right={<AntDesign name='edit' style={style.icon} onPress={() => { setOption(item); setModalOpen(true) }} />}
              titleStyle={style.optionText}
              backgroundColor='#b0b0b0'
              marginLeft={30}
            />
          )}
          keyExtractor={item => item.id.toString()}
        />
      }
      <Pressable
        style={[style.flexRow, { backgroundColor: "#b0b0b0", paddingVertical: 10 }]}
        onPress={() => {
          setOption({...INIT_SUBOPTION, ['foreign_id']: item.id});
          setModalOpen(true)
        }}
      >
        <AntDesign name='plus' style={[style.addIcon, { marginLeft: 35 }]} />
        <Text style={style.optionText} >추가하기</Text>
      </Pressable>
    </Accordion>
  );

155
156
157
158
159
160
  return (
    <>
      {console.log(option)}
      <View>
        <FlatList
          data={options}
161
          renderItem={type === 'asset' ? renderAssetItem : renderCatItem}
162
163
164
165
166
167
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
          keyExtractor={item => item.id.toString()}
        />
        <Pressable style={style.addButton} onPress={() => setModalOpen(true)}>
          <AntDesign name='plus' style={style.icon} />
          <Text style={style.optionText} >추가하기</Text>
        </Pressable>
      </View>

      <Modal
        transparent
        swipeDirection="down"
        animationType_id="fade"
        visible={modalOpen}
        onRequestClose={modalClose}
      >
        <View style={style.modalContainer}>
          <View style={style.modalHeader}>
            <View style={{ flexDirection: "row" }}>
              <AntDesign name='caretleft' style={style.icon} onPress={() => modalClose()} />
              <Text style={style.Font}>{option.id === 0 ? '추가' : '수정'}</Text>
            </View>
          </View>
          <View style={style.modalBody}>
            <InputBox
              placeholder="이름을 입력하세요."
              onChangeText={
                (name) => setOption({ ...option, value: name })
              }
              value={option.value}
              maxLength={30}
            />
            <View style={style.buttonRow}>
              <StyledButton
                name="저장하기"
                onPress={option.id === 0 ? handleAdd : handleUpdate}
                style={style.submitButton}
              />
            </View>
          </View>
        </View>
      </Modal>
    </>

  )
}

const style = StyleSheet.create({
  flexRow: {
    flexDirection: 'row',
  },
  flexCenter: {
    justifyContent: 'center',
    alignItems: 'center',
  },
  catBox: {
    justifyContent: 'space-between',
    paddingVertical: 10,
    backgroundColor: 'lightgray',
  },
  Font: {
    fontSize: 24
  },
  icon: {
    marginHorizontal: 5,
    fontSize: 30,
    color: 'black',
  },
229
230
231
232
233
  addIcon: {
    marginHorizontal: 5,
    fontSize: 25,
    color: 'black',
  },
234
235
  cancelIcon: {
    marginHorizontal: 5,
236
    fontSize: 25,
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
    color: 'red',
  },
  rightIcon: {
    marginHorizontal: 5,
    fontSize: 20,
    color: 'black',
  },
  optionText: {
    fontSize: 20,
    marginHorizontal: 10,
  },
  addButton: {
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    paddingVertical: 10,
    backgroundColor: 'gray',
    margin: 15
  },
  modalContainer: {
    flex: 1,
    backgroundColor: 'white',
  },
  modalHeader: {
    padding: 10,
  },
  modalBody: {

  },
  buttonRow: {
    flexDirection: 'row',
    alignItems: "center",
    marginHorizontal: 10,
    marginVertical: 3,
  },
  submitButton: {
    flex: 1,
    height: 50,
  },
});

export default EditOption;