diff --git a/src/components/common/TableVisibleColumns.vue b/src/components/common/TableVisibleColumns.vue index 7defdb0de..d9a1a2004 100644 --- a/src/components/common/TableVisibleColumns.vue +++ b/src/components/common/TableVisibleColumns.vue @@ -50,41 +50,35 @@ const toggleMarkAll = (val) => { formattedCols.value.forEach((col) => (col.active = val)); }; +const getConfig = async (url, filter) => { + const response = await axios.get(url, { + params: { filter: filter }, + }); + return response.data && response.data.length > 0 ? response.data[0] : null; +}; + const fetchViewConfigData = async () => { try { const userConfigFilter = { - where: { - tableCode: $props.tableCode, - userFk: user.id, - }, + where: { tableCode: $props.tableCode, userFk: user.id }, }; + const userConfig = await getConfig('UserConfigViews', userConfigFilter); - const userViewConfigResponse = await axios.get('UserConfigViews', { - params: { filter: userConfigFilter }, - }); - - if (userViewConfigResponse.data && userViewConfigResponse.data.length > 0) { - initialUserConfigViewData.value = userViewConfigResponse.data[0]; - setUserConfigViewData(userViewConfigResponse.data[0].configuration); + if (userConfig) { + initialUserConfigViewData.value = userConfig; + setUserConfigViewData(userConfig.configuration); return; } - const defaultConfigFilter = { - where: { - tableCode: $props.tableCode, - }, - }; + const defaultConfigFilter = { where: { tableCode: $props.tableCode } }; + const defaultConfig = await getConfig('DefaultViewConfigs', defaultConfigFilter); - const defaultViewConfigResponse = await axios.get('DefaultViewConfigs', { - params: { filter: defaultConfigFilter }, - }); - - if (defaultViewConfigResponse.data && defaultViewConfigResponse.data.length > 0) { - setUserConfigViewData(defaultViewConfigResponse.data[0].columns); + if (defaultConfig) { + setUserConfigViewData(defaultConfig.columns); return; } } catch (err) { - console.err('Error fetching config view data'); + console.err('Error fetching config view data', err); } }; @@ -127,17 +121,17 @@ const saveConfig = async () => { } emitSavedConfig(); notify('globals.dataSaved', 'positive'); - popupProxyRef.value.hide(); } catch (err) { - console.error('Error saving user view config'); + console.error('Error saving user view config', err); } }; const emitSavedConfig = () => { - const filteredCols = formattedCols.value.filter((col) => col.active); - const mappedCols = filteredCols.map((col) => col.name); - emit('onConfigSaved', mappedCols); + const activeColumns = formattedCols.value + .filter((col) => col.active) + .map((col) => col.name); + emit('onConfigSaved', activeColumns); }; onMounted(async () => {