Files
CRM_Montzar/apps/web/src/views/AttachmentsView.vue
T

158 lines
5.1 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import { computed, onMounted, ref } from 'vue';
import TableColumnsMenu from '@/components/TableColumnsMenu.vue';
import { RouterLink } from 'vue-router';
import axios from 'axios';
import { api } from '@/lib/api';
import { formatJalaliNumeric } from '@/lib/jalali';
import { registerTableColumns, useTableColumns } from '@/lib/tableColumns';
import type { NoteAttachmentDto, NoteDto, PaginatedResponse } from '@mento/shared';
import {
Alert as AAlert,
Button as AButton,
Card as ACard,
Empty as AEmpty,
Flex as AFlex,
Spin as ASpin,
Table as ATable,
Typography,
} from 'ant-design-vue';
import type { TableColumnsType } from 'ant-design-vue';
import { DownloadOutlined } from '@ant-design/icons-vue';
const { Title: ATypographyTitle, Link: ATypographyLink } = Typography;
type Row = NoteAttachmentDto & {
noteId: string;
noteTitle: string;
};
const ATT_COL_LABELS = ['نام فایل', 'یادداشت', 'حجم', 'تاریخ'] as const;
registerTableColumns('attachments', [...ATT_COL_LABELS]);
const tableCols = useTableColumns(() => 'attachments');
const loading = ref(false);
const error = ref('');
const items = ref<Row[]>([]);
const listEmpty = computed(() => !loading.value && !items.value.length);
const columns = computed<TableColumnsType<Row>>(() => {
const all: TableColumnsType<Row> = [
{ title: 'نام فایل', key: 'نام فایل', dataIndex: 'fileName' },
{ title: 'یادداشت', key: 'یادداشت' },
{ title: 'حجم', key: 'حجم' },
{ title: 'تاریخ', key: 'تاریخ' },
{ title: '', key: 'عملیات', width: 110 },
];
return all.filter((col) => {
const key = String(col.key ?? '');
if (key === 'عملیات') return true;
if (!ATT_COL_LABELS.includes(key as (typeof ATT_COL_LABELS)[number])) return true;
return tableCols.isVisible(key);
});
});
function sizeLabel(bytes: number) {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
function rowKey(row: Row) {
return `${row.noteId}-${row.id}`;
}
async function load() {
loading.value = true;
error.value = '';
try {
const { data } = await api.get<PaginatedResponse<NoteDto>>('/notes', {
params: { limit: 100 },
});
items.value = data.items.flatMap((note) =>
(note.attachments ?? []).map((att) => ({
...att,
noteId: note.id,
noteTitle: note.title?.trim() || note.body.slice(0, 40) || 'یادداشت',
})),
);
} catch (e) {
error.value = axios.isAxiosError(e)
? 'بارگذاری پیوست‌ها ناموفق بود.'
: 'بارگذاری پیوست‌ها ناموفق بود.';
} finally {
loading.value = false;
}
}
async function download(row: Row) {
const { data } = await api.get<Blob>(
`/notes/${row.noteId}/attachments/${row.id}/download`,
{ responseType: 'blob' },
);
const url = URL.createObjectURL(data);
const a = document.createElement('a');
a.href = url;
a.download = row.fileName;
a.click();
URL.revokeObjectURL(url);
}
onMounted(load);
</script>
<template>
<a-flex vertical :gap="16">
<a-typography-title :level="3" :style="{ margin: 0 }">پیوست‌ها</a-typography-title>
<a-alert v-if="error" type="error" show-icon :message="error" />
<a-spin :spinning="loading">
<a-card v-if="listEmpty && !error" :bordered="false">
<a-empty description="پیوستی ثبت نشده." />
</a-card>
<a-card v-else-if="!error && items.length" :bordered="false">
<a-flex justify="flex-end" :style="{ marginBottom: '12px' }">
<TableColumnsMenu table-key="attachments" align="end" />
</a-flex>
<a-table
:columns="columns"
:data-source="items"
:pagination="false"
:row-key="rowKey"
:scroll="{ x: 640 }"
>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'نام فایل'">
{{ (record as Row).fileName }}
</template>
<template v-else-if="column.key === 'یادداشت'">
<RouterLink :to="{ name: 'notes' }" custom v-slot="{ href, navigate }">
<a-typography-link :href="href" @click="navigate">
{{ (record as Row).noteTitle }}
</a-typography-link>
</RouterLink>
</template>
<template v-else-if="column.key === 'حجم'">
{{ sizeLabel((record as Row).sizeBytes) }}
</template>
<template v-else-if="column.key === 'تاریخ'">
{{ formatJalaliNumeric(new Date((record as Row).createdAt)) }}
</template>
<template v-else-if="column.key === 'عملیات'">
<a-button @click="download(record as Row)">
<template #icon><DownloadOutlined /></template>
دانلود
</a-button>
</template>
</template>
</a-table>
</a-card>
</a-spin>
</a-flex>
</template>