-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReRequestTask.vue
More file actions
178 lines (163 loc) · 5.63 KB
/
ReRequestTask.vue
File metadata and controls
178 lines (163 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<template>
<div class="w-full flex flex-col gap-y-6">
<CategoryDropDown
v-model="category1"
:options="mainCategoryArr"
:label-name="'1차 카테고리'"
:placeholderText="'1차 카테고리를 선택해주세요'"
:isDisabled="false"
:is-invalidate="isInvalidate" />
<CategoryDropDown
v-model="category2"
:options="afterSubCategoryArr"
:label-name="'2차 카테고리'"
:placeholderText="'2차 카테고리를 선택해주세요'"
:isDisabled="!category1"
:is-invalidate="isInvalidate" />
<RequestTaskInput
v-model="title"
:placeholderText="'제목을 입력해주세요'"
:label-name="'제목'"
:is-invalidate="isInvalidate" />
<RequestTaskTextArea
v-model="description"
:is-invalidate="isInvalidate"
:placeholderText="'부가 정보를 입력해주세요'" />
<RequestTaskFileInput v-model="file" />
<FormButtonContainer
:handleCancel="handleCancel"
:handleSubmit="handleSubmit"
cancelText="취소"
submitText="수정" />
<ModalView
:isOpen="isModalVisible === 'success'"
:type="'successType'"
@close="handleCancel">
<template #header>작업이 수정되었습니다</template>
</ModalView>
<ModalView
:isOpen="isModalVisible === 'fail'"
:type="'failType'"
@close="handleCancel">
<template #header>작업요청을 실패했습니다</template>
<template #body>잠시후 시도해주세요</template>
</ModalView>
</div>
</template>
<script lang="ts" setup>
import { getMainCategory, getSubCategory } from '@/api/common'
import { getTaskDetailUser, patchTaskRequest, postTaskRequest } from '@/api/user'
import type { Category, SubCategory } from '@/types/common'
import type { AttachmentResponse } from '@/types/user'
import { onMounted, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import FormButtonContainer from '../common/FormButtonContainer.vue'
import ModalView from '../common/ModalView.vue'
import CategoryDropDown from './CategoryDropDown.vue'
import RequestTaskFileInput from './RequestTaskFileInput.vue'
import RequestTaskInput from './RequestTaskInput.vue'
import RequestTaskTextArea from './RequestTaskTextArea.vue'
const category1 = ref<Category | null>(null)
const category2 = ref<SubCategory | null>(null)
const title = ref('')
const description = ref('')
const file = ref(null as File[] | null)
const isInvalidate = ref('')
const isModalVisible = ref('')
const isSubmitting = ref(false)
const mainCategoryArr = ref<Category[]>([])
const subCategoryArr = ref<SubCategory[]>([])
const afterSubCategoryArr = ref<SubCategory[]>([])
const initFileArr = ref<AttachmentResponse[]>([])
const isFirst = ref(true)
const { id, reqType } = defineProps<{ id: string; reqType: string }>()
const router = useRouter()
const handleCancel = () => {
router.back()
}
onMounted(async () => {
mainCategoryArr.value = await getMainCategory()
subCategoryArr.value = await getSubCategory()
afterSubCategoryArr.value = await getSubCategory()
const data = await getTaskDetailUser(Number(id))
const selected = mainCategoryArr.value.find(ct => ct.name === data.mainCategoryName) || null
category1.value = selected
category2.value = subCategoryArr.value.find(ct => ct.name === data.categoryName) || null
afterSubCategoryArr.value = subCategoryArr.value.filter(
subCategory => subCategory.mainCategoryId === selected?.mainCategoryId
)
title.value = data.title
description.value = data.description
file.value = data.attachmentResponses.map((attachment: AttachmentResponse) => {
return new File([attachment.fileUrl], attachment.fileName, { type: 'application/pdf' })
})
initFileArr.value = data.attachmentResponses
})
watch(category1, async newValue => {
if (isFirst.value) {
isFirst.value = false
} else {
category2.value = null
}
afterSubCategoryArr.value = subCategoryArr.value.filter(
subCategory => subCategory.mainCategoryId === newValue?.mainCategoryId
)
})
const handleSubmit = async () => {
if (isSubmitting.value || isModalVisible.value) return
if (!category2.value) {
isInvalidate.value = 'category'
return
} else if (!title.value) {
isInvalidate.value = 'input'
return
} else if (title.value.length > 30) {
isInvalidate.value = 'title'
return
} else if (description.value.length > 200) {
isInvalidate.value = 'description'
return
}
const formData = new FormData()
const attachmentsToDelete = initFileArr.value
.filter(initFile => !file.value?.some(f => f.name === initFile.fileName))
.map(initFile => initFile.fileId)
const taskInfo = {
categoryId: category2.value.subCategoryId,
title: title.value,
description: description.value
}
const taskInfoEdit = {
...taskInfo,
attachmentsToDelete: attachmentsToDelete
}
const jsonTaskInfo = JSON.stringify(taskInfoEdit)
const newBlob = new Blob([jsonTaskInfo], { type: 'application/json' })
formData.append('taskInfo', newBlob)
if (file.value && file.value.length > 0 && reqType === 'edit') {
const newFiles = file.value.filter(
f => !initFileArr.value.some(initFile => initFile.fileName === f.name)
)
newFiles.forEach(f => {
formData.append('attachment', f)
})
} else {
file.value?.forEach(f => {
formData.append('attachment', f)
})
}
try {
if (reqType === 're') {
await postTaskRequest(formData)
} else {
await patchTaskRequest(id, formData)
}
isModalVisible.value = 'success'
} catch (e) {
isModalVisible.value = 'fail'
console.error('요청 실패:', e)
} finally {
isSubmitting.value = false
}
}
</script>