Skip to content

Commit

Permalink
Alkamiskausi suodatin haulle
Browse files Browse the repository at this point in the history
  • Loading branch information
SalamaGofore committed Mar 27, 2024
1 parent 08fb526 commit 9df7c9f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/app/api/login/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions src/app/components/haku-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Haku[]>();
const [searchTila, setSearchTila] = useState<Tila>(Tila.JULKAISTU);
const [selectedHaku, setSelectedHaku] = useState<Haku>();
const [selectedAlkamisKausi, setSelectedAlkamisKausi] = useState<HaunAlkaminen | undefined>();

const alkamisKaudet = getHakuAlkamisKaudet();
type changeHandler = React.ChangeEventHandler<HTMLInputElement>;

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);
};
Expand All @@ -35,6 +61,13 @@ 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>
);
}
11 changes: 8 additions & 3 deletions src/app/lib/kouta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

0 comments on commit 9df7c9f

Please sign in to comment.