feat: local storage

This commit is contained in:
Sebastian Pravda 2022-12-02 16:00:46 +01:00 committed by EETagent
parent bfa04e0206
commit ffba0bc828
2 changed files with 30 additions and 11 deletions

View file

@ -12,25 +12,22 @@
let fullName = "";
let email = "";
$: if ($candidateData === undefined) {
$: if ($candidateData) {
fullName = ($candidateData.name ?? "") + " " + ($candidateData.surname ?? "");
email = $candidateData.email ?? "";
}
$: if ($candidateData.name === undefined) {
fetch();
} else {
setNameEmail();
}
const fetch = async () => {
try {
await fetchDetails();
setNameEmail();
} catch {
console.error("error");
}
}
const setNameEmail = async () => {
fullName = $candidateData.name + " " + $candidateData.surname;
email = $candidateData.email ?? "!Nevyplněné údaje!";
}
</script>
<FullLayout>

View file

@ -1,6 +1,6 @@
import { apiFetchDetails, apiFillDetails, apiLogin, apiLogout } from "../@api/candidate";
import { writable } from "svelte/store";
import type { ApiError } from "src/@api";
import { browser } from "$app/environment";
export interface CandidateData {
name?: string;
@ -25,7 +25,29 @@ export interface CandidateLogin {
password: string;
}
export const candidateData = writable<CandidateData>();
export const candidateData = writable<CandidateData>({});
if (browser) {
const name = localStorage.getItem("name");
const surname = localStorage.getItem("surname");
const email = localStorage.getItem("email");
if (name && email && surname) {
candidateData.set({
name,
surname,
email
});
}
}
candidateData.subscribe((val) => {
if (browser) {
localStorage.setItem("name", val.name ?? "");
localStorage.setItem("surname", val.surname ?? "");
localStorage.setItem("email", val.email ?? "");
}
})
export async function login(data: CandidateLogin) {
// TODO: handle errors