From a4fe67d968f002a8a08ef93eb5e8772dd6cf2116 Mon Sep 17 00:00:00 2001
From: Daniel Schadt <kingdread@gmx.de>
Date: Sat, 6 Apr 2024 22:11:55 +0200
Subject: fix URL resolution for AJAX calls

This also fixes a few spots from previous additions.
---
 asset-sources/fietsboek.ts | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

(limited to 'asset-sources')

diff --git a/asset-sources/fietsboek.ts b/asset-sources/fietsboek.ts
index e2d7d1f..d70890f 100644
--- a/asset-sources/fietsboek.ts
+++ b/asset-sources/fietsboek.ts
@@ -1,4 +1,5 @@
 declare const FRIENDS_URL: string;
+declare const BASE_URL: string;
 declare const LOCALE: string;
 
 /**
@@ -39,6 +40,19 @@ function getCookie(name: string): string | undefined {
         ?.split("=")[1];
 }
 
+
+/**
+ * Builds a URL with the correct application URL as base.
+ *
+ * Do not add the leading slash, otherwise the resolution will be wrong!
+ *
+ * @param path - Path to append to the base.
+ * @return The correct URL in regards to the application URL.
+ */
+function makeUrl(name: string): URL {
+    return new URL(name, BASE_URL);
+}
+
 /**
  * Installs a listener to the given DOM objects.
  *
@@ -392,7 +406,7 @@ addHandler(".summary-toggler", "click", toggleSummary);
  */
 addHandler("#archiveDownloadButton", "click", () => {
     const checked = document.querySelectorAll(".archive-checkbox:checked");
-    const url = new URL("/track/archive", window.location.href);
+    const url = makeUrl("track/archive");
     checked.forEach((c) => {
         url.searchParams.append("track_id[]", (c as HTMLInputElement).value);
     });
@@ -455,7 +469,7 @@ function toggleTrackFavourite(event: MouseEvent) {
     if (trackId === null) {
         return;
     }
-    const url = new URL("/me/toggle-favourite", window.location.href);
+    const url = makeUrl("me/toggle-favourite");
     const formData = new URLSearchParams();
     formData.append("track-id", trackId);
     formData.append("csrf_token", getCookie("csrf_token") ?? "");
@@ -493,7 +507,7 @@ function getLocalizedMonthNames(): string[] {
 function loadProfileStats() {
     const monthNames = getLocalizedMonthNames();
 
-    const url = new URL("/me/summary.json", window.location.href);
+    const url = makeUrl("me/summary.json");
     fetch(url)
         .then(response => response.json())
         .then((response: YearSummary) => {
-- 
cgit v1.2.3