Add fetch data
gitea/hedera-web/pipeline/pr-beta This commit looks good
Details
gitea/hedera-web/pipeline/pr-beta This commit looks good
Details
This commit is contained in:
parent
6aee070616
commit
c20695b1ee
|
@ -0,0 +1,66 @@
|
||||||
|
<script setup>
|
||||||
|
import { onMounted, inject } from 'vue';
|
||||||
|
|
||||||
|
const $props = defineProps({
|
||||||
|
autoLoad: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false
|
||||||
|
},
|
||||||
|
url: {
|
||||||
|
type: String,
|
||||||
|
default: ''
|
||||||
|
},
|
||||||
|
filter: {
|
||||||
|
type: Object,
|
||||||
|
default: null
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
type: Object,
|
||||||
|
default: null
|
||||||
|
},
|
||||||
|
sortBy: {
|
||||||
|
type: String,
|
||||||
|
default: ''
|
||||||
|
},
|
||||||
|
limit: {
|
||||||
|
type: [String, Number],
|
||||||
|
default: ''
|
||||||
|
},
|
||||||
|
params: {
|
||||||
|
type: Object,
|
||||||
|
default: null
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const emit = defineEmits(['onFetch']);
|
||||||
|
const api = inject('api');
|
||||||
|
|
||||||
|
defineExpose({ fetch });
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
if ($props.autoLoad) {
|
||||||
|
await fetch();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
async function fetch(fetchFilter = {}) {
|
||||||
|
try {
|
||||||
|
const filter = { ...fetchFilter, ...$props.filter }; // eslint-disable-line vue/no-dupe-keys
|
||||||
|
if ($props.where && !fetchFilter.where) filter.where = $props.where;
|
||||||
|
if ($props.sortBy) filter.order = $props.sortBy;
|
||||||
|
if ($props.limit) filter.limit = $props.limit;
|
||||||
|
|
||||||
|
const { data } = await api.get($props.url, {
|
||||||
|
params: { filter: JSON.stringify(filter), ...$props.params }
|
||||||
|
});
|
||||||
|
|
||||||
|
emit('onFetch', data);
|
||||||
|
return data;
|
||||||
|
} catch (e) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<template>
|
||||||
|
<template></template>
|
||||||
|
</template>
|
|
@ -1,10 +1,11 @@
|
||||||
<script setup>
|
<script setup>
|
||||||
import { useI18n } from 'vue-i18n';
|
import { useI18n } from 'vue-i18n';
|
||||||
import { ref, onMounted, inject, watch } from 'vue';
|
import { ref, onMounted, inject } from 'vue';
|
||||||
import { useRouter } from 'vue-router';
|
import { useRouter } from 'vue-router';
|
||||||
|
|
||||||
import CardList from 'src/components/ui/CardList.vue';
|
import CardList from 'src/components/ui/CardList.vue';
|
||||||
import VnList from 'src/components/ui/VnList.vue';
|
import VnList from 'src/components/ui/VnList.vue';
|
||||||
|
import FetchData from 'src/components/common/FetchData.vue';
|
||||||
|
|
||||||
import useNotify from 'src/composables/useNotify.js';
|
import useNotify from 'src/composables/useNotify.js';
|
||||||
import { useVnConfirm } from 'src/composables/useVnConfirm.js';
|
import { useVnConfirm } from 'src/composables/useVnConfirm.js';
|
||||||
|
@ -13,7 +14,6 @@ import { storeToRefs } from 'pinia';
|
||||||
import { useUserStore } from 'stores/user';
|
import { useUserStore } from 'stores/user';
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const api = inject('api');
|
|
||||||
const jApi = inject('jApi');
|
const jApi = inject('jApi');
|
||||||
const { notify } = useNotify();
|
const { notify } = useNotify();
|
||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
|
@ -21,6 +21,7 @@ const { openConfirmationModal } = useVnConfirm();
|
||||||
const appStore = useAppStore();
|
const appStore = useAppStore();
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
const { isHeaderMounted } = storeToRefs(appStore);
|
const { isHeaderMounted } = storeToRefs(appStore);
|
||||||
|
const fetchAddressesRef = ref(null);
|
||||||
|
|
||||||
const addresses = ref([]);
|
const addresses = ref([]);
|
||||||
const defaultAddress = ref(null);
|
const defaultAddress = ref(null);
|
||||||
|
@ -41,30 +42,6 @@ const getDefaultAddress = async () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getActiveAddresses = async () => {
|
|
||||||
try {
|
|
||||||
const filter = {
|
|
||||||
where: { clientFk: userStore.user.id, isActive: true },
|
|
||||||
fields: [
|
|
||||||
'id',
|
|
||||||
'nickname',
|
|
||||||
'postalCode',
|
|
||||||
'city',
|
|
||||||
'street',
|
|
||||||
'isActive'
|
|
||||||
]
|
|
||||||
};
|
|
||||||
const { data } = await api.get('Addresses', {
|
|
||||||
params: {
|
|
||||||
filter: JSON.stringify(filter)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
addresses.value = data;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error getting active addresses:', error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const changeDefaultAddress = async () => {
|
const changeDefaultAddress = async () => {
|
||||||
if (!clientId.value) return;
|
if (!clientId.value) return;
|
||||||
await jApi.execQuery(
|
await jApi.execQuery(
|
||||||
|
@ -91,7 +68,7 @@ const removeAddress = async id => {
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
getActiveAddresses();
|
fetchAddressesRef.value.fetch();
|
||||||
notify(t('dataSaved'), 'positive');
|
notify(t('dataSaved'), 'positive');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error removing address:', error);
|
console.error('Error removing address:', error);
|
||||||
|
@ -101,19 +78,27 @@ const removeAddress = async id => {
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
getDefaultAddress();
|
getDefaultAddress();
|
||||||
});
|
});
|
||||||
|
|
||||||
watch(
|
|
||||||
() => userStore?.user?.id,
|
|
||||||
async userId => {
|
|
||||||
if (userId) {
|
|
||||||
getActiveAddresses();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{ immediate: true }
|
|
||||||
);
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
<FetchData
|
||||||
|
v-if="userStore?.user?.id"
|
||||||
|
ref="fetchAddressesRef"
|
||||||
|
url="Addresses"
|
||||||
|
:filter="{
|
||||||
|
where: { clientFk: userStore.user.id, isActive: true },
|
||||||
|
fields: [
|
||||||
|
'id',
|
||||||
|
'nickname',
|
||||||
|
'postalCode',
|
||||||
|
'city',
|
||||||
|
'street',
|
||||||
|
'isActive'
|
||||||
|
]
|
||||||
|
}"
|
||||||
|
auto-load
|
||||||
|
@on-fetch="data => (addresses = data)"
|
||||||
|
/>
|
||||||
<Teleport v-if="isHeaderMounted" to="#actions">
|
<Teleport v-if="isHeaderMounted" to="#actions">
|
||||||
<QBtn
|
<QBtn
|
||||||
:label="t('addAddress')"
|
:label="t('addAddress')"
|
||||||
|
|
Loading…
Reference in New Issue