49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
import type { RegistrationDraft } from "../../../features/register-account";
|
|
|
|
const storageKey = "lincodex-registration-context";
|
|
|
|
export type RegistrationContext = RegistrationDraft & {
|
|
accepted: boolean;
|
|
};
|
|
|
|
const emptyContext: RegistrationContext = { email: "", scenario: "", accepted: false };
|
|
|
|
export function readRegistrationContext(): RegistrationContext {
|
|
if (typeof window === "undefined") return emptyContext;
|
|
try {
|
|
const raw = window.sessionStorage.getItem(storageKey);
|
|
if (!raw) return emptyContext;
|
|
const parsed: unknown = JSON.parse(raw);
|
|
if (typeof parsed !== "object" || parsed === null) return emptyContext;
|
|
const value = parsed as Record<string, unknown>;
|
|
const email = typeof value.email === "string" ? value.email : "";
|
|
const scenario =
|
|
value.scenario === "candidate" || value.scenario === "employer" ? value.scenario : "";
|
|
return {
|
|
email,
|
|
scenario,
|
|
accepted: value.accepted === true && email.length > 0 && scenario !== "",
|
|
};
|
|
} catch {
|
|
return emptyContext;
|
|
}
|
|
}
|
|
|
|
export function writeRegistrationContext(context: RegistrationContext): void {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
window.sessionStorage.setItem(storageKey, JSON.stringify(context));
|
|
} catch {
|
|
// Registration remains usable when the browser blocks session storage.
|
|
}
|
|
}
|
|
|
|
export function clearRegistrationContext(): void {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
window.sessionStorage.removeItem(storageKey);
|
|
} catch {
|
|
// Registration remains usable when the browser blocks session storage.
|
|
}
|
|
}
|