Show loading and retry states for approval attachments
This commit is contained in:
1 parent
14ed0ad15c
commit
0b20ce3068
5 files changed
+75
-16
No files matched your search
@@ -6,9 +6,23 @@ import http, { apiErrorMessage } from '../api/http'
|
||||
import AttachmentFileList from './AttachmentFileList.vue'
|
||||
|
||||
const props = defineProps<{ type: string; id: string }>()
|
||||
const { locale } = useI18n()
|
||||
const { t } = useI18n()
|
||||
const files = ref<{ id: string; fileName: string; length: number }[]>([])
|
||||
onMounted(async () => { try { files.value = (await http.get(`/approval-attachments/${props.type}/${props.id}`)).data } catch (e) { ElMessage.error(apiErrorMessage(e)) } })
|
||||
const loading = ref(true)
|
||||
const loadError = ref('')
|
||||
async function loadFiles() {
|
||||
loading.value = true
|
||||
loadError.value = ''
|
||||
try {
|
||||
files.value = (await http.get(`/approval-attachments/${props.type}/${props.id}`)).data
|
||||
} catch (error) {
|
||||
loadError.value = apiErrorMessage(error)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
onMounted(loadFiles)
|
||||
|
||||
async function download(file: { id: string; fileName: string }) {
|
||||
try {
|
||||
const response = await http.get(`/approval-attachments/${props.type}/${props.id}/${file.id}`, { responseType: 'blob' })
|
||||
@@ -18,8 +32,13 @@ async function download(file: { id: string; fileName: string }) {
|
||||
</script>
|
||||
<template>
|
||||
<section>
|
||||
<strong>{{ locale.startsWith('zh') ? '附件' : 'Attachments' }}</strong>
|
||||
<p v-if="!files.length" class="attachment-hint">{{ locale.startsWith('zh') ? '暂无附件' : 'No attachments' }}</p>
|
||||
<strong>{{ t('approvalAttachments.title') }}</strong>
|
||||
<p v-if="loading" class="attachment-hint" role="status">{{ t('approvalAttachments.loading') }}</p>
|
||||
<div v-else-if="loadError" class="attachment-error">
|
||||
<span role="alert">{{ loadError }}</span>
|
||||
<el-button link type="primary" @click="loadFiles">{{ t('common.reload') }}</el-button>
|
||||
</div>
|
||||
<p v-else-if="!files.length" class="attachment-hint">{{ t('approvalAttachments.empty') }}</p>
|
||||
<AttachmentFileList v-else :files="files" @download="download" />
|
||||
</section>
|
||||
</template>
|
||||
@@ -7,19 +7,27 @@ import http, { apiErrorMessage } from '../api/http'
|
||||
const policyHintId = useId()
|
||||
const props = defineProps<{ type: string }>()
|
||||
const emit = defineEmits<{ change: [files: File[]] }>()
|
||||
const { locale } = useI18n()
|
||||
const policy = ref({ allowedExtensions: '', maxFileBytes: 0, maxFileCount: 0 })
|
||||
const ready = ref(false)
|
||||
onMounted(async () => {
|
||||
try { policy.value = (await http.get(`/approval-attachments/policy/${props.type}`)).data; ready.value = true }
|
||||
catch (e) { ElMessage.error(apiErrorMessage(e)) }
|
||||
})
|
||||
const { t } = useI18n()
|
||||
const policy = ref<{ allowedExtensions: string; maxFileBytes: number; maxFileCount: number }>()
|
||||
const loadError = ref('')
|
||||
async function loadPolicy() {
|
||||
loadError.value = ''
|
||||
try {
|
||||
policy.value = (await http.get(`/approval-attachments/policy/${props.type}`)).data
|
||||
} catch (error) {
|
||||
loadError.value = apiErrorMessage(error)
|
||||
}
|
||||
}
|
||||
onMounted(loadPolicy)
|
||||
|
||||
function change(event: Event) {
|
||||
const limits = policy.value
|
||||
if (!limits) return
|
||||
const input = event.target as HTMLInputElement
|
||||
const files = Array.from(input.files ?? [])
|
||||
const allowed = policy.value.allowedExtensions.split(',').map(x => x.trim().toLowerCase())
|
||||
if (files.length > policy.value.maxFileCount || files.reduce((sum, x) => sum + x.size, 0) > 104857600 || files.some(x => !x.size || x.size > policy.value.maxFileBytes || !allowed.some(ext => x.name.toLowerCase().endsWith(ext)))) {
|
||||
ElMessage.warning(locale.value.startsWith('zh') ? '附件格式、大小或数量不符合要求。' : 'Attachment type, size or count does not meet the requirements.')
|
||||
const allowed = limits.allowedExtensions.split(',').map(x => x.trim().toLowerCase())
|
||||
if (files.length > limits.maxFileCount || files.reduce((sum, x) => sum + x.size, 0) > 104857600 || files.some(x => !x.size || x.size > limits.maxFileBytes || !allowed.some(ext => x.name.toLowerCase().endsWith(ext)))) {
|
||||
ElMessage.warning(t('approvalAttachments.invalid'))
|
||||
input.value = ''; emit('change', []); return
|
||||
}
|
||||
emit('change', files)
|
||||
@@ -27,7 +35,14 @@ function change(event: Event) {
|
||||
</script>
|
||||
<template>
|
||||
<div class="attachment-picker">
|
||||
<input type="file" :aria-label="locale.startsWith('zh') ? '附件' : 'Attachments'" :aria-describedby="ready ? policyHintId : undefined" multiple :disabled="!ready" :accept="policy.allowedExtensions" @change="change" />
|
||||
<p v-if="ready" :id="policyHintId" class="attachment-hint">{{ policy.allowedExtensions }} · {{ (policy.maxFileBytes / 1048576).toFixed(1) }} MiB / {{ locale.startsWith('zh') ? '文件' : 'file' }} · {{ locale.startsWith('zh') ? '最多' : 'up to' }} {{ policy.maxFileCount }} · {{ locale.startsWith('zh') ? '总计不超过 100 MiB' : '100 MiB total' }}</p>
|
||||
<input type="file" :aria-label="t('approvalAttachments.title')" :aria-describedby="policyHintId" multiple :disabled="!policy" :accept="policy?.allowedExtensions" @change="change" />
|
||||
<p v-if="policy" :id="policyHintId" class="attachment-hint">
|
||||
{{ policy.allowedExtensions }} · {{ t('approvalAttachments.limits', { size: (policy.maxFileBytes / 1048576).toFixed(1), count: policy.maxFileCount }) }}
|
||||
</p>
|
||||
<div v-else-if="loadError" class="attachment-error">
|
||||
<span :id="policyHintId" role="alert">{{ loadError }}</span>
|
||||
<el-button link type="primary" @click="loadPolicy">{{ t('common.reload') }}</el-button>
|
||||
</div>
|
||||
<p v-else :id="policyHintId" class="attachment-hint" role="status">{{ t('approvalAttachments.loadingPolicy') }}</p>
|
||||
</div>
|
||||
</template>
|
||||
@@ -10,6 +10,14 @@ export default {
|
||||
hours: "hours",
|
||||
hint: "Measured from the time shown on each item. The threshold is saved in this browser.",
|
||||
},
|
||||
approvalAttachments: {
|
||||
title: "Attachments",
|
||||
empty: "No attachments",
|
||||
loading: "Loading attachments…",
|
||||
loadingPolicy: "Loading upload requirements…",
|
||||
limits: "Up to {size} MiB per file, {count} files and 100 MiB in total.",
|
||||
invalid: "Attachment type, size or count does not meet the requirements.",
|
||||
},
|
||||
approvalProgress: {
|
||||
receivingCollegeApproved: "Receiving college approved",
|
||||
receivingCollegeRejected: "Receiving college rejected",
|
||||
|
||||
Loaded 3 of 5 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user