| 1 | <template> |
| 2 | <n-spin :show="loading"> |
| 3 | <div class="flex min-h-28 flex-col gap-2 p-px"> |
| 4 | <div v-if="sortedList.length" class="mb-2 flex justify-end"> |
| 5 | <n-button type="primary" @click="exportToCSV"> |
| 6 | <template #icon> |
| 7 | <Icon :name="DownloadIcon" /> |
| 8 | </template> |
| 9 | Export |
| 10 | </n-button> |
| 11 | </div> |
| 12 | <AgentCard |
| 13 | v-for="agent in sortedList" |
| 14 | :key="agent.agent_id" |
| 15 | :agent |
| 16 | embedded |
| 17 | show-actions |
| 18 | class="item-appear item-appear-bottom item-appear-005" |
| 19 | > |
| 20 | <template #actions-left> |
| 21 | <n-tooltip to="body"> |
| 22 | <span class="text-sm">Open in new tab</span> |
| 23 | <template #trigger> |
| 24 | <n-button text size="small" type="primary" @click.stop="openAgentInNewTab(agent.agent_id)"> |
| 25 | <template #icon> |
| 26 | <Icon name="carbon:launch" /> |
| 27 | </template> |
| 28 | Open |
| 29 | </n-button> |
| 30 | </template> |
| 31 | </n-tooltip> |
| 32 | </template> |
| 33 | </AgentCard> |
| 34 | <n-empty v-if="!sortedList.length" description="No Agents found" class="h-48 justify-center" /> |
| 35 | </div> |
| 36 | </n-spin> |
| 37 | </template> |
| 38 | |
| 39 | <script setup lang="ts"> |
| 40 | // TODO-FE: refactor |
| 41 | import type { Agent } from "@/types/agents.d" |
| 42 | import type { Customer } from "@/types/customers.d" |
| 43 | import { saveAs } from "file-saver" |
| 44 | import _sortBy from "lodash/sortBy" |
| 45 | import { NButton, NEmpty, NSpin, NTooltip, useMessage } from "naive-ui" |
| 46 | import Papa from "papaparse" |
| 47 | import { computed, onBeforeMount, ref, toRefs } from "vue" |
| 48 | import { useRouter } from "vue-router" |
| 49 | import Api from "@/api" |
| 50 | import AgentCard from "@/components/agents/AgentCard.vue" |
| 51 | import Icon from "@/components/common/Icon.vue" |
| 52 | |
| 53 | const props = defineProps<{ |
| 54 | customer: Customer |
| 55 | }>() |
| 56 | const { customer } = toRefs(props) |
| 57 | |
| 58 | const DownloadIcon = "carbon:download" |
| 59 | |
| 60 | const loading = ref(false) |
| 61 | const router = useRouter() |
| 62 | const message = useMessage() |
| 63 | const list = ref<Agent[] | []>([]) |
| 64 | |
| 65 | const sortedList = computed(() => _sortBy(list.value, "critical_asset").reverse()) |
| 66 | |
| 67 | function exportToCSV() { |
| 68 | if (!sortedList.value.length) { |
| 69 | message.warning("No agents to export") |
| 70 | return |
| 71 | } |
| 72 | |
| 73 | const csv = Papa.unparse(sortedList.value, { |
| 74 | header: true |
| 75 | }) |
| 76 | |
| 77 | const filename = `agents_${customer.value.customer_code}_${new Date().toISOString().split("T")[0]}.csv` |
| 78 | const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" }) |
| 79 | saveAs(blob, filename) |
| 80 | |
| 81 | message.success("Export completed successfully") |
| 82 | } |
| 83 | |
| 84 | function openAgentInNewTab(agentId: string) { |
| 85 | const route = router.resolve({ name: "Agent", params: { id: agentId } }) |
| 86 | window.open(route.href, "_blank") |
| 87 | } |
| 88 | |
| 89 | function getAgents() { |
| 90 | loading.value = true |
| 91 | |
| 92 | Api.customers |
| 93 | .getCustomerAgents(customer.value.customer_code) |
| 94 | .then(res => { |
| 95 | if (res.data.success) { |
| 96 | list.value = res.data.agents || [] |
| 97 | } else { |
| 98 | message.error(res.data?.message || "An error occurred. Please try again later.") |
| 99 | } |
| 100 | }) |
| 101 | .catch(err => { |
| 102 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 103 | }) |
| 104 | .finally(() => { |
| 105 | loading.value = false |
| 106 | }) |
| 107 | } |
| 108 | |
| 109 | onBeforeMount(() => { |
| 110 | getAgents() |
| 111 | }) |
| 112 | </script> |