useFetchData composable
gitea/salix-front/pipeline/pr-dev There was a failure building this commit
Details
gitea/salix-front/pipeline/pr-dev There was a failure building this commit
Details
This commit is contained in:
parent
21aba3bb2e
commit
464cf47a39
|
@ -1,6 +1,7 @@
|
||||||
<script setup>
|
<script setup>
|
||||||
import { onMounted } from 'vue';
|
import { onMounted } from 'vue';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import { useFetchData } from 'src/composables/useFetchData';
|
||||||
|
|
||||||
const $props = defineProps({
|
const $props = defineProps({
|
||||||
autoLoad: {
|
autoLoad: {
|
||||||
|
@ -38,7 +39,7 @@ defineExpose({ fetch });
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
if ($props.autoLoad) {
|
if ($props.autoLoad) {
|
||||||
await fetch();
|
await useFetchData(...$props).fetch();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref, toRefs, computed, watch } from 'vue';
|
import { ref, toRefs, computed, watch, h } from 'vue';
|
||||||
import { onMounted } from 'vue';
|
import { onMounted } from 'vue';
|
||||||
import { useI18n } from 'vue-i18n';
|
import { useI18n } from 'vue-i18n';
|
||||||
import FetchData from 'src/components/FetchData.vue';
|
import FetchData from 'src/components/FetchData.vue';
|
||||||
const emit = defineEmits(['update:modelValue', 'update:options']);
|
const emit = defineEmits(['update:modelValue', 'update:options']);
|
||||||
import { useArrayData } from 'src/composables/useArrayData';
|
import { useArrayData } from 'src/composables/useArrayData';
|
||||||
|
import { useFetchData } from 'src/composables/useFetchData';
|
||||||
|
|
||||||
const $props = defineProps({
|
const $props = defineProps({
|
||||||
modelValue: {
|
modelValue: {
|
||||||
|
@ -66,11 +67,29 @@ const requiredFieldRule = (val) => val ?? t('globals.fieldRequired');
|
||||||
|
|
||||||
const { optionLabel, optionValue, optionFilter, options, modelValue } = toRefs($props);
|
const { optionLabel, optionValue, optionFilter, options, modelValue } = toRefs($props);
|
||||||
const myOptions = ref([]);
|
const myOptions = ref([]);
|
||||||
// const myOptionsFiltered = ref([]);
|
|
||||||
const myOptionsOriginal = ref([]);
|
const myOptionsOriginal = ref([]);
|
||||||
const vnSelectRef = ref();
|
const vnSelectRef = ref();
|
||||||
const dataRef = ref();
|
const dataRef = ref(
|
||||||
|
useFetchData({
|
||||||
|
url: $props.url,
|
||||||
|
where: $props.where,
|
||||||
|
limit: $props.limit,
|
||||||
|
sortBy: $props.sortBy,
|
||||||
|
fields: $props.fields,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
// const fetchDataComponent = ref(null);
|
||||||
|
// fetchDataComponent.value = h(FetchData, {
|
||||||
|
// url: $props.url,
|
||||||
|
// where: $props.where,
|
||||||
|
// limit: $props.limit,
|
||||||
|
// sortBy: $props.sortBy,
|
||||||
|
// fields: $props.fields,
|
||||||
|
// onFetch: (data) => {
|
||||||
|
// setOptions(data); // Tu función para manejar los datos
|
||||||
|
// },
|
||||||
|
// });
|
||||||
|
// const dataRef = ref(null);
|
||||||
const selectValue = computed({
|
const selectValue = computed({
|
||||||
get() {
|
get() {
|
||||||
return $props.modelValue;
|
return $props.modelValue;
|
||||||
|
@ -193,6 +212,8 @@ async function onScroll(scrollEv) {
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
<!-- <component :is="fetchDataComponent" ref="fetchDataRef" />
|
||||||
|
|
||||||
<FetchData
|
<FetchData
|
||||||
v-if="useURL"
|
v-if="useURL"
|
||||||
ref="dataRef"
|
ref="dataRef"
|
||||||
|
@ -202,7 +223,7 @@ async function onScroll(scrollEv) {
|
||||||
:limit="limit"
|
:limit="limit"
|
||||||
:sort-by="sortBy"
|
:sort-by="sortBy"
|
||||||
:fields="fields"
|
:fields="fields"
|
||||||
/>
|
/> -->
|
||||||
<QSelect
|
<QSelect
|
||||||
:loading="isLoading"
|
:loading="isLoading"
|
||||||
@virtual-scroll="onScroll"
|
@virtual-scroll="onScroll"
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
import { ref } from 'vue';
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
export function useFetchData(props) {
|
||||||
|
const data = ref(null);
|
||||||
|
|
||||||
|
async function fetch(fetchFilter = {}) {
|
||||||
|
try {
|
||||||
|
const filter = Object.assign(fetchFilter, props.filter);
|
||||||
|
if (props.where && !fetchFilter.where) filter.where = props.where;
|
||||||
|
if (props.sortBy) filter.order = props.sortBy;
|
||||||
|
if (props.limit) filter.limit = props.limit;
|
||||||
|
|
||||||
|
const response = await axios.get(props.url, {
|
||||||
|
params: { filter: JSON.stringify(filter), ...props.params },
|
||||||
|
});
|
||||||
|
|
||||||
|
data.value = response.data;
|
||||||
|
return response.data;
|
||||||
|
} catch (e) {
|
||||||
|
// Manejo de errores
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
data,
|
||||||
|
fetch,
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue