44 :is-open =" isModalVisible.add"
55 type =" successType"
66 @close =" handleAddModal" >
7- <template #header >카테고리가 등록되었습니다 </template >
7+ <template #header >카테고리가 {{ route.params.id ? '수정' : '등록' }}되었습니다 </template >
88 </ModalView >
99 <ModalView
1010 :is-open =" isModalVisible.cancel"
1111 type =" warningType"
1212 @close =" handleCancelModal"
1313 @click =" handleGoBack" >
14- <template #header >생성을 취소 하시겠습니까?</template >
14+ <template #header >{{ route.params.id ? '수정' : '생성' }}을 취소 하시겠습니까?</template >
1515 <template #body >작성하신 내용은 사라집니다</template >
1616 </ModalView >
1717 <ModalView
2626 :options =" categoryOptions.map(el => el.name)"
2727 label-name =" 1차 카테고리"
2828 placeholder-text =" 1차 카테고리를 선택해주세요"
29- v-if =" categoryStep == '2'" />
29+ v-if =" categoryStep == '2'"
30+ :disabled =" route.params.id !== undefined" />
3031 <RequestTaskInput
3132 v-model =" categoryForm.name"
3233 placeholder-text =" 카테고리명을 입력해주세요"
4142 :handle-cancel =" handleCancel"
4243 :handle-submit =" handleSubmit"
4344 cancel-text =" 취소"
44- submit-text =" 생성 " />
45+ : submit-text =" route.params.id ? '수정' : '등록' " />
4546 </div >
4647</template >
4748
4849<script lang="ts" setup>
4950import { CATEGORY_FORM } from ' @/constants/admin'
5051import { computed , onMounted , ref , watch } from ' vue'
51- import { useRouter } from ' vue-router'
52+ import { useRoute , useRouter } from ' vue-router'
5253import FormButtonContainer from ' ../common/FormButtonContainer.vue'
5354import ModalView from ' ../ModalView.vue'
5455import RequestTaskDropdown from ' ../request-task/RequestTaskDropdown.vue'
5556import RequestTaskInput from ' ../request-task/RequestTaskInput.vue'
5657import { axiosInstance } from ' @/utils/axios'
57- import { getMainCategory } from ' @/api/common'
58- import type { Category , CategoryForm } from ' @/types/common'
58+ import { getMainCategory , getSubCategory } from ' @/api/common'
59+ import type { Category , CategoryForm , SubCategory } from ' @/types/common'
5960
6061const router = useRouter ()
62+ const route = useRoute ()
6163
6264const { categoryStep } = defineProps <{
6365 categoryStep: string
@@ -98,11 +100,18 @@ const handleSubmit = async () => {
98100 }
99101
100102 try {
101- const requestUrl =
102- categoryStep === ' 1' ? ' /api/managements/main-category' : ' /api/managements/sub-category'
103- await axiosInstance .post (requestUrl , categoryForm .value , {
104- headers: { Authorization: ` Bearer ${import .meta .env .VITE_ACCESS_TOKEN } ` }
105- })
103+ const categoryId = route .params .id
104+ if (categoryId ) {
105+ const patchUrl = ` /api/managements/categories/${categoryId } `
106+ await axiosInstance .patch (patchUrl , {
107+ name: categoryForm .value .name ,
108+ code: categoryForm .value .code
109+ })
110+ } else {
111+ const postUrl =
112+ categoryStep === ' 1' ? ' /api/managements/main-category' : ' /api/managements/sub-category'
113+ await axiosInstance .post (postUrl , categoryForm .value )
114+ }
106115 isModalVisible .value .add = true
107116 } catch {
108117 handleFailModal ()
@@ -120,9 +129,32 @@ const isCodeInvalidate = computed(() => {
120129const mainCategory = ref (' ' )
121130const categoryOptions = ref <Category []>([])
122131onMounted (async () => {
123- categoryOptions .value = await getMainCategory ()
124- if (categoryStep === ' 2' )
125- categoryForm .value = { ... categoryForm .value , mainCategoryId: undefined }
132+ const id = Number (route .params .id )
133+ categoryForm .value = { ... CATEGORY_FORM }
134+ if (categoryStep === ' 1' ) {
135+ if (id ) {
136+ const mainCategories: Category [] = await getMainCategory ()
137+ const initailValue = mainCategories .find (el => el .id === id )
138+ if (initailValue ) {
139+ categoryForm .value = { name: initailValue .name , code: initailValue .code }
140+ }
141+ }
142+ } else if (categoryStep === ' 2' ) {
143+ categoryOptions .value = await getMainCategory ()
144+ if (id ) {
145+ const subCategory: SubCategory [] = await getSubCategory ()
146+ const initailValue = subCategory .find (el => el .id === id )
147+ if (initailValue ) {
148+ categoryForm .value = {
149+ name: initailValue .name ,
150+ code: initailValue .code ,
151+ mainCategoryId: initailValue .mainCategoryId
152+ }
153+ mainCategory .value =
154+ categoryOptions .value .find (el => el .id === initailValue .mainCategoryId )?.name || ' '
155+ }
156+ }
157+ }
126158})
127159watch (mainCategory , () => {
128160 categoryForm .value .mainCategoryId = categoryOptions .value .find (
0 commit comments