Skip to content

Commit

Permalink
Hakutapa suodatin hauille
Browse files Browse the repository at this point in the history
  • Loading branch information
SalamaGofore committed Mar 27, 2024
1 parent 9df7c9f commit 7472ddf
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 21 deletions.
31 changes: 23 additions & 8 deletions src/app/components/haku-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Haku[]>();
const [searchTila, setSearchTila] = useState<Tila>(Tila.JULKAISTU);
const [selectedHaku, setSelectedHaku] = useState<Haku>();
const [selectedAlkamisKausi, setSelectedAlkamisKausi] = useState<HaunAlkaminen | undefined>();
const [selectedHakutapa, setSelectedHakutapa] = useState<Koodi | undefined>();

const alkamisKaudet = getHakuAlkamisKaudet();

type changeHandler = React.ChangeEventHandler<HTMLInputElement>;

const handleChange: changeHandler = (e) => {
Expand All @@ -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);
};
Expand All @@ -61,13 +65,24 @@ export const HakuSelector = ({haut}: {haut : Haku[]}) =>{
onChange={handleChange}
onSelect={(h: Haku) => setSelectedHaku(h)}
/>
<label htmlFor="alkamiskausi-select">Koulutuksen alkamiskausi</label>
<select name="alkamiskausi-select" onChange={(e) => setSelectedAlkamisKausi(e.target.value? alkamisKaudet[parseInt(e.target.value)]: undefined)}>
<option value={undefined}>Valitse...</option>
{alkamisKaudet.map((kausi, index) => {
return <option value={index} key={kausi.alkamisVuosi + kausi.alkamisKausiKoodiUri}>{kausi.alkamisVuosi} {kausi.alkamisKausiNimi}</option> //TODO: translate
})}
</select>
<div>
<label htmlFor="hakutapa-select">Hakutapa</label>
<select name="hakutapa-select" onChange={(e) => setSelectedHakutapa(e.target.value? hakutavat[parseInt(e.target.value)]: undefined)}>
<option value={undefined}>Valitse...</option>
{hakutavat.map((tapa, index) => {
return <option value={index} key={tapa.koodiUri}>{tapa.nimi.fi}</option> //TODO: translate
})}
</select>
</div>
<div>
<label htmlFor="alkamiskausi-select">Koulutuksen alkamiskausi</label>
<select name="alkamiskausi-select" onChange={(e) => setSelectedAlkamisKausi(e.target.value? alkamisKaudet[parseInt(e.target.value)]: undefined)}>
<option value={undefined}>Valitse...</option>
{alkamisKaudet.map((kausi, index) => {
return <option value={index} key={kausi.alkamisVuosi + kausi.alkamisKausiKoodiUri}>{kausi.alkamisVuosi} {kausi.alkamisKausiNimi}</option> //TODO: translate
})}
</select>
</div>
</div>
);
}
9 changes: 9 additions & 0 deletions src/app/lib/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type TranslatedName = {
fi?: string,
en?: string,
sv?: string
}

export enum Language {
FI, EN, SV
}
8 changes: 4 additions & 4 deletions src/app/lib/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
};
37 changes: 37 additions & 0 deletions src/app/lib/koodisto.ts
Original file line number Diff line number Diff line change
@@ -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<Koodi[]> {
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<Koodi[]> {
return await getKoodit('hakutapa');
}


7 changes: 1 addition & 6 deletions src/app/lib/kouta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/app/lib/login-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export async function login() {
if (cookies().get(configuration.sessionCookie) === undefined) {
redirect(configuration.loginUrl, RedirectType.replace);
}
}
}
5 changes: 3 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
<h1>Valintojen Toteuttaminen</h1>
<HakuSelector haut={haut}/>
<HakuSelector haut={haut} hakutavat={hakutavat}/>
</div>
</main>
);
Expand Down

0 comments on commit 7472ddf

Please sign in to comment.