From 9df7c9f3ef296e64979f0ddfe1bf95ff5d4d5701 Mon Sep 17 00:00:00 2001 From: Risto Salama Date: Wed, 27 Mar 2024 12:16:56 +0200 Subject: [PATCH] Alkamiskausi suodatin haulle --- src/app/api/login/route.ts | 2 +- src/app/components/haku-selector.tsx | 33 ++++++++++++++++++++++++++++ src/app/lib/kouta.ts | 11 +++++++--- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/app/api/login/route.ts b/src/app/api/login/route.ts index 22017001..09402814 100644 --- a/src/app/api/login/route.ts +++ b/src/app/api/login/route.ts @@ -43,7 +43,7 @@ export async function GET(req: NextRequest) { 'Caller-id': 'valintojen-toteuttaminen', 'redirect-strategy': 'none', 'throw-exception': false, - cookie: cookie.serialize('session', 'InGhVP1zBIjI-u3SjO-x5PzrycA' || '', {httpOnly: true, path: '/'}) + cookie: cookie.serialize('session', 'session' || '', {httpOnly: true, path: '/'}) }; //const login = await axios.get('https://virkailija.untuvaopintopolku.fi/kouta-internal/auth/login'); //console.log(login.headers); diff --git a/src/app/components/haku-selector.tsx b/src/app/components/haku-selector.tsx index d57f8473..4c2ca39f 100644 --- a/src/app/components/haku-selector.tsx +++ b/src/app/components/haku-selector.tsx @@ -4,19 +4,45 @@ import { useState } from "react"; import { SearchDropdown } from "./search-dropdown"; import { Haku, Tila } from "../lib/kouta"; +//TODO: move HaunAlkaminen getHakuAlkamisKaudet to some util for example +const STARTING_YEAR = 2019; // check earliest kouta haku + +type HaunAlkaminen = { + alkamisVuosi: number, + alkamisKausiKoodiUri: string, + alkamisKausiNimi: string, +} + +const getHakuAlkamisKaudet = (): HaunAlkaminen[] => { + const nowYear = new Date().getFullYear(); + const alkamiset: HaunAlkaminen[] = []; + for (let i = nowYear; i >= STARTING_YEAR; i--) { + alkamiset.push({alkamisVuosi: i, alkamisKausiKoodiUri: 'kausi_s', alkamisKausiNimi: 'SYKSY'}) + alkamiset.push({alkamisVuosi: i, alkamisKausiKoodiUri: 'kausi_k', alkamisKausiNimi: 'KEVÄT'}) + } + return alkamiset; +} + +const alkamisKausiMatchesSelected = (haku: Haku, selectedAlkamisKausi: HaunAlkaminen): boolean => + haku.alkamisVuosi === selectedAlkamisKausi.alkamisVuosi && haku.alkamisKausiKoodiUri.startsWith(selectedAlkamisKausi.alkamisKausiKoodiUri); + export const HakuSelector = ({haut}: {haut : Haku[]}) =>{ const [results, setResults] = useState(); const [searchTila, setSearchTila] = useState(Tila.JULKAISTU); const [selectedHaku, setSelectedHaku] = useState(); + const [selectedAlkamisKausi, setSelectedAlkamisKausi] = useState(); + const alkamisKaudet = getHakuAlkamisKaudet(); type changeHandler = React.ChangeEventHandler; + const handleChange: changeHandler = (e) => { const { target } = e; if (!target.value.trim()) return setResults([]); const filteredValue = haut.filter((haku: Haku) => haku.tila == searchTila && haku.nimi.fi?.toLowerCase().includes(target.value.toLowerCase()) + && (!selectedAlkamisKausi || alkamisKausiMatchesSelected(haku, selectedAlkamisKausi)) ); setResults(filteredValue); }; @@ -35,6 +61,13 @@ export const HakuSelector = ({haut}: {haut : Haku[]}) =>{ onChange={handleChange} onSelect={(h: Haku) => setSelectedHaku(h)} /> + + ); } diff --git a/src/app/lib/kouta.ts b/src/app/lib/kouta.ts index 3daccf15..836374a8 100644 --- a/src/app/lib/kouta.ts +++ b/src/app/lib/kouta.ts @@ -12,10 +12,14 @@ type TranslatedName = { sv?: string } +//TODO: check whether any values are optional export type Haku = { oid: string, nimi: TranslatedName, - tila: Tila + tila: Tila, + alkamisVuosi: number, + alkamisKausiKoodiUri: string, + hakutapaKoodiUri: string } export enum Tila { @@ -43,9 +47,10 @@ export async function getHaut(active: boolean = true) { const response = await axios.get(configuration.hautUrl, { headers, }); - const haut: Haku[] = response.data.map((h: { oid: any; nimi: any; tila: string}) => { + const haut: Haku[] = response.data.map((h: { oid: string; nimi: TranslatedName; tila: string, hakutapaKoodiUri: string, hakuvuosi: string, hakukausi: string}) => { const haunTila: Tila = Tila[h.tila.toUpperCase() as keyof typeof Tila]; - return {oid: h.oid, nimi: h.nimi, tila: haunTila}; + return {oid: h.oid, nimi: h.nimi, tila: haunTila, hakutapaKoodiUri: h.hakutapaKoodiUri, + alkamisVuosi: parseInt(h.hakuvuosi), alkamisKausiKoodiUri: h.hakukausi}; }); return haut; }