refactor: add delete

This commit is contained in:
Net 2024-09-19 09:50:31 +07:00
parent 479b933a54
commit 8122b578e5

View file

@ -1,11 +1,13 @@
<script setup lang="ts"> <script setup lang="ts">
import { QTableProps } from 'quasar'; import { QTableProps } from 'quasar';
import { computed, ref, watch, toRaw } from 'vue'; import { ref, toRaw } from 'vue';
import { dialog } from 'stores/utils';
import { useI18n } from 'vue-i18n';
import TableComponents from 'src/components/TableComponents.vue'; import TableComponents from 'src/components/TableComponents.vue';
import ShowAttachent from 'src/components/ShowAttachent.vue'; import ShowAttachent from 'src/components/ShowAttachent.vue';
import DialogForm from 'components/DialogForm.vue'; import DialogForm from 'components/DialogForm.vue';
const { t } = useI18n();
const obj = defineModel< const obj = defineModel<
{ {
_meta?: Record<string, any>; _meta?: Record<string, any>;
@ -18,22 +20,43 @@ const obj = defineModel<
default: [], default: [],
}); });
const dialog = ref(false); const modalDialog = ref(false);
const splitAttachment = ref(50); const splitAttachment = ref(50);
const currentIndex = ref(-1); const currentIndex = ref(-1);
const currentIndexDropdownList = ref(0);
const props = defineProps<{ const props = defineProps<{
ocr?: (group: any, file: File) => void | Promise<void>; ocr?: (group: any, file: File) => void | Promise<void>;
getFileList?: (group: any) => Promise<typeof obj.value>; getFileList?: (group: any) => Promise<typeof obj.value>;
deleteItem?: (obj: any) => void | Promise<boolean>;
save?: (
group: any,
meta: any,
file: File | undefined,
) => void | Promise<boolean>;
autoSave?: boolean;
readonly?: boolean; readonly?: boolean;
hideAction?: boolean; hideAction?: boolean;
columns: QTableProps['columns']; columns: QTableProps['columns'];
menu?: { label: string; value: string; _meta?: Record<string, any> }[]; menu?: { label: string; value: string; _meta?: Record<string, any> }[];
}>(); }>();
async function triggerDelete(obj: any) {
dialog({
color: 'negative',
icon: 'mdi-alert',
title: t('dialog.title.confirmDelete'),
actionText: t('general.delete'),
message: t('dialog.message.confirmDelete'),
action: async () => {
await props.deleteItem?.(obj);
await fileList();
},
cancel: () => {},
});
}
function browse() { function browse() {
inputFile?.click(); inputFile?.click();
} }
@ -74,7 +97,17 @@ function change(e: Event) {
}; };
} }
dialog.value = true; modalDialog.value = true;
}
async function fileList() {
if (props.getFileList) {
const res = await props.getFileList(selectedMenu.value?.value);
if (res && Array.isArray(res)) {
obj.value = [...res];
}
}
} }
defineEmits<{ defineEmits<{
@ -127,13 +160,8 @@ defineEmits<{
@click=" @click="
async () => { async () => {
selectedMenu = v; selectedMenu = v;
if (autoSave) {
if (getFileList) { fileList();
const res = await getFileList(
menu?.[currentIndexDropdownList].value || '',
);
console.log(res);
} }
} }
" "
@ -155,14 +183,34 @@ defineEmits<{
<div class="full-height full-width q-pa-md"> <div class="full-height full-width q-pa-md">
<TableComponents <TableComponents
buttomDownload buttomDownload
@delete="
async (index) => {
if (autoSave) {
await triggerDelete(obj[index]);
}
}
"
@view="
(index) => {
currentIndex = index;
modalDialog = true;
}
"
:rows=" :rows="
obj.map((v, index) => { obj
return { .filter((v) => {
branchNo: index + 1, if (!autoSave && v.group !== selectedMenu?.value) {
attachmentName: v.file?.name, return false;
uploadDate: '', }
}; return true;
}) })
.map((v, index) => {
return {
branchNo: index + 1,
attachmentName: v.file?.name || v.name,
uploadDate: '',
};
})
" "
:columns="columns" :columns="columns"
></TableComponents> ></TableComponents>
@ -175,19 +223,32 @@ defineEmits<{
style="position: absolute" style="position: absolute"
height="100vh" height="100vh"
weight="90%" weight="90%"
v-model:modal="dialog" v-model:modal="modalDialog"
title="ดูตัวอย่าง" title="ดูตัวอย่าง"
hideCloseEvent hideCloseEvent
v-if="obj.length > 0" v-if="obj.length > 0"
:close=" :close="
() => { () => {
obj.splice(currentIndex, 1); if (!autoSave || !obj[currentIndex]?._meta?.hasOwnProperty('id')) {
dialog = false; obj.splice(currentIndex, 1);
}
modalDialog = false;
} }
" "
:submit=" :submit="
() => { async () => {
dialog = false; modalDialog = false;
if (autoSave) {
const statusSave = await save?.(
obj[currentIndex].group,
obj[currentIndex]._meta,
obj[currentIndex].file,
);
if (statusSave) {
fileList();
}
}
} }
" "
> >