diff --git a/src/pages/Customer/Card/CustomerAddress.vue b/src/pages/Customer/Card/CustomerAddress.vue index b080fe457..77f6be0c2 100644 --- a/src/pages/Customer/Card/CustomerAddress.vue +++ b/src/pages/Customer/Card/CustomerAddress.vue @@ -3,6 +3,8 @@ import { ref } from 'vue'; import { useI18n } from 'vue-i18n'; import { useRoute, useRouter } from 'vue-router'; +import axios from 'axios'; + import FetchData from 'components/FetchData.vue'; import VnPaginate from 'src/components/ui/VnPaginate.vue'; @@ -10,6 +12,7 @@ const { t } = useI18n(); const route = useRoute(); const router = useRouter(); +const addresses = ref([]); const client = ref(null); const provincesLocation = ref([]); @@ -45,6 +48,11 @@ const addressFilter = { ], }; +const setRows = (data) => { + addresses.value = data; + sortAddresses(); +}; + const setProvince = (provinceFk) => { const result = provincesLocation.value.filter( (province) => province.id === provinceFk @@ -56,6 +64,24 @@ const isDefaultAddress = (address) => { return client?.value?.defaultAddressFk === address.id ? 1 : 0; }; +const setDefault = (address) => { + const url = `Clients/${route.params.id}`; + const payload = { defaultAddressFk: address.id }; + axios.patch(url, payload).then((res) => { + if (res.data) { + client.value.defaultAddressFk = res.data.defaultAddressFk; + sortAddresses(); + } + }); +}; + +const sortAddresses = () => { + if (!client.value || !addresses.value) return; + addresses.value = addresses.value.sort((a, b) => { + return isDefaultAddress(b) - isDefaultAddress(a); + }); +}; + const toCustomerAddressCreate = () => { router.push({ name: 'CustomerAddressCreate' }); }; @@ -72,6 +98,12 @@ const toCustomerAddressEdit = (addressId) => {