refactor(realestate): 데이터 로직 useComplexes 훅 추출 → RealEstate shell 축소
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UHXzpsZQxKG9hQmNRfZjRS
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
import React, { useState, useEffect, useMemo } from 'react';
|
import React, { useState, useMemo } from 'react';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
import { apiGet, apiPost, apiPut, apiDelete } from '../../api';
|
|
||||||
import {
|
import {
|
||||||
SAMPLE_COMPLEXES, STATUS_CONFIG, TABS,
|
STATUS_CONFIG, TABS,
|
||||||
} from './realEstateUtils';
|
} from './realEstateUtils';
|
||||||
|
import useComplexes from './hooks/useComplexes';
|
||||||
import ComplexCard from './components/ComplexCard';
|
import ComplexCard from './components/ComplexCard';
|
||||||
import RightPanel from './components/RightPanel';
|
import RightPanel from './components/RightPanel';
|
||||||
import ScheduleView from './components/ScheduleView';
|
import ScheduleView from './components/ScheduleView';
|
||||||
@@ -14,41 +14,29 @@ import './RealEstate.css';
|
|||||||
|
|
||||||
// ── 메인 컴포넌트 ──────────────────────────────────────────────────────────────
|
// ── 메인 컴포넌트 ──────────────────────────────────────────────────────────────
|
||||||
const RealEstate = () => {
|
const RealEstate = () => {
|
||||||
const [complexes, setComplexes] = useState(SAMPLE_COMPLEXES);
|
const { complexes, addComplex, updateComplex, deleteComplex } = useComplexes();
|
||||||
const [selectedComplex, setSelectedComplex] = useState(null);
|
const [selectedComplex, setSelectedComplex] = useState(null);
|
||||||
const [activeTab, setActiveTab] = useState('목록');
|
const [activeTab, setActiveTab] = useState('목록');
|
||||||
const [filterStatus, setFilterStatus] = useState('전체');
|
const [filterStatus, setFilterStatus] = useState('전체');
|
||||||
const [showModal, setShowModal] = useState(false);
|
const [showModal, setShowModal] = useState(false);
|
||||||
const [editingComplex, setEditingComplex] = useState(null);
|
const [editingComplex, setEditingComplex] = useState(null);
|
||||||
|
|
||||||
useEffect(() => {
|
const handleAdd = (data) => {
|
||||||
apiGet('/api/realestate/complexes')
|
addComplex(data);
|
||||||
.then((data) => {
|
|
||||||
if (Array.isArray(data) && data.length > 0) setComplexes(data);
|
|
||||||
})
|
|
||||||
.catch(() => {});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const handleAdd = async (data) => {
|
|
||||||
const newComplex = { ...data, id: Date.now() };
|
|
||||||
setComplexes((prev) => [...prev, newComplex]);
|
|
||||||
setShowModal(false);
|
setShowModal(false);
|
||||||
try { await apiPost('/api/realestate/complexes', data); } catch {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleUpdate = async (data) => {
|
const handleUpdate = (data) => {
|
||||||
setComplexes((prev) => prev.map((c) => (c.id === data.id ? data : c)));
|
updateComplex(data);
|
||||||
if (selectedComplex?.id === data.id) setSelectedComplex(data);
|
if (selectedComplex?.id === data.id) setSelectedComplex(data);
|
||||||
setEditingComplex(null);
|
setEditingComplex(null);
|
||||||
setShowModal(false);
|
setShowModal(false);
|
||||||
try { await apiPut(`/api/realestate/complexes/${data.id}`, data); } catch {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = async (id) => {
|
const handleDelete = (id) => {
|
||||||
if (!confirm('삭제하시겠습니까?')) return;
|
if (!confirm('삭제하시겠습니까?')) return;
|
||||||
setComplexes((prev) => prev.filter((c) => c.id !== id));
|
deleteComplex(id);
|
||||||
if (selectedComplex?.id === id) setSelectedComplex(null);
|
if (selectedComplex?.id === id) setSelectedComplex(null);
|
||||||
try { await apiDelete(`/api/realestate/complexes/${id}`); } catch {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleModalSave = (data) => {
|
const handleModalSave = (data) => {
|
||||||
|
|||||||
33
src/pages/realestate/hooks/useComplexes.js
Normal file
33
src/pages/realestate/hooks/useComplexes.js
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { useState, useEffect } from 'react';
|
||||||
|
import { apiGet, apiPost, apiPut, apiDelete } from '../../../api';
|
||||||
|
import { SAMPLE_COMPLEXES } from '../realEstateUtils';
|
||||||
|
|
||||||
|
export default function useComplexes() {
|
||||||
|
const [complexes, setComplexes] = useState(SAMPLE_COMPLEXES);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
apiGet('/api/realestate/complexes')
|
||||||
|
.then((data) => {
|
||||||
|
if (Array.isArray(data) && data.length > 0) setComplexes(data);
|
||||||
|
})
|
||||||
|
.catch(() => {});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const addComplex = async (data) => {
|
||||||
|
const newComplex = { ...data, id: Date.now() };
|
||||||
|
setComplexes((prev) => [...prev, newComplex]);
|
||||||
|
try { await apiPost('/api/realestate/complexes', data); } catch { /* noop */ }
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateComplex = async (data) => {
|
||||||
|
setComplexes((prev) => prev.map((c) => (c.id === data.id ? data : c)));
|
||||||
|
try { await apiPut(`/api/realestate/complexes/${data.id}`, data); } catch { /* noop */ }
|
||||||
|
};
|
||||||
|
|
||||||
|
const deleteComplex = async (id) => {
|
||||||
|
setComplexes((prev) => prev.filter((c) => c.id !== id));
|
||||||
|
try { await apiDelete(`/api/realestate/complexes/${id}`); } catch { /* noop */ }
|
||||||
|
};
|
||||||
|
|
||||||
|
return { complexes, addComplex, updateComplex, deleteComplex };
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user