This commit is contained in:
parent
1bc26c3e74
commit
ea2620662b
|
@ -1,6 +1,6 @@
|
||||||
<script setup>
|
<script setup>
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { computed, ref, watch } from 'vue';
|
import { computed, nextTick, ref, watch } from 'vue';
|
||||||
import { useRouter, onBeforeRouteLeave } from 'vue-router';
|
import { useRouter, onBeforeRouteLeave } from 'vue-router';
|
||||||
import { useI18n } from 'vue-i18n';
|
import { useI18n } from 'vue-i18n';
|
||||||
import { useQuasar } from 'quasar';
|
import { useQuasar } from 'quasar';
|
||||||
|
|
|
@ -4,12 +4,14 @@ import { computed } from 'vue';
|
||||||
import { useI18n } from 'vue-i18n';
|
import { useI18n } from 'vue-i18n';
|
||||||
import VnTable from 'src/components/VnTable/VnTable.vue';
|
import VnTable from 'src/components/VnTable/VnTable.vue';
|
||||||
import VnSelect from 'src/components/common/VnSelect.vue';
|
import VnSelect from 'src/components/common/VnSelect.vue';
|
||||||
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';
|
import VnInput from 'src/components/common/VnInput.vue';
|
||||||
import { useVnConfirm } from 'composables/useVnConfirm';
|
import { useVnConfirm } from 'composables/useVnConfirm';
|
||||||
|
import { useArrayData } from 'src/composables/useArrayData';
|
||||||
|
import axios from 'axios';
|
||||||
const { openConfirmationModal } = useVnConfirm();
|
const { openConfirmationModal } = useVnConfirm();
|
||||||
|
useArrayData;
|
||||||
|
|
||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
const columns = computed(() => [
|
const columns = computed(() => [
|
||||||
|
@ -36,70 +38,51 @@ const columns = computed(() => [
|
||||||
label: lang,
|
label: lang,
|
||||||
component: 'input',
|
component: 'input',
|
||||||
},
|
},
|
||||||
{
|
|
||||||
align: 'right',
|
|
||||||
label: '',
|
|
||||||
name: 'tableActions',
|
|
||||||
actions: [
|
|
||||||
{
|
|
||||||
title: t('View Summary'),
|
|
||||||
icon: 'save',
|
|
||||||
action: (row) => upsertI18n(row),
|
|
||||||
isPrimary: true,
|
|
||||||
disable: (row) => !row.hasChanges,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: t('globals.delete'),
|
|
||||||
icon: 'delete',
|
|
||||||
isPrimary: true,
|
|
||||||
action: (row) =>
|
|
||||||
openConfirmationModal(
|
|
||||||
t('You are going to delete this ticket purchase request'),
|
|
||||||
t(
|
|
||||||
'This ticket will be removed from ticket purchase requests! Continue anyway?'
|
|
||||||
),
|
|
||||||
() => removeLine(row.id)
|
|
||||||
),
|
|
||||||
|
|
||||||
disable: (row) => row.id,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const tableRef = ref({});
|
const tableRef = ref({});
|
||||||
const langs = ref([]);
|
const langs = ref([]);
|
||||||
const tables = ref([]);
|
const tables = ref([]);
|
||||||
const lang = ref('es');
|
const lang = ref('ca');
|
||||||
const primaryKey = computed(() => table.value.primaryKey);
|
|
||||||
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 selected = ref([]);
|
||||||
|
const PRIMARY_LANG = 'en';
|
||||||
|
|
||||||
|
const primaryKey = computed(() => table.value?.primaryKey);
|
||||||
|
const field = computed(() => table.value.field);
|
||||||
|
const url = computed(() => {
|
||||||
|
if (!table.value?.tableName) return;
|
||||||
|
const arrayData = useArrayData('translations');
|
||||||
|
if (arrayData) arrayData.reset(['skip', 'filter.skip', 'page']);
|
||||||
|
|
||||||
|
return table.value?.tableName + 's';
|
||||||
|
});
|
||||||
|
|
||||||
async function loadTable(data) {
|
async function loadTable(data) {
|
||||||
console.log('data: ', data);
|
|
||||||
if (data) originalData.value = data;
|
if (data) originalData.value = data;
|
||||||
if (!lang.value) return;
|
if (!lang.value) return;
|
||||||
const toFilter = JSON.parse(JSON.stringify(originalData.value));
|
const toFilter = JSON.parse(JSON.stringify(originalData.value));
|
||||||
console.log('toFilter: ', toFilter);
|
if (!toFilter) return;
|
||||||
|
|
||||||
const filtered = [];
|
const filtered = [];
|
||||||
for (const translation of toFilter) {
|
for (const row of toFilter) {
|
||||||
if (translation.lang != lang.value) continue;
|
if (row.lang != PRIMARY_LANG) continue;
|
||||||
translation[lang.value] = translation[field.value];
|
|
||||||
translation.en = originalData.value.find(
|
|
||||||
(o) => o.lang == 'en' && o[primaryKey.value] == translation[primaryKey.value]
|
|
||||||
)[field.value];
|
|
||||||
|
|
||||||
filtered.push(translation);
|
row.en = row[field.value];
|
||||||
|
const translation = originalData.value.find(
|
||||||
|
(o) => o.lang == lang.value && o[primaryKey.value] == row[primaryKey.value]
|
||||||
|
);
|
||||||
|
|
||||||
|
row[lang.value] = translation?.[field.value];
|
||||||
|
row.id = translation?.id;
|
||||||
|
filtered.push(row);
|
||||||
}
|
}
|
||||||
|
|
||||||
tableRef.value.CrudModelRef.formData = filtered;
|
tableRef.value.CrudModelRef.resetData(filtered);
|
||||||
|
|
||||||
// PENDIENTE
|
// PENDIENTE
|
||||||
/*
|
/*
|
||||||
Borrar por id (nativo de crudModel) y poner mensaje para si se quiieren borrar todas la traducciones (confirmar con Javi)
|
Poner mensaje para si se quiieren borrar todas la traducciones (confirmar con Javi)
|
||||||
Para guardar, Setear el original data de crudmodel para saber cuando cambia
|
Para guardar, Setear el original data de crudmodel para saber cuando cambia
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
@ -108,27 +91,25 @@ function resetOriginalData() {
|
||||||
originalData.value = null;
|
originalData.value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function upsertI18n(data) {
|
async function save(data, changes) {
|
||||||
console.log('data: ', data);
|
const { updates, creates } = changes();
|
||||||
await axios.patch(url.value, {
|
|
||||||
[field.value]: data[lang.value],
|
|
||||||
[primaryKey.value]: data[primaryKey.value],
|
|
||||||
lang: lang.value,
|
|
||||||
});
|
|
||||||
|
|
||||||
const index = originalData.value.findIndex(
|
for (const update of updates ?? []) {
|
||||||
(t) => t[primaryKey.value] == data[primaryKey.value]
|
update.data[field.value] = update.data[lang.value];
|
||||||
);
|
delete update.data[lang.value];
|
||||||
originalData.value[index] = data;
|
|
||||||
data.hasChanges = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function removeLine(id) {
|
|
||||||
try {
|
|
||||||
console.log({ id }, 'eliminado');
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Error ', err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const create of creates ?? []) {
|
||||||
|
delete create['$index'];
|
||||||
|
delete create[PRIMARY_LANG];
|
||||||
|
create.lang = lang.value;
|
||||||
|
create[field.value] = create[lang.value];
|
||||||
|
delete create[lang.value];
|
||||||
|
}
|
||||||
|
|
||||||
|
await axios.post(url.value + '/crud', { updates, creates });
|
||||||
|
tableRef.value.reload();
|
||||||
|
tableRef.value.CrudModelRef.hasChanges = false;
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -161,9 +142,11 @@ async function removeLine(id) {
|
||||||
</template>
|
</template>
|
||||||
</VnSubToolbar>
|
</VnSubToolbar>
|
||||||
<VnTable
|
<VnTable
|
||||||
|
v-if="url"
|
||||||
ref="tableRef"
|
ref="tableRef"
|
||||||
data-key="translations"
|
data-key="translations"
|
||||||
:url="url"
|
:url="url"
|
||||||
|
:limit="0"
|
||||||
:columns="columns"
|
:columns="columns"
|
||||||
:create="{
|
:create="{
|
||||||
urlCreate: url,
|
urlCreate: url,
|
||||||
|
@ -173,18 +156,17 @@ async function removeLine(id) {
|
||||||
}"
|
}"
|
||||||
:right-search="false"
|
:right-search="false"
|
||||||
@on-fetch="(data) => loadTable(data)"
|
@on-fetch="(data) => loadTable(data)"
|
||||||
:save-fn="upsertI18n"
|
|
||||||
:search-url="false"
|
:search-url="false"
|
||||||
:is-editable="true"
|
:is-editable="true"
|
||||||
|
:table="{
|
||||||
|
'row-key': 'id',
|
||||||
|
selection: 'multiple',
|
||||||
|
}"
|
||||||
|
v-model:selected="selected"
|
||||||
|
:save-fn="save"
|
||||||
|
auto-load
|
||||||
>
|
>
|
||||||
<template #more-create-dialog="{ data }">
|
<template #more-create-dialog="{ data }">
|
||||||
<VnSelect
|
|
||||||
:label="t('lang')"
|
|
||||||
v-model="data.lang"
|
|
||||||
:options="langs"
|
|
||||||
option-label="code"
|
|
||||||
option-value="code"
|
|
||||||
/>
|
|
||||||
<VnInput v-model="data[field]" :label="field" />
|
<VnInput v-model="data[field]" :label="field" />
|
||||||
</template>
|
</template>
|
||||||
</VnTable>
|
</VnTable>
|
||||||
|
|
Loading…
Reference in New Issue