diff --git a/src/routes/aaaa/+page.server.ts b/src/routes/aaaa/+page.server.ts index b799628..b329c55 100644 --- a/src/routes/aaaa/+page.server.ts +++ b/src/routes/aaaa/+page.server.ts @@ -1,15 +1,26 @@ -const headers = { 'X-API-KEY': '', 'Content-type': 'application/json' } +const headers = { 'X-API-KEY': '1FvqqqUpoeNgNpxZQxJX6FBDLbwKscx5hgXhZxUb', 'Content-type': 'application/json' } let location: string = "L'Hospitalet de Llobregat"; let municipis = await fetch('https://api.meteo.cat/referencia/v1/municipis', { method: 'GET', headers: new Headers(headers), credentials: 'include' }).then( (data) => data.json() ); export function load(): { location: string, weatherData: Promise } { - return { location: location, weatherData: getCityCode( location ) } + let objMunicipi = getCityCode( location ); + if (objMunicipi === location) { + return { location, weatherData: getWeather(location).then( (data) => data.message ) } + } + let codiMunicipi: string = objMunicipi.codi; + let locFullName: string = objMunicipi.nom; + return { location: locFullName, weatherData: getWeather(codiMunicipi) } }; -function changeLocation(newLoc: string) { +function changeLocation(newLoc: string): void { location = newLoc; } +async function fetchApi(url: string) { + let response: Response = await fetch(url, { method: 'GET', headers: new Headers(headers), credentials: 'include' }) + return response; +} + export const actions = { changeLoc: async ({ request }) => { const data: FormData = await request.formData(); @@ -17,11 +28,18 @@ export const actions = { } } -async function getWeather(): Promise { - let response = await fetch('https://api.meteo.cat/referencia/v1/municipis', { method: 'GET', headers: new Headers(headers), credentials: 'include' }) - return response.json(); +async function getWeather(codiMunicipi: string): Promise { + if (codiMunicipi !== '') { + let response = await fetchApi(`https://api.meteo.cat/pronostic/v1/municipal/${codiMunicipi}`) + return response.json(); + } + return ''; } -function getCityCode(locName: string) { - return municipis.filter( municipi => municipi["nom"].startsWith(location) ) +function getCityCode(locName: string): object | string { + let foundMunicipis: object[] = municipis.filter( municipi => municipi["nom"].toLowerCase().startsWith(locName.toLowerCase()) ) + if ( foundMunicipis.length > 0 ) { + return foundMunicipis[0]; + } + return locName; } diff --git a/src/routes/aaaa/+page.svelte b/src/routes/aaaa/+page.svelte index b06434d..f9241c4 100644 --- a/src/routes/aaaa/+page.svelte +++ b/src/routes/aaaa/+page.svelte @@ -8,15 +8,43 @@ function toggleDisplay(): void { display = (display === 'none') ? 'block' : 'none'; } + + $: variables = data.weatherData.dies[0].variables;

Change location

- +
-

Weather in {data.location}:

- -
-
{ JSON.stringify(data.weatherData) }
+ +

El temps a: {data.location}

+ + + + + + + + + + + + + + + + +
Temperatura màximaTemperatura mínimaPrecipitació
{ variables.tmax.valor }{ variables.tmax.unitat }{ variables.tmin.valor }{ variables.tmin.unitat }{ variables.precipitacio.valor }{ variables.precipitacio.unitat }
+