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> <script setup>
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import { ref, computed } from 'vue'; import { ref, computed, onMounted } from 'vue';
import FetchData from 'components/FetchData.vue';
import { useState } from 'src/composables/useState'; import { useState } from 'src/composables/useState';
import axios from 'axios'; import axios from 'axios';
@ -29,12 +27,6 @@ const { t } = useI18n();
const popupProxyRef = ref(null); const popupProxyRef = ref(null);
const user = state.getUser(); const user = state.getUser();
const initialUserConfigViewData = ref(null); const initialUserConfigViewData = ref(null);
const userConfigFilter = {
where: {
tableCode: $props.tableCode,
userFk: user.id,
},
};
const formattedCols = ref([]); const formattedCols = ref([]);
@ -43,37 +35,96 @@ const areAllChecksMarked = computed(() => {
}); });
const setUserConfigViewData = (data) => { const setUserConfigViewData = (data) => {
// initialUserConfigViewData.value = data; if (!data) return;
// if (data.length === 0) return; formattedCols.value = $props.allColumns.map((col) => {
// 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
// // Importante: El name de las columnas de la tabla debe conincidir con el name de las variables que devuelve la view config const obj = {
// const obj = { name: col,
// name: col, active: data[col],
// active: data[0].configuration[col], };
// }; return obj;
// return obj; });
// }); emitSavedConfig();
// emitSavedConfig();
}; };
const toggleMarkAll = (val) => { const toggleMarkAll = (val) => {
formattedCols.value.forEach((col) => (col.active = val)); formattedCols.value.forEach((col) => (col.active = val));
}; };
const saveConfig = async () => { const fetchViewConfigData = async () => {
try { try {
const data = { const userConfigFilter = {
id: initialUserConfigViewData.value[0].id, where: {
userFk: 9, tableCode: $props.tableCode,
tableCode: $props.tableCode, userFk: user.id,
configuration: {}, },
}; };
formattedCols.value.forEach((col) => { const userViewConfigResponse = await axios.get('UserConfigViews', {
data.configuration[col.name] = col.active; 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(); emitSavedConfig();
popupProxyRef.value.hide(); popupProxyRef.value.hide();
} catch (err) { } 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 emitSavedConfig = () => {
const filteredCols = formattedCols.value.filter((col) => col.active); const filteredCols = formattedCols.value.filter((col) => col.active);
const mappedCols = filteredCols.map((col) => col.name); const mappedCols = filteredCols.map((col) => col.name);
emit('onConfigSaved', mappedCols); emit('onConfigSaved', mappedCols);
}; };
onMounted(async () => {
await fetchViewConfigData();
});
</script> </script>
<template> <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"> <QBtn color="primary" icon="view_column">
<QPopupProxy ref="popupProxyRef"> <QPopupProxy ref="popupProxyRef">
<QCard class="column q-pa-md"> <QCard class="column q-pa-md">