main
vue 115 lines 2.77 KB
Raw
1 <template>
2 <div class="customer-info flex flex-col gap-4">
3 <div v-if="editing">
4 <CustomerForm :customer lock-code @submitted="submitted">
5 <template #additionalActions>
6 <n-button @click="editing = false">Close</n-button>
7 </template>
8 </CustomerForm>
9 </div>
10 <template v-else>
11 <div class="flex items-center justify-between gap-4">
12 <n-button size="small" :disabled="loadingDelete" @click="editing = true">
13 <template #icon>
14 <Icon :name="EditIcon" :size="14" />
15 </template>
16 Edit
17 </n-button>
18 <n-button size="small" type="error" ghost :loading="loadingDelete" @click="handleDelete">
19 <template #icon>
20 <Icon :name="DeleteIcon" :size="15" />
21 </template>
22 Delete Customer
23 </n-button>
24 </div>
25
26 <div class="grid-auto-fit-200 grid gap-2">
27 <CardKV v-for="(value, key) of customer" :key>
28 <template #key>
29 {{ key }}
30 </template>
31 <template #value>
32 {{ value || "-" }}
33 </template>
34 </CardKV>
35 </div>
36 </template>
37 </div>
38 </template>
39
40 <script setup lang="ts">
41 import type { Customer } from "@/types/customers.d"
42 import { NButton, useDialog, useMessage } from "naive-ui"
43 import { h, ref, toRefs, watch } from "vue"
44 import Api from "@/api"
45 import CardKV from "@/components/common/cards/CardKV.vue"
46 import Icon from "@/components/common/Icon.vue"
47 import CustomerForm from "./CustomerForm.vue"
48
49 const props = defineProps<{
50 customer: Customer
51 }>()
52
53 const emit = defineEmits<{
54 (e: "update:loading", value: boolean): void
55 (e: "delete"): void
56 (e: "submitted", value: Customer): void
57 }>()
58
59 const { customer } = toRefs(props)
60
61 const EditIcon = "uil:edit-alt"
62 const DeleteIcon = "ph:trash"
63
64 const loadingDelete = ref(false)
65 const editing = ref(false)
66 const dialog = useDialog()
67 const message = useMessage()
68
69 function submitted(newData: Customer) {
70 emit("submitted", newData)
71 editing.value = false
72 }
73
74 function deleteCustomer() {
75 loadingDelete.value = true
76
77 Api.customers
78 .deleteCustomer(customer.value.customer_code)
79 .then(res => {
80 if (res.data.success) {
81 emit("delete")
82 } else {
83 message.warning(res.data?.message || "An error occurred. Please try again later.")
84 }
85 })
86 .catch(err => {
87 message.error(err.response?.data?.message || "An error occurred. Please try again later.")
88 })
89 .finally(() => {
90 loadingDelete.value = false
91 })
92 }
93
94 function handleDelete() {
95 dialog.warning({
96 title: "Confirm",
97 content: () =>
98 h("div", {
99 innerHTML: `Are you sure you want to delete the Customer: <strong>${customer.value.customer_code}</strong> ?`
100 }),
101 positiveText: "Yes I'm sure",
102 negativeText: "Cancel",
103 onPositiveClick: () => {
104 deleteCustomer()
105 },
106 onNegativeClick: () => {
107 message.info("Delete canceled")
108 }
109 })
110 }
111
112 watch(loadingDelete, val => {
113 emit("update:loading", val)
114 })
115 </script>