198 lines
5.1 KiB
Vue
198 lines
5.1 KiB
Vue
|
|
<script setup>
|
||
|
|
import { computed, onMounted, ref } from 'vue'
|
||
|
|
import { useRouter } from 'vue-router'
|
||
|
|
import { accountApi } from '@/api/accountApi'
|
||
|
|
import IconBtn from '@/components/ui/IconBtn.vue'
|
||
|
|
|
||
|
|
const router = useRouter()
|
||
|
|
|
||
|
|
const categories = ref([])
|
||
|
|
const loading = ref(false)
|
||
|
|
const error = ref(null)
|
||
|
|
const activeType = ref('EXPENSE') // EXPENSE / INCOME
|
||
|
|
const newName = ref('')
|
||
|
|
|
||
|
|
const filtered = computed(() => categories.value.filter((c) => c.type === activeType.value))
|
||
|
|
|
||
|
|
async function load() {
|
||
|
|
loading.value = true
|
||
|
|
error.value = null
|
||
|
|
try {
|
||
|
|
categories.value = await accountApi.categories()
|
||
|
|
} catch (e) {
|
||
|
|
error.value = e.response?.data?.message || '불러오지 못했습니다.'
|
||
|
|
} finally {
|
||
|
|
loading.value = false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function addCategory() {
|
||
|
|
const name = newName.value.trim()
|
||
|
|
if (!name) return
|
||
|
|
try {
|
||
|
|
await accountApi.createCategory({ type: activeType.value, name })
|
||
|
|
newName.value = ''
|
||
|
|
await load()
|
||
|
|
} catch (e) {
|
||
|
|
alert(e.response?.data?.message || '추가에 실패했습니다.')
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function saveCategory(c) {
|
||
|
|
const name = (c.name || '').trim()
|
||
|
|
if (!name) return
|
||
|
|
try {
|
||
|
|
await accountApi.updateCategory(c.id, { type: c.type, name })
|
||
|
|
await load()
|
||
|
|
} catch (e) {
|
||
|
|
alert(e.response?.data?.message || '수정에 실패했습니다.')
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function removeCategory(c) {
|
||
|
|
if (!confirm(`'${c.name}' 분류를 삭제할까요? (기존 내역의 분류명은 그대로 유지됩니다)`)) return
|
||
|
|
try {
|
||
|
|
await accountApi.removeCategory(c.id)
|
||
|
|
await load()
|
||
|
|
} catch (e) {
|
||
|
|
alert(e.response?.data?.message || '삭제에 실패했습니다.')
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function importExisting() {
|
||
|
|
if (!confirm('기존 내역에서 사용한 분류들을 목록으로 가져올까요?')) return
|
||
|
|
try {
|
||
|
|
categories.value = await accountApi.importCategories()
|
||
|
|
} catch (e) {
|
||
|
|
alert(e.response?.data?.message || '불러오기에 실패했습니다.')
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
onMounted(load)
|
||
|
|
</script>
|
||
|
|
|
||
|
|
<template>
|
||
|
|
<section class="cat">
|
||
|
|
<header class="head">
|
||
|
|
<h1>분류 관리</h1>
|
||
|
|
<div class="head-actions">
|
||
|
|
<button type="button" class="text-btn" @click="importExisting">기존 분류 불러오기</button>
|
||
|
|
<IconBtn icon="back" title="가계부로" @click="router.push('/account')" />
|
||
|
|
</div>
|
||
|
|
</header>
|
||
|
|
<p class="hint">내역·예산에서 선택할 분류를 관리합니다. 분류 이름을 바꾸면 기존 내역·예산에도 반영됩니다.</p>
|
||
|
|
|
||
|
|
<div class="tabs">
|
||
|
|
<button type="button" :class="{ active: activeType === 'EXPENSE' }" @click="activeType = 'EXPENSE'">지출 분류</button>
|
||
|
|
<button type="button" :class="{ active: activeType === 'INCOME' }" @click="activeType = 'INCOME'">수입 분류</button>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<form class="new-cat" @submit.prevent="addCategory">
|
||
|
|
<input v-model="newName" type="text" :placeholder="activeType === 'EXPENSE' ? '예: 식비, 교통' : '예: 급여, 용돈'" />
|
||
|
|
<IconBtn icon="plus" title="추가" variant="primary" type="submit" />
|
||
|
|
</form>
|
||
|
|
|
||
|
|
<p v-if="error" class="msg error">{{ error }}</p>
|
||
|
|
<p v-if="loading" class="msg">불러오는 중...</p>
|
||
|
|
|
||
|
|
<ul v-else-if="filtered.length" class="cat-list">
|
||
|
|
<li v-for="c in filtered" :key="c.id" class="cat-row">
|
||
|
|
<input v-model="c.name" class="cat-name" @keyup.enter="saveCategory(c)" />
|
||
|
|
<IconBtn icon="check" title="저장" @click="saveCategory(c)" />
|
||
|
|
<IconBtn icon="trash" title="삭제" variant="danger" @click="removeCategory(c)" />
|
||
|
|
</li>
|
||
|
|
</ul>
|
||
|
|
<p v-else-if="!loading" class="msg">
|
||
|
|
등록된 {{ activeType === 'EXPENSE' ? '지출' : '수입' }} 분류가 없습니다.
|
||
|
|
</p>
|
||
|
|
</section>
|
||
|
|
</template>
|
||
|
|
|
||
|
|
<style scoped>
|
||
|
|
.cat {
|
||
|
|
max-width: 560px;
|
||
|
|
margin: 0 auto;
|
||
|
|
}
|
||
|
|
.head {
|
||
|
|
display: flex;
|
||
|
|
align-items: center;
|
||
|
|
justify-content: space-between;
|
||
|
|
}
|
||
|
|
.head-actions {
|
||
|
|
display: flex;
|
||
|
|
gap: 0.5rem;
|
||
|
|
}
|
||
|
|
h1 {
|
||
|
|
font-size: 1.5rem;
|
||
|
|
}
|
||
|
|
.hint {
|
||
|
|
font-size: 0.85rem;
|
||
|
|
opacity: 0.7;
|
||
|
|
margin: 0.5rem 0 1rem;
|
||
|
|
}
|
||
|
|
input {
|
||
|
|
padding: 0.5rem 0.7rem;
|
||
|
|
border: 1px solid var(--color-border);
|
||
|
|
border-radius: 4px;
|
||
|
|
background: var(--color-background-soft);
|
||
|
|
color: var(--color-text);
|
||
|
|
}
|
||
|
|
button {
|
||
|
|
padding: 0.45rem 0.85rem;
|
||
|
|
border: 1px solid var(--color-border);
|
||
|
|
border-radius: 4px;
|
||
|
|
background: var(--color-background-soft);
|
||
|
|
color: var(--color-text);
|
||
|
|
cursor: pointer;
|
||
|
|
}
|
||
|
|
button.danger {
|
||
|
|
border-color: #c0392b;
|
||
|
|
color: #c0392b;
|
||
|
|
}
|
||
|
|
.tabs {
|
||
|
|
display: flex;
|
||
|
|
gap: 0.25rem;
|
||
|
|
border-bottom: 1px solid var(--color-border);
|
||
|
|
margin-bottom: 1rem;
|
||
|
|
}
|
||
|
|
.tabs button {
|
||
|
|
border: 0;
|
||
|
|
border-bottom: 2px solid transparent;
|
||
|
|
border-radius: 0;
|
||
|
|
background: transparent;
|
||
|
|
padding: 0.6rem 1rem;
|
||
|
|
}
|
||
|
|
.tabs button.active {
|
||
|
|
border-bottom-color: hsla(160, 100%, 37%, 1);
|
||
|
|
color: hsla(160, 100%, 37%, 1);
|
||
|
|
font-weight: 600;
|
||
|
|
}
|
||
|
|
.new-cat {
|
||
|
|
display: flex;
|
||
|
|
gap: 0.5rem;
|
||
|
|
margin-bottom: 1rem;
|
||
|
|
}
|
||
|
|
.new-cat input {
|
||
|
|
flex: 1;
|
||
|
|
}
|
||
|
|
.cat-list {
|
||
|
|
list-style: none;
|
||
|
|
}
|
||
|
|
.cat-row {
|
||
|
|
display: flex;
|
||
|
|
gap: 0.5rem;
|
||
|
|
align-items: center;
|
||
|
|
padding: 0.4rem 0;
|
||
|
|
border-bottom: 1px solid var(--color-border);
|
||
|
|
}
|
||
|
|
.cat-name {
|
||
|
|
flex: 1;
|
||
|
|
}
|
||
|
|
.msg {
|
||
|
|
margin: 0.75rem 0;
|
||
|
|
}
|
||
|
|
.msg.error {
|
||
|
|
color: #c0392b;
|
||
|
|
}
|
||
|
|
</style>
|