main
vue 93 lines 2.35 KB
Raw
1 <template>
2 <div class="flex items-center gap-2">
3 <code v-if="!editing" class="text-primary cursor-pointer" @click="edit()">
4 {{ velociraptorId }}
5 <Icon :name="loading ? LoadingIcon : EditIcon" :size="13" class="relative top-0.5" />
6 </code>
7 <n-input-group v-else>
8 <n-input
9 v-model:value="velociraptorIdModel"
10 size="small"
11 :disabled="loading"
12 placeholder="Input velociraptor_id"
13 >
14 <template #suffix>
15 <Icon
16 v-if="!loading"
17 :name="CloseIcon"
18 :size="13"
19 class="cursor-pointer"
20 @click="editing = false"
21 />
22 </template>
23 </n-input>
24 <n-button type="primary" ghost :loading size="small" @click="updateAgent()">
25 <span v-if="!loading">Save</span>
26 </n-button>
27 </n-input-group>
28 </div>
29 </template>
30
31 <script setup lang="ts">
32 import type { Agent } from "@/types/agents.d"
33 import { NButton, NInput, NInputGroup, useMessage } from "naive-ui"
34 import { onBeforeMount, ref, toRefs } from "vue"
35 import Api from "@/api"
36 import Icon from "@/components/common/Icon.vue"
37
38 const props = defineProps<{
39 agent: Agent
40 }>()
41
42 const emit = defineEmits<{
43 (e: "updated", value: string): void
44 }>()
45
46 const velociraptorId = defineModel<string>("velociraptorId", { default: "" })
47
48 const { agent } = toRefs(props)
49
50 const LoadingIcon = "eos-icons:loading"
51 const EditIcon = "uil:edit-alt"
52 const CloseIcon = "carbon:close-filled"
53
54 const loading = ref(false)
55 const editing = ref(false)
56 const message = useMessage()
57 const velociraptorIdModel = ref<string | null>("")
58
59 function edit() {
60 editing.value = true
61 velociraptorIdModel.value = velociraptorId.value
62 }
63
64 function updateAgent() {
65 if (agent.value.agent_id) {
66 loading.value = true
67
68 const velociraptorIdPayload = velociraptorIdModel.value || ""
69
70 Api.agents
71 .updateAgent(agent.value.agent_id.toString(), { velociraptor_id: velociraptorIdPayload })
72 .then(res => {
73 if (res.data.success) {
74 velociraptorId.value = velociraptorIdPayload
75 editing.value = false
76 emit("updated", velociraptorIdPayload)
77 } else {
78 message.warning(res.data?.message || "An error occurred. Please try again later.")
79 }
80 })
81 .catch(err => {
82 message.error(err.response?.data?.message || "An error occurred. Please try again later.")
83 })
84 .finally(() => {
85 loading.value = false
86 })
87 }
88 }
89
90 onBeforeMount(() => {
91 velociraptorIdModel.value = velociraptorId.value || ""
92 })
93 </script>