-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
frontend/src/components/incidentManagement/cases/CasesExport.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<template> | ||
<n-dropdown placement="bottom-start" trigger="click" :options="customersOptions" @select="exportCases"> | ||
<n-button :size :loading="exporting" secondary @click="load()"> | ||
<template #icon> | ||
<Icon :name="DownloadIcon" :size="14" /> | ||
</template> | ||
Export | ||
</n-button> | ||
</n-dropdown> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import type { Customer } from "@/types/customers.d" | ||
import type { Size } from "naive-ui/es/button/src/interface" | ||
import Api from "@/api" | ||
import Icon from "@/components/common/Icon.vue" | ||
import { useSettingsStore } from "@/stores/settings" | ||
import { formatDate } from "@/utils" | ||
import { saveAs } from "file-saver" | ||
import { NButton, NDropdown, useMessage } from "naive-ui" | ||
import { computed, inject, ref, type Ref } from "vue" | ||
const { size } = defineProps<{ size?: Size }>() | ||
const DownloadIcon = "carbon:cloud-download" | ||
const loadingCustomersList = ref(false) | ||
const dFormats = useSettingsStore().dateFormat | ||
const exporting = ref(false) | ||
const message = useMessage() | ||
const customersList = inject<Ref<Customer[]>>("customers-list", ref([])) | ||
const customersOptions = computed(() => { | ||
return [ | ||
{ | ||
label: "Export All Cases", | ||
key: "--all--" | ||
}, | ||
{ | ||
label: `Export by Customer${loadingCustomersList.value ? "..." : ""}`, | ||
key: "customer", | ||
disabled: loadingCustomersList.value, | ||
children: loadingCustomersList.value | ||
? undefined | ||
: customersList.value.map(o => ({ | ||
label: `#${o.customer_code} - ${o.customer_name}`, | ||
key: o.customer_code | ||
})) | ||
} | ||
] | ||
}) | ||
function exportCases(key: string) { | ||
exporting.value = true | ||
const fileName = | ||
key === "--all--" | ||
? `cases_${formatDate(new Date(), dFormats.datetimesec)}.csv` | ||
: `cases_customer:${key}_${formatDate(new Date(), dFormats.datetimesec)}.csv` | ||
Api.incidentManagement | ||
.exportCases(key === "--all--" ? undefined : key) | ||
.then(res => { | ||
if (res.data) { | ||
saveAs(new Blob([res.data], { type: "text/csv;charset=utf-8" }), fileName) | ||
} else { | ||
message.warning("An error occurred. Please try again later.") | ||
} | ||
}) | ||
.catch(err => { | ||
message.error(err.response?.data?.message || "An error occurred. Please try again later.") | ||
}) | ||
.finally(() => { | ||
exporting.value = false | ||
}) | ||
} | ||
function getCustomers() { | ||
loadingCustomersList.value = true | ||
Api.customers | ||
.getCustomers() | ||
.then(res => { | ||
if (res.data.success) { | ||
customersList.value = res.data?.customers || [] | ||
} else { | ||
message.warning(res.data?.message || "An error occurred. Please try again later.") | ||
} | ||
}) | ||
.catch(err => { | ||
message.error(err.response?.data?.message || "An error occurred. Please try again later.") | ||
}) | ||
.finally(() => { | ||
loadingCustomersList.value = false | ||
}) | ||
} | ||
function load() { | ||
if (!customersList.value.length) { | ||
getCustomers() | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters