From 7472ddff159ce4e67bb63f8efa7ef0ef74b3b78a Mon Sep 17 00:00:00 2001 From: Risto Salama Date: Wed, 27 Mar 2024 13:47:35 +0200 Subject: [PATCH] Hakutapa suodatin hauille --- src/app/components/haku-selector.tsx | 31 +++++++++++++++++------ src/app/lib/common.ts | 9 +++++++ src/app/lib/configuration.ts | 8 +++--- src/app/lib/koodisto.ts | 37 ++++++++++++++++++++++++++++ src/app/lib/kouta.ts | 7 +----- src/app/lib/login-handler.ts | 2 +- src/app/page.tsx | 5 ++-- 7 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 src/app/lib/common.ts create mode 100644 src/app/lib/koodisto.ts diff --git a/src/app/components/haku-selector.tsx b/src/app/components/haku-selector.tsx index 4c2ca39f..95c9d410 100644 --- a/src/app/components/haku-selector.tsx +++ b/src/app/components/haku-selector.tsx @@ -3,6 +3,7 @@ import { useState } from "react"; import { SearchDropdown } from "./search-dropdown"; import { Haku, Tila } from "../lib/kouta"; +import { Koodi } from "../lib/koodisto"; //TODO: move HaunAlkaminen getHakuAlkamisKaudet to some util for example const STARTING_YEAR = 2019; // check earliest kouta haku @@ -26,14 +27,16 @@ const getHakuAlkamisKaudet = (): HaunAlkaminen[] => { const alkamisKausiMatchesSelected = (haku: Haku, selectedAlkamisKausi: HaunAlkaminen): boolean => haku.alkamisVuosi === selectedAlkamisKausi.alkamisVuosi && haku.alkamisKausiKoodiUri.startsWith(selectedAlkamisKausi.alkamisKausiKoodiUri); -export const HakuSelector = ({haut}: {haut : Haku[]}) =>{ +export const HakuSelector = ({haut, hakutavat}: {haut : Haku[], hakutavat: Koodi[]}) =>{ const [results, setResults] = useState(); const [searchTila, setSearchTila] = useState(Tila.JULKAISTU); const [selectedHaku, setSelectedHaku] = useState(); const [selectedAlkamisKausi, setSelectedAlkamisKausi] = useState(); + const [selectedHakutapa, setSelectedHakutapa] = useState(); const alkamisKaudet = getHakuAlkamisKaudet(); + type changeHandler = React.ChangeEventHandler; const handleChange: changeHandler = (e) => { @@ -43,6 +46,7 @@ export const HakuSelector = ({haut}: {haut : Haku[]}) =>{ const filteredValue = haut.filter((haku: Haku) => haku.tila == searchTila && haku.nimi.fi?.toLowerCase().includes(target.value.toLowerCase()) && (!selectedAlkamisKausi || alkamisKausiMatchesSelected(haku, selectedAlkamisKausi)) + && (!selectedHakutapa || haku.hakutapaKoodiUri.startsWith(selectedHakutapa.koodiUri)) ); setResults(filteredValue); }; @@ -61,13 +65,24 @@ export const HakuSelector = ({haut}: {haut : Haku[]}) =>{ onChange={handleChange} onSelect={(h: Haku) => setSelectedHaku(h)} /> - - +
+ + +
+
+ + +
); } diff --git a/src/app/lib/common.ts b/src/app/lib/common.ts new file mode 100644 index 00000000..f1e7af53 --- /dev/null +++ b/src/app/lib/common.ts @@ -0,0 +1,9 @@ +export type TranslatedName = { + fi?: string, + en?: string, + sv?: string +} + +export enum Language { + FI, EN, SV +} diff --git a/src/app/lib/configuration.ts b/src/app/lib/configuration.ts index db26bd73..7e108fb8 100644 --- a/src/app/lib/configuration.ts +++ b/src/app/lib/configuration.ts @@ -4,14 +4,14 @@ interface Configuration { serviceUrl: string, loginUrl: string, sessionCookie: string, - hautUrl: string + hautUrl: string, + kooditUrl: string }; - - export const configuration: Configuration = { serviceUrl: process.env.SERVICE_URL || 'http://localhost:3404', loginUrl: process.env.LOGIN_URL || 'https://virkailija.untuvaopintopolku.fi/cas/login?service=http://localhost:3404/api/login', sessionCookie: process.env.SESSION_COOKIE || 'JSESSIONID', - hautUrl: `${DOMAIN}/kouta-internal/haku/search` + hautUrl: `${DOMAIN}/kouta-internal/haku/search`, + kooditUrl: 'https://virkailija.untuvaopintopolku.fi/koodisto-service/rest/codeelement/codes/' }; diff --git a/src/app/lib/koodisto.ts b/src/app/lib/koodisto.ts new file mode 100644 index 00000000..08ed3eb0 --- /dev/null +++ b/src/app/lib/koodisto.ts @@ -0,0 +1,37 @@ +import { configuration } from "./configuration"; +import axios from "axios"; +import { TranslatedName, Language } from "./common"; + +export type Koodi = { + koodiUri: string, + nimi: TranslatedName, +} + +async function getKoodit(koodisto: string): Promise { + const headers = { + accept: 'application/json', + 'Caller-id': 'valintojen-toteuttaminen' + }; + + const getTranslatedNimi = (language: Language, metadata: [{nimi: string, kieli: string}]): string => { + const matchingData= metadata.find((m: {nimi: string, kieli: string}) => + Language[m.kieli.toUpperCase() as keyof typeof Language] === language); + return matchingData ? matchingData.nimi : ''; + } + + const response = await axios.get(configuration.kooditUrl + koodisto, {headers}); + return response.data.map((k: { koodiUri: string, metadata: [{nimi: string, kieli: string}]}) => { + const translated = { + fi: getTranslatedNimi(Language.FI, k.metadata), + sv: getTranslatedNimi(Language.SV, k.metadata), + en: getTranslatedNimi(Language.EN, k.metadata), + }; + return {koodiUri: k.koodiUri, nimi: translated}; + }); +} + +export async function getHakutavat(): Promise { + return await getKoodit('hakutapa'); +} + + diff --git a/src/app/lib/kouta.ts b/src/app/lib/kouta.ts index 836374a8..befaff31 100644 --- a/src/app/lib/kouta.ts +++ b/src/app/lib/kouta.ts @@ -2,16 +2,11 @@ import { cookies } from "next/headers"; import { configuration } from "./configuration"; import axios from "axios"; import cookie from 'cookie'; +import { TranslatedName } from "./common"; const KOUTA_COOKIE_NAME = 'session'; const KOUTA_COOKIE_PATH = 'kouta-internal' -type TranslatedName = { - fi?: string, - en?: string, - sv?: string -} - //TODO: check whether any values are optional export type Haku = { oid: string, diff --git a/src/app/lib/login-handler.ts b/src/app/lib/login-handler.ts index 68e1843c..621e5503 100644 --- a/src/app/lib/login-handler.ts +++ b/src/app/lib/login-handler.ts @@ -8,4 +8,4 @@ export async function login() { if (cookies().get(configuration.sessionCookie) === undefined) { redirect(configuration.loginUrl, RedirectType.replace); } -} \ No newline at end of file +} diff --git a/src/app/page.tsx b/src/app/page.tsx index e72613a7..895f1aa6 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -4,19 +4,20 @@ import { getHaut, Haku } from "./lib/kouta"; import { cookies } from 'next/headers'; import { configuration } from "./lib/configuration"; import { HakuSelector } from "./components/haku-selector"; +import { getHakutavat } from "./lib/koodisto"; export default async function Home() { //await login(); const haut = await getHaut(); - console.log(haut); + const hakutavat = await getHakutavat(); return (

Valintojen Toteuttaminen

- +
);