| 1 | <template> |
| 2 | <n-spin :show="loading" class="flex min-h-48 grow flex-col" content-class="flex grow flex-col gap-4"> |
| 3 | <div> |
| 4 | <n-collapse-transition :show="!showUploadForm"> |
| 5 | <n-button v-if="dataStore.length" :loading="uploading" type="primary" @click="openUploadForm()"> |
| 6 | <template #icon> |
| 7 | <Icon :name="UploadIcon" /> |
| 8 | </template> |
| 9 | Upload Data Store file |
| 10 | </n-button> |
| 11 | </n-collapse-transition> |
| 12 | |
| 13 | <n-collapse-transition :show="showUploadForm"> |
| 14 | <div class="flex flex-col gap-2"> |
| 15 | <n-upload v-model:file-list="fileList" :max="1" :disabled="uploading"> |
| 16 | <n-upload-dragger> |
| 17 | <div> |
| 18 | <Icon :name="UploadIcon" :size="28" :depth="3" /> |
| 19 | </div> |
| 20 | <div class="font-semibold">Click or drag a file to this area to upload</div> |
| 21 | </n-upload-dragger> |
| 22 | </n-upload> |
| 23 | |
| 24 | <div class="flex items-center justify-end gap-3"> |
| 25 | <n-button quaternary :disabled="uploading" @click="closeUploadForm()">Close</n-button> |
| 26 | |
| 27 | <n-button |
| 28 | :disabled="!isValid" |
| 29 | :loading="uploading" |
| 30 | type="primary" |
| 31 | @click="uploadDataStoreFile()" |
| 32 | > |
| 33 | <template #icon> |
| 34 | <Icon :name="UploadIcon" /> |
| 35 | </template> |
| 36 | |
| 37 | Upload |
| 38 | </n-button> |
| 39 | </div> |
| 40 | </div> |
| 41 | </n-collapse-transition> |
| 42 | </div> |
| 43 | |
| 44 | <div class="flex flex-col gap-2"> |
| 45 | <template v-if="dataStore.length"> |
| 46 | <CaseDataStoreItem |
| 47 | v-for="dataStoreFile of dataStore" |
| 48 | :key="dataStoreFile.id" |
| 49 | :data-store-file |
| 50 | embedded |
| 51 | @deleted="deleteDataStoreFile(dataStoreFile)" |
| 52 | /> |
| 53 | </template> |
| 54 | <template v-else> |
| 55 | <n-collapse-transition :show="!showUploadForm"> |
| 56 | <n-empty v-if="!loading" class="min-h-48"> |
| 57 | <div class="flex flex-col items-center gap-4"> |
| 58 | <p>No files found</p> |
| 59 | <n-button type="primary" :loading="uploading" @click="openUploadForm()"> |
| 60 | <template #icon> |
| 61 | <Icon :name="UploadIcon" /> |
| 62 | </template> |
| 63 | Upload a Data Store file |
| 64 | </n-button> |
| 65 | </div> |
| 66 | </n-empty> |
| 67 | </n-collapse-transition> |
| 68 | </template> |
| 69 | </div> |
| 70 | </n-spin> |
| 71 | </template> |
| 72 | |
| 73 | <script setup lang="ts"> |
| 74 | import type { UploadFileInfo } from "naive-ui" |
| 75 | import type { CaseDataStore } from "@/types/incidentManagement/cases.d" |
| 76 | import { NButton, NCollapseTransition, NEmpty, NSpin, NUpload, NUploadDragger, useMessage } from "naive-ui" |
| 77 | import { computed, onBeforeMount, ref } from "vue" |
| 78 | import Api from "@/api" |
| 79 | import Icon from "@/components/common/Icon.vue" |
| 80 | import CaseDataStoreItem from "./CaseDataStoreItem.vue" |
| 81 | |
| 82 | const { caseId } = defineProps<{ |
| 83 | caseId: number |
| 84 | }>() |
| 85 | |
| 86 | const emit = defineEmits<{ |
| 87 | (e: "deleted"): void |
| 88 | (e: "updated"): void |
| 89 | }>() |
| 90 | |
| 91 | const UploadIcon = "carbon:cloud-upload" |
| 92 | const message = useMessage() |
| 93 | const loading = ref(false) |
| 94 | const uploading = ref(false) |
| 95 | const showUploadForm = ref(false) |
| 96 | const dataStore = ref<CaseDataStore[]>([]) |
| 97 | const fileList = ref<UploadFileInfo[]>([]) |
| 98 | const newFile = computed<File | null>(() => fileList.value?.[0]?.file || null) |
| 99 | |
| 100 | const isValid = computed(() => !!newFile.value) |
| 101 | |
| 102 | function deleteDataStoreFile(dataStoreFile: CaseDataStore) { |
| 103 | dataStore.value = dataStore.value.filter(o => o.id !== dataStoreFile.id) |
| 104 | emit("deleted") |
| 105 | } |
| 106 | |
| 107 | function updateDataStore(dataStoreFile: CaseDataStore) { |
| 108 | fileList.value = [] |
| 109 | dataStore.value.push(dataStoreFile) |
| 110 | emit("updated") |
| 111 | } |
| 112 | |
| 113 | function openUploadForm() { |
| 114 | showUploadForm.value = true |
| 115 | } |
| 116 | |
| 117 | function closeUploadForm() { |
| 118 | showUploadForm.value = false |
| 119 | } |
| 120 | |
| 121 | function getCaseDataStore(caseId: number) { |
| 122 | loading.value = true |
| 123 | |
| 124 | Api.incidentManagement.cases |
| 125 | .getCaseDataStoreFiles(caseId) |
| 126 | .then(res => { |
| 127 | if (res.data.success) { |
| 128 | dataStore.value = res.data?.case_data_store || [] |
| 129 | } else { |
| 130 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 131 | } |
| 132 | }) |
| 133 | .catch(err => { |
| 134 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 135 | }) |
| 136 | .finally(() => { |
| 137 | loading.value = false |
| 138 | }) |
| 139 | } |
| 140 | |
| 141 | function uploadDataStoreFile() { |
| 142 | if (!newFile.value) return |
| 143 | |
| 144 | uploading.value = true |
| 145 | |
| 146 | Api.incidentManagement.cases |
| 147 | .uploadCaseDataStoreFile(caseId, newFile.value) |
| 148 | .then(res => { |
| 149 | if (res.data.success) { |
| 150 | message.success(res.data?.message || "Case Data Store File uploaded successfully") |
| 151 | updateDataStore(res.data.case_data_store) |
| 152 | } else { |
| 153 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 154 | } |
| 155 | }) |
| 156 | .catch(err => { |
| 157 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 158 | }) |
| 159 | .finally(() => { |
| 160 | uploading.value = false |
| 161 | }) |
| 162 | } |
| 163 | |
| 164 | onBeforeMount(() => { |
| 165 | getCaseDataStore(caseId) |
| 166 | }) |
| 167 | </script> |