Skip to content

Commit

Permalink
seperate for each export type (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaudiomv authored Jun 9, 2024
1 parent 7795f14 commit ad66bdd
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 222 deletions.
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default [
ignores: ['build/', '.svelte-kit/', 'dist/', '*.lock']
},
{
files: ['**/*.ts'],
files: ['**/*.ts', '**/*.svelte'],
rules: {
'@typescript-eslint/no-explicit-any': 'off'
}
Expand Down
23 changes: 0 additions & 23 deletions src/components/DownloadLinks.svelte

This file was deleted.

21 changes: 21 additions & 0 deletions src/components/ExportCSV.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script lang="ts">
import { processExportData } from '../utils/DataUtils';
import * as XLSX from 'xlsx';
export let data: any[];
let downloadUrl: string = '';
$: downloadUrl = exportCSV(data);
function exportCSV(data: any[]): string {
const processedData = processExportData(data);
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(processedData);
XLSX.utils.book_append_sheet(wb, ws, 'Data');
const csvString = XLSX.write(wb, { bookType: 'csv', type: 'string' });
const blob = new Blob([csvString], { type: 'text/csv' });
return URL.createObjectURL(blob);
}
</script>

<a href={downloadUrl} class="download-links" download="BMLT_data.csv">Download CSV</a>
43 changes: 24 additions & 19 deletions src/components/ExportForm.svelte
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
<script lang="ts">
import { writable } from 'svelte/store';
import { fetchData, exportCSV, exportXLSX, exportXML, exportKML, exportYAML } from '$lib/DataUtils';
import DownloadLinks from './DownloadLinks.svelte';
import { fetchData } from '../utils/DataUtils';
import ExportCSV from './ExportCSV.svelte';
import ExportYAML from './ExportYAML.svelte';
import ExportXML from './ExportXML.svelte';
import ExportXLSX from './ExportXLSX.svelte';
import ExportKML from './ExportKML.svelte';
const processing = writable<boolean>(false);
const errorMessage = writable<string>('');
let query: string = '';
let csvDownloadUrl: string = '';
let xlsxDownloadUrl: string = '';
let xmlDownloadUrl: string = '';
let kmlDownloadUrl: string = '';
let yamlDownloadUrl: string = '';
let data: any[] = [];
let includeKML: boolean = false;
async function handleExport() {
if (query.trim() === '') return;
errorMessage.set('');
csvDownloadUrl = '';
xlsxDownloadUrl = '';
xmlDownloadUrl = '';
kmlDownloadUrl = '';
yamlDownloadUrl = '';
data = [];
includeKML = query.includes('GetSearchResults');
try {
processing.set(true);
const data = await fetchData(query);
csvDownloadUrl = exportCSV(data);
xlsxDownloadUrl = exportXLSX(data);
xmlDownloadUrl = exportXML(data);
yamlDownloadUrl = exportYAML(data);
kmlDownloadUrl = query.includes('GetSearchResults') ? exportKML(data) : '';
data = await fetchData(query);
} catch (error) {
errorMessage.set(error instanceof Error ? error.message : 'Failed to export data.');
} finally {
Expand All @@ -47,6 +40,18 @@
{#if $errorMessage}
<p class="error" id="errorMessages">{$errorMessage}</p>
{/if}
<DownloadLinks {csvDownloadUrl} {xlsxDownloadUrl} {xmlDownloadUrl} {kmlDownloadUrl} {yamlDownloadUrl} />
{#if data.length}
<ExportCSV {data} />
<br />
<ExportYAML {data} />
<br />
<ExportXML {data} />
<br />
<ExportXLSX {data} />
<br />
{#if includeKML}
<ExportKML {data} />
{/if}
{/if}
<div id="description">Converts BMLT data from JSON to CSV, XLSX, XML, KML or YAML</div>
</div>
92 changes: 92 additions & 0 deletions src/components/ExportKML.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<script lang="ts">
import type { Meeting } from '../utils/DataUtils';
export let data: Meeting[];
let downloadUrl: string = '';
$: downloadUrl = exportKML(data);
function exportKML(data: Meeting[]): string {
const placemarks = data.map(createPlacemark).filter(Boolean).join('\n');
const kmlContent = `<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
${placemarks}
</Document>
</kml>`;
const blob = new Blob([kmlContent], { type: 'application/vnd.google-earth.kml+xml' });
return URL.createObjectURL(blob);
}
function createPlacemark(meeting: Meeting): string {
const name = meeting.meeting_name.trim() || 'NA Meeting';
const lng = parseFloat(meeting.longitude);
const lat = parseFloat(meeting.latitude);
if (!lng || !lat) return '';
const description = prepareSimpleLine(meeting);
const address = prepareSimpleLine(meeting, false);
return `
<Placemark>
<name>${name}</name>
${address ? `<address>${address}</address>` : ''}
${description ? `<description>${description}</description>` : ''}
<Point>
<coordinates>${lng},${lat}</coordinates>
</Point>
</Placemark>
`.trim();
}
function prepareSimpleLine(meeting: Meeting, withDate: boolean = true): string {
const getLocationInfo = () => {
const locationInfo: string[] = [];
const addInfo = (property: keyof Meeting) => {
const value = meeting[property]?.trim() ?? '';
if (value) locationInfo.push(value);
};
addInfo('location_text');
addInfo('location_street');
addInfo('location_city_subsection');
addInfo('location_municipality');
addInfo('location_neighborhood');
addInfo('location_province');
addInfo('location_postal_code_1');
addInfo('location_nation');
addInfo('location_info');
return locationInfo.join(', ');
};
const getDateString = () => {
const dayOfWeekInt = parseInt(meeting.weekday_tinyint?.trim() ?? '0');
const adjustedDay = dayOfWeekInt % 7;
// January 1, 2023, was a Sunday.
const baseDate = new Date('2023-01-01');
baseDate.setDate(baseDate.getDate() + adjustedDay);
const lang = meeting.lang_enum ? (meeting.lang_enum === 'dk' ? 'da' : meeting.lang_enum) : window.navigator.language;
const twelveHrLangs: string[] = ['en', 'es'];
if (dayOfWeekInt && withDate) {
let dateString = baseDate.toLocaleDateString(lang, { weekday: 'long' });
if (!isNaN(baseDate.getTime())) {
dateString += `, ${baseDate.toLocaleTimeString(lang, {
hour: 'numeric',
minute: 'numeric',
hour12: twelveHrLangs.includes(lang)
})}`;
}
return dateString;
}
return '';
};
const locationInfo = getLocationInfo();
const dateString = getDateString();
if (withDate && dateString && locationInfo) {
return `${dateString}, ${locationInfo}`;
} else if (dateString) {
return dateString;
} else {
return locationInfo;
}
}
</script>

<a href={downloadUrl} class="download-links" download="BMLT_data.kml">Download KML</a>
21 changes: 21 additions & 0 deletions src/components/ExportXLSX.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script lang="ts">
import { processExportData, s2ab } from '../utils/DataUtils';
import * as XLSX from 'xlsx';
export let data: any[];
let downloadUrl: string = '';
$: downloadUrl = exportXLSX(data);
function exportXLSX(data: any[]): string {
const processedData = processExportData(data);
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(processedData);
XLSX.utils.book_append_sheet(wb, ws, 'Data');
const wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'binary' });
const blob = new Blob([s2ab(wbout)], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
return URL.createObjectURL(blob);
}
</script>

<a href={downloadUrl} class="download-links" download="BMLT_data.xlsx">Download XLSX</a>
18 changes: 18 additions & 0 deletions src/components/ExportXML.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script lang="ts">
import { processExportData } from '../utils/DataUtils';
import * as js2xmlparser from 'js2xmlparser';
export let data: any[];
let downloadUrl: string = '';
$: downloadUrl = exportXML(data);
function exportXML(data: any[]): string {
const processedData = processExportData(data);
const xmlResult = js2xmlparser.parse('root', processedData);
const blob = new Blob([xmlResult], { type: 'text/xml' });
return URL.createObjectURL(blob);
}
</script>

<a href={downloadUrl} class="download-links" download="BMLT_data.xml">Download XML</a>
18 changes: 18 additions & 0 deletions src/components/ExportYAML.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script lang="ts">
import { processExportData } from '../utils/DataUtils';
import * as yaml from 'js-yaml';
export let data: any[];
let downloadUrl: string = '';
$: downloadUrl = exportYAML(data);
function exportYAML(data: any[]): string {
const processedData = processExportData(data);
const yamlString = yaml.dump(processedData);
const blob = new Blob([yamlString], { type: 'application/x-yaml' });
return URL.createObjectURL(blob);
}
</script>

<a href={downloadUrl} class="download-links" download="BMLT_data.yaml">Download YAML</a>
Loading

0 comments on commit ad66bdd

Please sign in to comment.