main
vue 82 lines 1.84 KB
Raw
1 <template>
2 <n-button :size :type quaternary class="w-full! justify-start!" :loading @click="handleDelete()">
3 <template #icon>
4 <Icon :name="DeleteIcon" :size="14" />
5 </template>
6 Delete User
7 </n-button>
8 </template>
9
10 <script setup lang="ts">
11 import type { ButtonSize, ButtonType } from "naive-ui"
12 import type { User } from "@/types/user"
13 import { NButton, useDialog, useMessage } from "naive-ui"
14 import { computed, h, ref, watch } from "vue"
15 import Api from "@/api"
16 import Icon from "@/components/common/Icon.vue"
17
18 const {
19 type = "error",
20 size,
21 user
22 } = defineProps<{
23 user?: User
24 size?: ButtonSize
25 type?: ButtonType
26 }>()
27
28 const emit = defineEmits<{
29 (e: "success"): void
30 (e: "loading", value: boolean): void
31 }>()
32
33 const DeleteIcon = "ph:trash"
34 const dialog = useDialog()
35 const message = useMessage()
36 const username = computed(() => user?.username || "")
37 const userId = computed(() => user?.id || 0)
38 const loading = ref(false)
39
40 function deleteCustomer() {
41 loading.value = true
42
43 Api.auth
44 .delete(userId.value)
45 .then(res => {
46 if (res.data.success) {
47 emit("success")
48 message.success(res.data?.message || "User was successfully deleted.")
49 } else {
50 message.warning(res.data?.message || "An error occurred. Please try again later.")
51 }
52 })
53 .catch(err => {
54 message.error(err.response?.data?.message || "An error occurred. Please try again later.")
55 })
56 .finally(() => {
57 loading.value = false
58 })
59 }
60
61 function handleDelete() {
62 dialog.warning({
63 title: "Confirm",
64 content: () =>
65 h("div", {
66 innerHTML: `Are you sure you want to delete the User: <strong>${username.value}</strong> ?`
67 }),
68 positiveText: "Yes I'm sure",
69 negativeText: "Cancel",
70 onPositiveClick: () => {
71 deleteCustomer()
72 },
73 onNegativeClick: () => {
74 message.info("Delete canceled")
75 }
76 })
77 }
78
79 watch(loading, val => {
80 emit("loading", val)
81 })
82 </script>