diff --git a/database/data.db b/database/data.db index 2e8585a..b2b1cda 100644 Binary files a/database/data.db and b/database/data.db differ diff --git a/src/lib/components/ComboBox.svelte b/src/lib/components/ComboBox.svelte index 8ab28fc..7c83e18 100644 --- a/src/lib/components/ComboBox.svelte +++ b/src/lib/components/ComboBox.svelte @@ -4,24 +4,40 @@ import ChevronUp from './icons/ChevronUp.svelte'; import XMark from './icons/XMark.svelte'; import FuzzySearch from 'fuzzy-search'; - // import Fuse from 'fuse.js' type ComboBoxProps = { - options: ComboOption[] | CountryBasic[] | CityBasic[]; + options: readonly CountryBasic[]; onSelect: (data: ComboOption | CountryBasic | CityBasic) => void; onClear: () => void; disabledItems?: number[]; + selectedOptionId: number | null; }; - let { options, onSelect, onClear, disabledItems }: ComboBoxProps = $props(); + let { options, onSelect, onClear, disabledItems, selectedOptionId }: ComboBoxProps = $props(); + $effect(() => console.log(selectedOptionId)); let focused = $state(false); let selectSet = $state(false); let inputElement: HTMLInputElement; let dropdownElement: HTMLUListElement | null = $state(null); - let filterInput = $state(''); let emoji: string = $state(''); - + + const ifOptionSelected = (id: number | null) => { + let textValue = ''; + if (id) { + const value = options.find((option) => option.id === id) + if(value) { + emoji = 'emoji' in value ? (value.emoji ?? '') : ''; + textValue = 'name' in value ? value.name : ''; + selectSet = true; + } + } + return textValue; + }; + + + + let filterInput = $state(ifOptionSelected(selectedOptionId)); let searcher = $state(new FuzzySearch(options, ['name'], { caseSensitive: false, sort: true })); $effect(() => { diff --git a/src/lib/types.ts b/src/lib/types.ts index 1e192c7..4ec8595 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -20,6 +20,19 @@ export type CityTemperature = { max: number; date: string; } + +export type HotColdCity ={ + city_id: number; + city_name: string; + country_id: number; + country_name: string; + max_temp: number; + min_temp: number; + date: string; + hottest_rank: number; + coldest_rank: number; +} + export type ComboOption = { id: number; name: string; diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index f847c67..c53d35d 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,9 +1,9 @@ import { allCities, allCountries, getHottestAndColdestCityInCountry } from '$lib/db/queries'; -import type { CityBasic, CountryBasic } from '$lib/types'; -import { error, type Actions } from '@sveltejs/kit'; -import { getLocationTemperature } from '$lib/server/meteoService'; +import type { CityBasic, CityTemperature, CountryBasic, HotColdCity } from '$lib/types'; +import { type Actions } from '@sveltejs/kit'; import type { PageServerLoad } from './$types'; -import { fetchAllCitiesTemperatures } from '$lib/server/meteoDataHandler'; +// import { getLocationTemperature } from '$lib/server/meteoService'; +// import { fetchAllCitiesTemperatures } from '$lib/server/meteoDataHandler'; const errorObject = { ok: false, @@ -11,19 +11,36 @@ const errorObject = { error: 'Failed to fetch temperature' } -export const load: PageServerLoad = () => { +export const load: PageServerLoad = async ({ url }) => { try { const countries = allCountries.all() as CountryBasic[]; const cities = allCities.all() as CityBasic[]; + const country_id = url.searchParams.get('country'); + console.log('country_id >>> ', country_id); + + let temperatureData: HotColdCity[] | null = null; + if (country_id) { + temperatureData = getHottestAndColdestCityInCountry.all({ + $country_id: Number(country_id), + $date: new Date().toISOString().split('T')[0] + }) as HotColdCity[]; + } + + console.log('Loading >>>>', temperatureData); + return { countries, - cities + cities, + temperatureData, + country_id: country_id ? parseInt(country_id) : null }; } catch (error) { console.log(error); return { countries: [], - cities: [] + cities: [], + temperatureData: null, + country_id: null }; } }; @@ -34,28 +51,25 @@ export const actions: Actions = { const formData = await request.formData(); const country_id = formData.get('country_id') as string; - const response = await getHottestAndColdestCityInCountry.all({ + console.log('FORM ACTION >>>', country_id) + const response = getHottestAndColdestCityInCountry.all({ $country_id: Number(country_id), $date: new Date().toISOString().split('T')[0] - }); - console.log('RESPONSE >>>>', response); + }) as HotColdCity[]; - // if (response.length == 2) { - // console.log(response.data); + if (response.length == 2) { + console.log('RESPONSE >>>>', response); - // return { - // ok: true, - // data: response.data, - // error: null - // }; - // } else { + return { + ok: true, + data: response, + error: null, + country_id: country_id ?? null + }; + } else { return errorObject; - // } - // return { - // ok: true, - // data: response, - // error: null - // } + } + } catch (error) { console.log(error); return errorObject; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 002af30..76df528 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,38 +1,42 @@ HotColdCities -
+

HotColdCities

@@ -44,13 +48,39 @@
(selectedCountryId = null)} />
+ + {#if selectedCountryId && temperatureData} +
+ {#each temperatureData as temperature} +
+
{temperature.city_name}
+
Max: {temperature.max_temp}°C
+
Min: {temperature.min_temp}°C
+
+ {/each} +
+ {/if} -
    +