-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestTask.vue
More file actions
149 lines (135 loc) · 4.42 KB
/
RequestTask.vue
File metadata and controls
149 lines (135 loc) · 4.42 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
<template>
<div class="w-full flex flex-col gap-y-6">
<CategoryDropDown
v-model="category1"
:options="mainCategoryArr"
:label-name="'1차 카테고리'"
:placeholderText="'1차 카테고리를 선택해주세요'"
:is-invalidate="isInvalidate"
:isDisabled="false" />
<CategoryDropDown
v-model="category2"
:options="afterSubCategoryArr"
:label-name="'2차 카테고리'"
:placeholderText="'2차 카테고리를 선택해주세요'"
:is-invalidate="isInvalidate"
:isDisabled="!category1" />
<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 { getSubCategoryDetail, postTaskRequest } from '@/api/user'
import type { Category, SubCategory } from '@/types/common'
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[]>([])
onMounted(async () => {
mainCategoryArr.value = await getMainCategory()
subCategoryArr.value = await getSubCategory()
afterSubCategoryArr.value = await getSubCategory()
})
watch(category1, async newValue => {
category2.value = null
afterSubCategoryArr.value = subCategoryArr.value.filter(
subCategory => subCategory.mainCategoryId === newValue?.mainCategoryId
)
})
watch(category2, async newVal => {
if (newVal) {
const res = await getSubCategoryDetail(newVal.subCategoryId)
description.value = res.descriptionExample
}
})
const router = useRouter()
const handleCancel = () => {
category1.value = null
category2.value = null
title.value = ''
description.value = ''
file.value = []
isModalVisible.value = ''
router.back()
}
const handleSubmit = async () => {
if (isSubmitting.value || isModalVisible.value) return
if (!category1.value || !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
}
isSubmitting.value = true
const formData = new FormData()
const taskInfo = {
categoryId: category2.value.subCategoryId,
title: title.value,
description: description.value
}
const jsonTaskInfo = JSON.stringify(taskInfo)
const newBlob = new Blob([jsonTaskInfo], { type: 'application/json' })
formData.append('taskInfo', newBlob)
if (file.value && file.value.length > 0) {
file.value.forEach(f => formData.append('attachment', f))
}
try {
await postTaskRequest(formData)
isModalVisible.value = 'success'
} catch (e) {
console.error(e)
isModalVisible.value = 'fail'
} finally {
isSubmitting.value = false
}
}
</script>