0
0
Fork 0

agregar data por default en tabla extended list

This commit is contained in:
William Buezas 2024-01-03 13:49:08 -03:00
parent 24fdc1ff97
commit 142bac7a43
1 changed files with 84 additions and 58 deletions

View File

@ -1,8 +1,6 @@
<script setup>
import { useI18n } from 'vue-i18n';
import { ref, computed } from 'vue';
import FetchData from 'components/FetchData.vue';
import { ref, computed, onMounted } from 'vue';
import { useState } from 'src/composables/useState';
import axios from 'axios';
@ -29,12 +27,6 @@ const { t } = useI18n();
const popupProxyRef = ref(null);
const user = state.getUser();
const initialUserConfigViewData = ref(null);
const userConfigFilter = {
where: {
tableCode: $props.tableCode,
userFk: user.id,
},
};
const formattedCols = ref([]);
@ -43,37 +35,96 @@ const areAllChecksMarked = computed(() => {
});
const setUserConfigViewData = (data) => {
// initialUserConfigViewData.value = data;
// if (data.length === 0) return;
// formattedCols.value = $props.allColumns.map((col) => {
// // Importante: El name de las columnas de la tabla debe conincidir con el name de las variables que devuelve la view config
// const obj = {
// name: col,
// active: data[0].configuration[col],
// };
// return obj;
// });
// emitSavedConfig();
if (!data) return;
formattedCols.value = $props.allColumns.map((col) => {
// Importante: El name de las columnas de la tabla debe conincidir con el name de las variables que devuelve la view config
const obj = {
name: col,
active: data[col],
};
return obj;
});
emitSavedConfig();
};
const toggleMarkAll = (val) => {
formattedCols.value.forEach((col) => (col.active = val));
};
const saveConfig = async () => {
const fetchViewConfigData = async () => {
try {
const data = {
id: initialUserConfigViewData.value[0].id,
userFk: 9,
tableCode: $props.tableCode,
configuration: {},
const userConfigFilter = {
where: {
tableCode: $props.tableCode,
userFk: user.id,
},
};
formattedCols.value.forEach((col) => {
data.configuration[col.name] = col.active;
const userViewConfigResponse = await axios.get('UserConfigViews', {
params: { filter: userConfigFilter },
});
await axios.patch('UserConfigViews', data);
if (userViewConfigResponse.data && userViewConfigResponse.data.length > 0) {
initialUserConfigViewData.value = userViewConfigResponse.data[0];
setUserConfigViewData(userViewConfigResponse.data[0].configuration);
return;
}
const defaultConfigFilter = {
where: {
tableCode: $props.tableCode,
},
};
const defaultViewConfigResponse = await axios.get('DefaultViewConfigs', {
params: { filter: defaultConfigFilter },
});
if (defaultViewConfigResponse.data && defaultViewConfigResponse.data.length > 0) {
setUserConfigViewData(defaultViewConfigResponse.data[0].columns);
return;
}
} catch (err) {
console.err('Error fetching config view data');
}
};
const saveConfig = async () => {
try {
const params = {};
const configuration = {};
formattedCols.value.forEach((col) => {
configuration[col.name] = col.active;
});
// Si existe una view config del usuario hacemos un update si no la creamos
if (initialUserConfigViewData.value) {
params.updates = [
{
data: {
configuration: configuration,
},
where: {
id: initialUserConfigViewData.value.id,
},
},
];
} else {
params.creates = [
{
userFk: user.value.id,
tableCode: $props.tableCode,
tableConfig: $props.tableCode,
configuration: configuration,
},
];
}
const response = await axios.post('UserConfigViews/crud', params);
if (response.data && response.data[0]) {
initialUserConfigViewData.value = response.data[0];
}
emitSavedConfig();
popupProxyRef.value.hide();
} catch (err) {
@ -81,42 +132,17 @@ const saveConfig = async () => {
}
};
const setDefaultViewConfig = (data) => {
initialUserConfigViewData.value = data;
if (data.length === 0) return;
formattedCols.value = $props.allColumns.map((col) => {
// Importante: El name de las columnas de la tabla debe conincidir con el name de las variables que devuelve la view config
const obj = {
name: col,
active: data[0].columns[col],
};
return obj;
});
console.log('formattedCols:: ', formattedCols);
emitSavedConfig();
};
const emitSavedConfig = () => {
const filteredCols = formattedCols.value.filter((col) => col.active);
const mappedCols = filteredCols.map((col) => col.name);
emit('onConfigSaved', mappedCols);
};
onMounted(async () => {
await fetchViewConfigData();
});
</script>
<template>
<!-- <fetch-data
v-if="user"
url="UserConfigViews"
:filter="userConfigFilter"
@on-fetch="(data) => setUserConfigViewData(data)"
auto-load
/> -->
<fetch-data
v-if="user"
url="DefaultViewConfigs"
:filter="{ where: { tableCode: tableCode } }"
@on-fetch="(data) => setDefaultViewConfig(data)"
auto-load
/>
<QBtn color="primary" icon="view_column">
<QPopupProxy ref="popupProxyRef">
<QCard class="column q-pa-md">