fix: lỗi nút xuất PDF và chèn link ở Địa điểm khi xuất PDF

This commit is contained in:
2026-06-24 19:54:14 +07:00
parent 957ba6c72c
commit 5d47e6d291
6 changed files with 407 additions and 21 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+50 -21
View File
@@ -511,6 +511,7 @@ export const TourDetailPage = ({
const tableColumn = ["STT", "Ngày giờ", "Chặng", "Địa điểm", "Ghi chú"];
const tableRows: any[] = [];
const legRowRanges: Record<string, { start: number; count: number }> = {};
const pdfMapLinks: { [key: number]: string } = {};
const formatDateTime = (dateStr: string) => {
if (!dateStr) return '';
@@ -534,16 +535,15 @@ export const TourDetailPage = ({
loc && (loc.plannedStart || loc.plannedEnd || loc.name)
);
const startRow = globalRowIndex;
if (legLocations.length > 0) {
const startRow = globalRowIndex;
legRowRanges[leg.id] = { start: startRow, count: legLocations.length };
legLocations.forEach((loc: any) => {
const timeStr = loc.plannedStart
? formatDateTime(loc.plannedStart)
: loc.plannedEnd
? formatDateTime(loc.plannedEnd)
: '';
legLocations.forEach((loc: any, locIdx: number) => {
const isStartPoint = loc.plannedStart && new Date(loc.plannedStart).getTime() === 0;
const timeSource = isStartPoint ? loc.plannedEnd : loc.plannedStart;
const timeStr = timeSource ? formatDateTime(timeSource) : '';
const locName = loc.name || '';
const addressStr = loc.address || '';
@@ -551,18 +551,44 @@ export const TourDetailPage = ({
? `\n${loc.latitude}, ${loc.longitude}`
: '';
const locationText = [locName, addressStr, coordStr].filter(Boolean).join('\n');
const noteText = loc.note || '';
let mapUrl = '';
if (loc.latitude && loc.longitude) {
mapUrl = `https://www.google.com/maps/search/?api=1&query=${loc.latitude},${loc.longitude}`;
} else if (addressStr || locName) {
mapUrl = `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(addressStr || locName)}`;
}
const currentRowPosition = tableRows.length;
if (mapUrl) {
pdfMapLinks[currentRowPosition] = mapUrl;
}
const locationCellObj = {
content: locationText,
styles: mapUrl ? { textColor: [40, 83, 107], fontStyle: 'bold' as const } : {}
};
tableRows.push([
stt++,
timeStr,
legName,
locationText,
locIdx === 0 ? legName : '',
locationCellObj,
noteText
]);
globalRowIndex++;
});
} else {
legRowRanges[leg.id] = { start: startRow, count: 1 };
tableRows.push([
stt++,
'',
legName,
'Chưa có địa điểm trong chặng này',
''
]);
globalRowIndex++;
}
});
}
@@ -618,17 +644,20 @@ export const TourDetailPage = ({
cell.rowSpan = 1;
}
}
if (cell.column.index === 3) {
const rowData = tableRows[cell.row.index];
if (rowData && rowData[3]) {
const coordMatch = rowData[3].match(/([\d.]+),\s*([\d.]+)/);
if (coordMatch) {
const lat = coordMatch[1];
const lng = coordMatch[2];
cell.cell.link = `https://www.openstreetmap.org/?mlat=${lat}&mlon=${lng}`;
}
}
}
},
didDrawCell: (data: any) => {
if (data.section === 'body' && data.column.index === 3) {
const activeRowIndex = data.row.index;
const targetMapUrl = pdfMapLinks[activeRowIndex];
if (targetMapUrl) {
data.doc.link(
data.cell.x,
data.cell.y,
data.cell.width,
data.cell.height,
{ url: targetMapUrl }
);
}
}
}