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'; import Accordion from './components/Accordion'; const INIT_OPTION = { id: 0, value: '' } const TEST_OPTION = { id: 15, value: 'testtest' } const TEST_SUB = [{ id: 1, value: 'test1', }, { id: 2, value: 'test2' }] const EditOption = ({ route }) => { console.log('catEdit: type_id ', route.params) const type_id = route.params 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(() => { if (type_id > 0) { loadCat() } else { loadAsset() } }, []) const loadCat = async () => { try { const catArray = await editApi.selectCategories(type_id) setOptions(catArray) } catch (error) { } } const loadAsset = async () => { try { const assetArray = await editApi.selectAssetsType() setOptions(assetArray) } catch (error) { } } const handleUpdate = () => { if (type_id > 0) { updateCat() } else { updateAsset() } } const updateAsset = async () => { try { const res = await editApi.updateOption('assets_type', { id: option.id, name: 'assets', value: option.value }) console.log(res) loadAsset() modalClose() } catch (error) { } } const updateCat = async () => { try { const res = await editApi.updateOption('categories', { id: option.id, name: 'category', value: option.value }) console.log(res) loadCat() modalClose() } catch (error) { } } const handleDelete = (item) => { if (type_id > 0) { deleteCat(item) } else { deleteAsset(item) } } const deleteAsset = async (item) => { try { const res = await editApi.deleteOption('assets_type', { id: item.id, name: 'assets' }) console.log(res) loadAsset() modalClose() } catch (error) { } } const deleteCat = async (item) => { try { const res = await editApi.deleteOption('categories', { id: item.id, name: 'category' }) console.log(res) loadCat() modalClose() } catch (error) { } } const handleAdd = () => { if (type_id > 0) { addCat() } else { addAsset() } } const addAsset = async () => { try { const res = await editApi.addOption('assets_type', { name: 'assets', value: option.value }) console.log(res) loadAsset() modalClose() } catch (error) { } } const addCat = async () => { try { const res = await editApi.addOption('categories', { name: 'category', value: option.value, foreign_name: 'type', foreign_id: type_id }) console.log(res) loadCat() modalClose() } catch (error) { } } const renderOptionItem = ({ item }) => ( {item.deletable && { handleDelete(item) }} />} {item.value} {item.deletable && { setOption(item); setModalOpen(true) }} />} console.log('서브 카테고리 더보기')} /> ); return ( <> {console.log(option)} item.id.toString()} /> { handleDelete({ TEST_OPTION }) }} /> {TEST_OPTION.value} { setOption(TEST_OPTION); setModalOpen(true) }} /> console.log('서브 카테고리 더보기')} /> setModalOpen(true)}> 추가하기 modalClose()} /> {option.id === 0 ? '추가' : '수정'} setOption({ ...option, value: name }) } value={option.value} maxLength={30} /> ) } 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', }, cancelIcon: { marginHorizontal: 5, fontSize: 30, 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;