perf: refs #6321 clean code

This commit is contained in:
Javier Segarra 2025-01-31 01:37:08 +01:00
parent 0b084ae371
commit 107b8a7692
4 changed files with 99 additions and 92 deletions

View File

@ -10,29 +10,6 @@ import notifyResults from 'src/utils/notifyResults';
const MATCH_VALUES = [5, 6, 7, 8]; const MATCH_VALUES = [5, 6, 7, 8];
const { t } = useI18n(); const { t } = useI18n();
const extractNumericValue = (percentageString) => {
const match = percentageString.match(/(\d+(\.\d+)?)/);
return match ? parseFloat(match[0]) : null;
};
const compatibilityItem = (value) => `${100 * (value / MATCH_VALUES.length)}%`;
const gradientStyle = (value) => {
let color = 'white';
const perc = extractNumericValue(compatibilityItem(value));
switch (true) {
case perc >= 0 && perc < 33:
color = 'orange';
break;
case perc >= 33 && perc < 66:
color = 'yellow';
break;
default:
color = 'green';
break;
}
return color;
};
const tagColor = (match) => `color: ${!match ? 'red' : 'var(--vn-label-color)'}`;
const $props = defineProps({ const $props = defineProps({
itemLack: { itemLack: {
type: Object, type: Object,
@ -52,20 +29,17 @@ const $props = defineProps({
}); });
const proposalSelected = ref([]); const proposalSelected = ref([]);
const quantity = ref(-1); const quantity = ref(-1);
const sale = computed(() => $props.sales[0]);
const saleFk = computed(() => sale.value.saleFk);
const filter = computed(() => ({
itemFk: $props.itemLack.itemFk,
sales: saleFk.value,
}));
const proposalTableRef = ref(null);
const defaultColumnAttrs = { const defaultColumnAttrs = {
align: 'center', align: 'center',
sortable: false, sortable: false,
}; };
const sale = computed(() => $props.sales[0]);
const saleFk = computed(() => sale.value.saleFk);
const statusConditionalValue = (row) => {
const total = MATCH_VALUES.reduce((acc, i) => acc + row[`match${i}`], 0);
return total;
};
const proposalTableRef = ref(null);
const emit = defineEmits(['onDialogClosed', 'itemReplaced']);
const conditionalValuePrice = (price) => (price > 1.3 ? 'match' : 'not-match');
const columns = computed(() => [ const columns = computed(() => [
{ {
...defaultColumnAttrs, ...defaultColumnAttrs,
@ -157,8 +131,44 @@ const columns = computed(() => [
field: 'located', field: 'located',
}, },
]); ]);
const extractNumericValue = (percentageString) => {
const match = percentageString.match(/(\d+(\.\d+)?)/);
return match ? parseFloat(match[0]) : null;
};
const compatibilityItem = (value) => `${100 * (value / MATCH_VALUES.length)}%`;
const gradientStyle = (value) => {
let color = 'white';
const perc = extractNumericValue(compatibilityItem(value));
switch (true) {
case perc >= 0 && perc < 33:
color = 'orange';
break;
case perc >= 33 && perc < 66:
color = 'yellow';
break;
default:
color = 'green';
break;
}
return color;
};
const tagColor = (match) => `color: ${!match ? 'red' : 'var(--vn-label-color)'}`;
const statusConditionalValue = (row) => {
const total = MATCH_VALUES.reduce((acc, i) => acc + row[`match${i}`], 0);
return total;
};
const emit = defineEmits(['onDialogClosed', 'itemReplaced']);
const conditionalValuePrice = (price) => (price > 1.3 ? 'match' : 'not-match');
const isSelected = (row) => const isSelected = (row) =>
proposalSelected.value.some((item) => row.itemFk === item.itemFk); proposalSelected.value.some((item) => row.itemFk === item.itemFk);
function change(row) { function change(row) {
if (isSelected(row)) { if (isSelected(row)) {
confirm(row); confirm(row);
@ -166,6 +176,7 @@ function change(row) {
} }
proposalSelected.value = [row]; proposalSelected.value = [row];
} }
async function confirm() { async function confirm() {
try { try {
const substitutionFk = proposalSelected.value[0].itemFk; const substitutionFk = proposalSelected.value[0].itemFk;
@ -190,10 +201,7 @@ async function confirm() {
console.error(error); console.error(error);
} }
} }
const filter = computed(() => ({
itemFk: $props.itemLack.itemFk,
sales: saleFk.value,
}));
const isSelectionAvailable = (itemProposal) => { const isSelectionAvailable = (itemProposal) => {
const { price2 } = itemProposal; const { price2 } = itemProposal;
const salePrice = sale.value.price; const salePrice = sale.value.price;
@ -276,8 +284,9 @@ const isSelectionAvailable = (itemProposal) => {
</template> </template>
<template #column-counter="{ row }"> <template #column-counter="{ row }">
<span <span
:style="{ :class="{
color: row[`match${tag}`] ? 'green' : 'var(--vn-label-color)', match: row.counter === 1,
'not-match': row.counter !== 1,
}" }"
>{{ row.counter }}</span >{{ row.counter }}</span
> >

View File

@ -1,16 +1,7 @@
<script setup> <script setup>
import ItemProposal from './ItemProposal.vue'; import ItemProposal from './ItemProposal.vue';
import { useDialogPluginComponent } from 'quasar'; import { useDialogPluginComponent } from 'quasar';
const emit = defineEmits([
'onDialogClosed',
'itemReplaced',
'confirm',
'cancel',
...useDialogPluginComponent.emits,
]);
defineExpose({ show: () => dialogRef.value.show(), hide: () => dialogRef.value.hide() });
const { dialogRef } = useDialogPluginComponent();
const $props = defineProps({ const $props = defineProps({
itemLack: { itemLack: {
type: Object, type: Object,
@ -28,6 +19,13 @@ const $props = defineProps({
default: () => [], default: () => [],
}, },
}); });
const { dialogRef } = useDialogPluginComponent();
const emit = defineEmits([
'onDialogClosed',
'itemReplaced',
...useDialogPluginComponent.emits,
]);
defineExpose({ show: () => dialogRef.value.show(), hide: () => dialogRef.value.hide() });
</script> </script>
<template> <template>
<QDialog ref="dialogRef" full-width transition-show="scale" transition-hide="scale"> <QDialog ref="dialogRef" full-width transition-show="scale" transition-hide="scale">

View File

@ -56,7 +56,7 @@ const canProceed = ref();
watch( watch(
() => route.params.id, () => route.params.id,
() => tableRef.value.reload() () => tableRef.value.reload(),
); );
const columns = computed(() => [ const columns = computed(() => [
@ -199,7 +199,7 @@ const changeQuantity = async (sale) => {
await updateQuantity(sale); await updateQuantity(sale);
} catch (e) { } catch (e) {
const { quantity } = tableRef.value.CrudModelRef.originalData.find( const { quantity } = tableRef.value.CrudModelRef.originalData.find(
(s) => s.id === sale.id (s) => s.id === sale.id,
); );
sale.quantity = quantity; sale.quantity = quantity;
throw e; throw e;
@ -503,7 +503,7 @@ async function isSalePrepared(item) {
componentProps: { componentProps: {
title: t('Item prepared'), title: t('Item prepared'),
message: t( message: t(
'This item is already prepared. Do you want to continue?' 'This item is already prepared. Do you want to continue?',
), ),
data: item, data: item,
}, },
@ -525,7 +525,7 @@ watch(
if (newItemFk) { if (newItemFk) {
updateItem(newRow.value); updateItem(newRow.value);
} }
} },
); );
</script> </script>
@ -595,7 +595,7 @@ watch(
openConfirmationModal( openConfirmationModal(
t('Continue anyway?'), t('Continue anyway?'),
t('You are going to delete lines of the ticket'), t('You are going to delete lines of the ticket'),
removeSales removeSales,
) )
" "
> >

View File

@ -46,46 +46,9 @@ const filterLack = ref({
const selectedRows = ref([]); const selectedRows = ref([]);
const { t } = useI18n(); const { t } = useI18n();
const { notify } = useNotify(); const { notify } = useNotify();
const route = useRoute();
const getInputEvents = ({ col, ...rows }) => ({
'update:modelValue': () => saveChange(col.name, rows),
'keyup.enter': () => saveChange(col.name, rows),
});
const saveChange = async (field, { rowIndex, row }) => {
try {
switch (field) {
case 'alertLevelCode':
await axios.post(`Tickets/state`, {
ticketFk: row.ticketFk,
code: row[field],
});
break;
case 'quantity':
await axios.post(`Sales/${row.saleFk}/updateQuantity`, {
quantity: +row.quantity,
});
break;
default:
console.error(field, { rowIndex, row });
break;
}
notify('globals.dataSaved', 'positive');
fetchItemLack.value.fetch();
} catch (err) {
console.error('Error saving changes', err);
f;
}
};
const entityId = computed(() => route.params.id); const entityId = computed(() => route.params.id);
const item = ref({}); const item = ref({});
const hasToIgnore = (row) => row.hasToIgnore === 1; const route = useRoute();
const rowColor = (row) => {
if (hasToIgnore(row)) return 'transparent';
return 'negative';
};
const columns = computed(() => [ const columns = computed(() => [
{ {
name: 'status', name: 'status',
@ -186,11 +149,48 @@ const emit = defineEmits(['update:selection']);
const itemLack = ref(null); const itemLack = ref(null);
const fetchItemLack = ref(null); const fetchItemLack = ref(null);
const tableRef = ref(null); const tableRef = ref(null);
defineExpose({ tableRef, itemLack });
watch(selectedRows, () => emit('update:selection', selectedRows)); watch(selectedRows, () => emit('update:selection', selectedRows));
const getInputEvents = ({ col, ...rows }) => ({
'update:modelValue': () => saveChange(col.name, rows),
'keyup.enter': () => saveChange(col.name, rows),
});
const saveChange = async (field, { rowIndex, row }) => {
try {
switch (field) {
case 'alertLevelCode':
await axios.post(`Tickets/state`, {
ticketFk: row.ticketFk,
code: row[field],
});
break;
case 'quantity':
await axios.post(`Sales/${row.saleFk}/updateQuantity`, {
quantity: +row.quantity,
});
break;
default:
console.error(field, { rowIndex, row });
break;
}
notify('globals.dataSaved', 'positive');
fetchItemLack.value.fetch();
} catch (err) {
console.error('Error saving changes', err);
f;
}
};
const hasToIgnore = (row) => row.hasToIgnore === 1;
const rowColor = (row) => {
if (hasToIgnore(row)) return 'transparent';
return 'negative';
};
function onBuysFetched(data) { function onBuysFetched(data) {
Object.assign(item.value, data[0]); Object.assign(item.value, data[0]);
} }
defineExpose({ tableRef, itemLack });
</script> </script>
<template> <template>