0
0
Fork 0

fix: refs #4774 finalize

This commit is contained in:
Carlos Satorres 2024-10-24 12:57:17 +02:00
parent 42f975b025
commit 82b53ea1aa
3 changed files with 75 additions and 44 deletions

View File

@ -183,6 +183,12 @@ watch(
{ immediate: true } { immediate: true }
); );
watch(
() => $props.create,
(value) => (createForm.value = value),
{ immediate: true }
);
watch( watch(
() => route.query[$props.searchUrl], () => route.query[$props.searchUrl],
(val) => setUserParams(val) (val) => setUserParams(val)
@ -535,6 +541,7 @@ function handleScroll() {
<QBtn <QBtn
v-for="(btn, index) of col.actions" v-for="(btn, index) of col.actions"
v-show="btn.show ? btn.show(row) : true" v-show="btn.show ? btn.show(row) : true"
v-bind="row.attrs"
:key="index" :key="index"
:title="btn.title" :title="btn.title"
:icon="btn.icon" :icon="btn.icon"
@ -548,6 +555,7 @@ function handleScroll() {
(btn.show && btn.show(row)) ?? true ? 'visible' : 'hidden' (btn.show && btn.show(row)) ?? true ? 'visible' : 'hidden'
}`" }`"
@click="btn.action(row)" @click="btn.action(row)"
:disable="btn.disable ? btn.disable(row) : true"
/> />
</QTd> </QTd>
</template> </template>

View File

@ -128,7 +128,7 @@ const addFilter = async (filter, params) => {
async function fetch(params) { async function fetch(params) {
useArrayData(props.dataKey, params); useArrayData(props.dataKey, params);
arrayData.reset(['filter.skip', 'skip']); arrayData.reset(['filter.skip', 'skip', 'page']);
await arrayData.fetch({ append: false }); await arrayData.fetch({ append: false });
if (!store.hasMoreData) isLoading.value = false; if (!store.hasMoreData) isLoading.value = false;

View File

@ -7,6 +7,7 @@ import VnSelect from 'src/components/common/VnSelect.vue';
import axios from 'axios'; import axios from 'axios';
import VnSubToolbar from 'src/components/ui/VnSubToolbar.vue'; import VnSubToolbar from 'src/components/ui/VnSubToolbar.vue';
import FetchData from 'src/components/FetchData.vue'; import FetchData from 'src/components/FetchData.vue';
import VnInput from 'src/components/common/VnInput.vue';
const { t } = useI18n(); const { t } = useI18n();
const columns = computed(() => [ const columns = computed(() => [
@ -18,19 +19,13 @@ const columns = computed(() => [
}, },
{ {
align: 'left', align: 'left',
name: field, name: 'en',
label: t('defaultLang(en)'), label: t('defaultLang(en)'),
create: true,
}, },
{ {
align: 'left', align: 'left',
name: 'secondLang', name: lang,
label: t('secondLang'), label: lang,
component: 'input',
isEditable: true,
attrs: {
clearable: false,
},
}, },
{ {
align: 'right', align: 'right',
@ -42,6 +37,7 @@ const columns = computed(() => [
icon: 'save', icon: 'save',
action: (row) => upsertI18n(row), action: (row) => upsertI18n(row),
isPrimary: true, isPrimary: true,
disable: (row) => !row.hasChanges,
}, },
], ],
}, },
@ -54,47 +50,52 @@ const lang = ref('es');
const primaryKey = computed(() => table.value.primaryKey); const primaryKey = computed(() => table.value.primaryKey);
const field = computed(() => table.value.field); const field = computed(() => table.value.field);
const table = ref(); const table = ref();
const originalData = ref(); const originalData = ref([]);
const url = computed(() => table.value?.tableName + 's'); const url = computed(() => table.value?.tableName + 's');
async function loadTable(changeLang) { async function loadTable(data) {
console.log('table: ', tableRef.value.CrudModelRef.formData); if (data) {
const data = tableRef.value.CrudModelRef.formData; originalData.value = [];
if (!changeLang) originalData.value = data; tableRef.value.CrudModelRef.formData = [];
if (!originalData.value) return; const newData = {};
const en = originalData.value.filter((d) => d.lang === 'en'); for (const translation of data) {
for (const translation of en) { if (!newData[translation[primaryKey.value]])
translation['secondLang'] = originalData.value.find( newData[translation[primaryKey.value]] = {};
(d) => newData[translation[primaryKey.value]][translation.lang] =
d[primaryKey.value] == translation[primaryKey.value] && translation[field.value];
d['lang'] == lang.value }
)?.[field.value];
}
console.log('data: ', en);
await tableRef.value.CrudModelRef.resetData(en); for (const currentData in newData) {
originalData.value.push({
[primaryKey.value]: currentData,
...newData[currentData],
});
tableRef.value.CrudModelRef.formData.push({
[primaryKey.value]: currentData,
...newData[currentData],
});
}
return;
}
tableRef.value.CrudModelRef.formData = JSON.parse(JSON.stringify(originalData.value));
} }
function resetOriginalData() { function resetOriginalData() {
originalData.value = null; originalData.value = null;
} }
function upsertI18n(data) { async function upsertI18n(data) {
const newData = { ...data }; await axios.patch(url.value, {
newData[field.value] = newData.secondLang; [field.value]: data[lang.value],
newData.lang = lang.value; [primaryKey.value]: data[primaryKey.value],
delete newData.secondLang; lang: lang.value,
delete newData.$index; });
axios.patch(url.value, newData);
}
function deleteI18n(data) { const index = originalData.value.findIndex(
// const newData = { ...data }; (t) => t[primaryKey.value] == data[primaryKey.value]
// newData[field.value] = newData.secondLang; );
// newData.lang = lang.value; originalData.value[index] = data;
// delete newData.secondLang; data.hasChanges = false;
// delete newData.$index;
// axios.deleteById(url.value, newData);
} }
</script> </script>
@ -122,7 +123,7 @@ function deleteI18n(data) {
:options="langs" :options="langs"
option-label="code" option-label="code"
option-value="code" option-value="code"
@update:model-value="loadTable(true)" @update:model-value="loadTable()"
/> />
</template> </template>
</VnSubToolbar> </VnSubToolbar>
@ -131,6 +132,7 @@ function deleteI18n(data) {
data-key="translations" data-key="translations"
:url="url" :url="url"
:columns="columns" :columns="columns"
:filter="{ limit: 50 }"
:create="{ :create="{
urlCreate: url, urlCreate: url,
title: 'Create translation', title: 'Create translation',
@ -138,9 +140,30 @@ function deleteI18n(data) {
formInitialData: { lang: 'en' }, formInitialData: { lang: 'en' },
}" }"
:right-search="false" :right-search="false"
@on-fetch="loadTable()" @on-fetch="(data) => loadTable(data)"
:save-fn="upsertI18n" :save-fn="upsertI18n"
/> :search-url="false"
>
<template #[`column-${lang}`]="{ row }">
<VnInput
v-model="row[lang]"
:clearable="false"
@keyup.enter="upsertI18n(row)"
@update:model-value="
(value) => {
row.hasChanges =
value !=
originalData.find((o) => o[primaryKey] == row[primaryKey])?.[
lang
];
}
"
/>
</template>
<template #more-create-dialog="{ data }">
<VnInput v-model="data[field]" :label="field" />
</template>
</VnTable>
</template> </template>
<i18n> <i18n>