From 2693f259d4617e4fbedaf10558a052efb09abaef Mon Sep 17 00:00:00 2001 From: wbuezas Date: Tue, 13 Feb 2024 10:17:24 -0300 Subject: [PATCH] Add drag and drop do Travel Extra Community table --- src/pages/Travel/ExtraCommunity.vue | 150 ++++++++++++++++++++++------ 1 file changed, 117 insertions(+), 33 deletions(-) diff --git a/src/pages/Travel/ExtraCommunity.vue b/src/pages/Travel/ExtraCommunity.vue index 4cbb850e1..42c5480f4 100644 --- a/src/pages/Travel/ExtraCommunity.vue +++ b/src/pages/Travel/ExtraCommunity.vue @@ -47,6 +47,10 @@ const arrayData = useArrayData('ExtraCommunity', { }); const rows = computed(() => arrayData.store.data || []); +const draggedRowIndex = ref(null); +const targetRowIndex = ref(null); +const entryRowIndex = ref(null); +const draggedEntry = ref(null); const tableColumnComponents = { id: { @@ -263,6 +267,56 @@ onMounted(async () => { landedTo.value.setHours(23, 59, 59, 59); await getData(); }); + +const handleDragStart = (event, rowIndex, entryIndex) => { + draggedRowIndex.value = rowIndex; + entryRowIndex.value = entryIndex; + event.dataTransfer.effectAllowed = 'move'; +}; + +const handleDragEnter = (_, targetIndex) => { + targetRowIndex.value = targetIndex; +}; + +const handleDragOver = (e) => { + e.preventDefault(); +}; + +const saveRowDrop = async (targetRowIndex) => { + const entryId = draggedEntry.value.id; + const travelId = rows.value[targetRowIndex].id; + await axios.patch(`Entries/${entryId}`, { travelFk: travelId }); +}; + +const moveRow = async (draggedRowIndex, targetRowIndex, entryIndex) => { + try { + if (draggedRowIndex === targetRowIndex) return; + // Remover entry de la row original + draggedEntry.value = rows.value[draggedRowIndex].entries.splice(entryIndex, 1)[0]; + //Si la row de destino por alguna razón no tiene la propiedad entry la creamos + if (!rows.value[targetRowIndex].entries) rows.value[targetRowIndex].entries = []; + // Añadir entry a la row de destino + rows.value[targetRowIndex].entries.push(draggedEntry.value); + + await saveRowDrop(targetRowIndex); + } catch (err) { + cleanDragAndDropData(); + console.error('Error moving row', err); + } +}; + +const handleDrop = () => { + if (!draggedRowIndex.value && !targetRowIndex.value) return; + moveRow(draggedRowIndex.value, targetRowIndex.value, entryRowIndex.value); + cleanDragAndDropData(); +}; + +const cleanDragAndDropData = () => { + draggedRowIndex.value = null; + targetRowIndex.value = null; + entryRowIndex.value = null; + draggedEntry.value = null; +}; + + en: searchExtraCommunity: Search for extra community shipping