Merge pull request 'Address list query' (!122) from wbuezas/hedera-web-mindshore:feature/address-list-query into beta
gitea/hedera-web/pipeline/head There was a failure building this commit Details

Reviewed-on: #122
Reviewed-by: Javier Segarra <jsegarra@verdnatura.es>
This commit is contained in:
Javier Segarra 2025-03-21 14:06:01 +00:00
commit bc01a4bf53
2 changed files with 89 additions and 15 deletions

View File

@ -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>

View File

@ -5,11 +5,13 @@ import { useRouter } from 'vue-router';
import CardList from 'src/components/ui/CardList.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 { useVnConfirm } from 'src/composables/useVnConfirm.js';
import { useAppStore } from 'stores/app';
import { storeToRefs } from 'pinia';
import { useUserStore } from 'stores/user';
const router = useRouter();
const jApi = inject('jApi');
@ -17,7 +19,9 @@ const { notify } = useNotify();
const { t } = useI18n();
const { openConfirmationModal } = useVnConfirm();
const appStore = useAppStore();
const userStore = useUserStore();
const { isHeaderMounted } = storeToRefs(appStore);
const fetchAddressesRef = ref(null);
const addresses = ref([]);
const defaultAddress = ref(null);
@ -38,19 +42,6 @@ const getDefaultAddress = async () => {
}
};
const getActiveAddresses = async () => {
try {
addresses.value = await jApi.query(
`SELECT a.id, a.nickname, p.name province, a.postalCode, a.city, a.street, a.isActive
FROM myAddress a
LEFT JOIN vn.province p ON p.id = a.provinceFk
WHERE a.isActive`
);
} catch (error) {
console.error('Error getting active addresses:', error);
}
};
const changeDefaultAddress = async () => {
if (!clientId.value) return;
await jApi.execQuery(
@ -77,7 +68,7 @@ const removeAddress = async id => {
id
}
);
getActiveAddresses();
fetchAddressesRef.value.fetch();
notify(t('dataSaved'), 'positive');
} catch (error) {
console.error('Error removing address:', error);
@ -86,11 +77,28 @@ const removeAddress = async id => {
onMounted(async () => {
getDefaultAddress();
getActiveAddresses();
});
</script>
<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">
<QBtn
:label="t('addAddress')"