Move to trace endpoint

This commit is contained in:
Zachary
2024-11-11 19:06:06 +01:00
parent 38cb569884
commit d243fa241a

View File

@@ -1,15 +1,3 @@
<script context="module">
export async function load({ request }) {
// Access the CF-IPCountry header
const countryCode = request.headers.get('cf-ipcountry') || 'IE';
return {
props: {
countryCode
}
};
}
</script>
<script> <script>
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import countries from 'i18n-iso-countries'; import countries from 'i18n-iso-countries';
@@ -18,21 +6,33 @@
import { getHolidaysForYear, optimizeDaysOff, calculateConsecutiveDaysOff } from '../lib/holidayUtils.js'; import { getHolidaysForYear, optimizeDaysOff, calculateConsecutiveDaysOff } from '../lib/holidayUtils.js';
import { ptoData } from '../lib/ptoData.js'; import { ptoData } from '../lib/ptoData.js';
export let countryCode; // Receive the country code as a prop
countries.registerLocale(enLocale); countries.registerLocale(enLocale);
let countriesList = countries.getNames('en'); let countriesList = countries.getNames('en');
let year = new Date().getFullYear(); let year = new Date().getFullYear();
let months = Array.from({ length: 12 }, (_, i) => i); let months = Array.from({ length: 12 }, (_, i) => i);
let selectedCountry = countriesList[countryCode] || 'Ireland'; let selectedCountry = 'Belgium';
let holidays = []; let holidays = [];
let daysOff = ptoData[countryCode] || ptoData['IE']; let daysOff = ptoData['BE'];
let optimizedDaysOff = []; let optimizedDaysOff = [];
let consecutiveDaysOff = []; let consecutiveDaysOff = [];
let placeholder = "Country"; let placeholder = "Country";
let inputElement; let inputElement;
async function fetchCountryCode() {
try {
const response = await fetch('/cdn-cgi/trace');
const text = await response.text();
const countryCodeMatch = text.match(/cf-ipcountry=(\w+)/);
const countryCode = countryCodeMatch ? countryCodeMatch[1] : 'BE';
selectedCountry = countriesList[countryCode] || 'Belgium';
daysOff = ptoData[countryCode] || ptoData['BE'];
updateHolidays();
} catch (error) {
console.error('Error fetching country code:', error);
}
}
function handleCountryChange(event) { function handleCountryChange(event) {
const fullValue = event.target.value; const fullValue = event.target.value;
const countryCode = Object.keys(countriesList).find(code => countriesList[code] === fullValue); const countryCode = Object.keys(countriesList).find(code => countriesList[code] === fullValue);
@@ -102,6 +102,7 @@
} }
onMount(() => { onMount(() => {
fetchCountryCode(); // Fetch the country code on mount
adjustInputWidth(inputElement); adjustInputWidth(inputElement);
inputElement.addEventListener('input', () => { inputElement.addEventListener('input', () => {
adjustInputWidth(inputElement); adjustInputWidth(inputElement);