|
5 | 5 | type="file" |
6 | 6 | id="file" |
7 | 7 | accept=".csv" |
| 8 | + ref="fileInput" |
8 | 9 | @change="handleFileUpload" /> |
9 | 10 | <label |
10 | 11 | for="file" |
|
15 | 16 | 파일로 일괄 추가 |
16 | 17 | </label> |
17 | 18 | <ModalView |
18 | | - :isOpen="isModalVisible" |
| 19 | + :isOpen="isModalVisible === 'success'" |
19 | 20 | :type="'successType'" |
20 | 21 | @close="handleCancel"> |
21 | 22 | <template #header>회원이 추가되었습니다</template> |
22 | 23 | </ModalView> |
| 24 | + <ModalView |
| 25 | + :isOpen="isModalVisible === 'error'" |
| 26 | + :type="'failType'" |
| 27 | + @close="handleCancel"> |
| 28 | + <template #header>회원 추가를 실패했습니다</template> |
| 29 | + <template #body>{{ errorText }}</template> |
| 30 | + </ModalView> |
23 | 31 | </div> |
24 | 32 | </template> |
25 | 33 |
|
26 | 34 | <script setup lang="ts"> |
27 | 35 | import { addMemberAdminByCsv } from '@/api/admin' |
28 | 36 | import { plusIcon } from '@/constants/iconPath' |
29 | 37 | import { useMemberManagementParamsStore } from '@/stores/params' |
| 38 | +import { getErrorCSV } from '@/utils/getErorr' |
30 | 39 | import { useQueryClient } from '@tanstack/vue-query' |
31 | 40 | import { ref } from 'vue' |
32 | 41 | import CommonIcons from '../common/CommonIcons.vue' |
33 | 42 | import ModalView from '../common/ModalView.vue' |
34 | 43 |
|
35 | 44 | const queryClient = useQueryClient() |
36 | | -const isModalVisible = ref(false) |
| 45 | +const isModalVisible = ref('') |
37 | 46 | const { params } = useMemberManagementParamsStore() |
| 47 | +const errorText = ref('') |
| 48 | +const fileInput = ref<HTMLInputElement | null>(null) |
38 | 49 |
|
39 | 50 | const handleFileUpload = async (event: Event) => { |
40 | | - const target = event.target as HTMLInputElement |
41 | | - const file = target.files?.[0] |
42 | | - if (!file) return |
43 | | - const formData = new FormData() |
44 | | - formData.append('file', file) |
45 | | - await addMemberAdminByCsv(formData) |
46 | | - queryClient.invalidateQueries({ queryKey: ['member', { ...params }] }) |
47 | | - isModalVisible.value = true |
48 | | - target.value = '' |
| 51 | + try { |
| 52 | + const target = event.target as HTMLInputElement |
| 53 | + const file = target.files?.[0] |
| 54 | + if (!file) return |
| 55 | +
|
| 56 | + const formData = new FormData() |
| 57 | + formData.append('file', file) |
| 58 | + await addMemberAdminByCsv(formData) |
| 59 | +
|
| 60 | + queryClient.invalidateQueries({ queryKey: ['member', { ...params }] }) |
| 61 | + isModalVisible.value = 'success' |
| 62 | + } catch (error) { |
| 63 | + errorText.value = getErrorCSV(error) |
| 64 | + isModalVisible.value = 'error' |
| 65 | + } finally { |
| 66 | + if (fileInput.value) { |
| 67 | + fileInput.value.value = '' |
| 68 | + } |
| 69 | + } |
49 | 70 | } |
50 | 71 |
|
51 | 72 | const handleCancel = () => { |
52 | | - isModalVisible.value = false |
| 73 | + isModalVisible.value = '' |
| 74 | + errorText.value = '' |
53 | 75 | } |
54 | 76 | </script> |
0 commit comments