diff --git a/frontend/src/api/candidate.ts b/frontend/src/api/candidate.ts index d57f460..4608a46 100644 --- a/frontend/src/api/candidate.ts +++ b/frontend/src/api/candidate.ts @@ -1,26 +1,32 @@ import axios from "axios"; -import type { CandidateLogin } from "src/stores/candidate"; +import type { CandidateData, CandidateLogin } from "src/stores/candidate"; import { API_URL, errorHandler } from "."; -export async function apiLogin(data: CandidateLogin): Promise { - axios.post(API_URL + '/candidate/login', data, {withCredentials: true}).then((res) => { - console.log(res); - return res.status === 200; - }).catch((err) => { - throw errorHandler(err, "Login failed"); - }); - return false; +export async function apiLogin(data: CandidateLogin): Promise { + try { + let res = await axios.post(API_URL + '/candidate/login', data, {withCredentials: true}); + return data.application_id; + } catch (e: any) { + throw errorHandler(e, "Login failed"); + } } // TODO -export async function apiLogout(): Promise { +export async function apiLogout() { try { - const res = await axios.get(API_URL + '/candidate/logout', { - withCredentials: true - }); - return res.status === 200; - } catch (error) { - return false; + await axios.post(API_URL + '/candidate/logout', {withCredentials: true}); + } catch (e: any) { + throw errorHandler(e, "Logout failed"); + } +} + +export async function apiFillDetails(data: CandidateData): Promise { + console.log(data); + try { + let res = await axios.post(API_URL + '/candidate/details', data, {withCredentials: true}); + return res.data; + } catch (e: any) { + throw errorHandler(e, "Failed to fill details"); } } \ No newline at end of file diff --git a/frontend/src/routes/login/[code=application]/+page.svelte b/frontend/src/routes/login/[code=application]/+page.svelte index 1e61dc7..75f76ed 100644 --- a/frontend/src/routes/login/[code=application]/+page.svelte +++ b/frontend/src/routes/login/[code=application]/+page.svelte @@ -5,6 +5,7 @@ import { onMount } from 'svelte'; import { page } from '$app/stores'; import { login } from '../../../stores/candidate'; + import { goto } from '$app/navigation'; let applicationId = Number($page.params.code); @@ -38,10 +39,19 @@ }; $: if (codeValueArray.length === 8) { - login({application_id: applicationId, password: codeValueMobile}); - // alert('ApplicationId: ' + applicationId + '; Password: ' + codeValueMobile); + submit(); }; + async function submit() { + try { + await login({application_id: applicationId, password: codeValueMobile}); + goto("/register"); + } catch (e) { + console.error(e); + } + // alert('ApplicationId: ' + applicationId + '; Password: ' + codeValueMobile); + } + onMount(() => { codeElementArray[0].focus(); }); diff --git a/frontend/src/stores/candidate.ts b/frontend/src/stores/candidate.ts index d12f3ac..ed7f9f8 100644 --- a/frontend/src/stores/candidate.ts +++ b/frontend/src/stores/candidate.ts @@ -1,11 +1,23 @@ -import { apiLogin, apiLogout } from "../api/candidate"; +import { apiFillDetails, apiLogin, apiLogout } from "../api/candidate"; import { writable } from "svelte/store"; export interface CandidateData { - id?: string; - name?: string; - surname?: string; - email?: string; + // id?: string; + name: string; + surname: string; + birthplace: string; + birthdate: string; + address: string; + telephone: string; + citizenship: string; + email: string; + sex: string; + study: string; + personalIdNumber: string; + parentName: string; + parentSurname: string; + parentTelephone: string; + parentEmail: string; } export interface CandidateLogin { @@ -17,11 +29,47 @@ export const candidateData = writable(); export async function login(data: CandidateLogin) { // TODO: handle errors - let res = await apiLogin(data); // TODO: set candidate data from response to store + try { + let applicationId = await apiLogin(data); // TODO: set candidate data from response to store + console.log("login result: " + applicationId); + } catch (e) { + console.error("login failed"); + throw e; + } } export async function logout() { // TOOD: handle errors - await apiLogout(); - candidateData.set({}); + try { + await apiLogout(); + candidateData.set({ + name: "", + surname: "", + birthplace: "", + birthdate: "", + address: "", + telephone: "", + citizenship: "", + email: "", + sex: "", + study: "", + personalIdNumber: "", + parentName: "", + parentSurname: "", + parentTelephone: "", + parentEmail: "" + }); + } catch (e) { + console.error(e); + } +} + +export async function fillDetails(data: CandidateData) { + try { + let res = await apiFillDetails(data); + candidateData.set(res); + } catch (e) { + console.error(e); + throw e; + } } \ No newline at end of file