Files
Investidor-V2/resources/js/hooks/use-interest-draft.ts
T

17 lines
1.4 KiB
TypeScript

import { useEffect, useState } from 'react';
import { newSubmissionKey } from '@/lib/portal-api';
export function useInterestDraft<T>(name: string, initial: T) {
const storage = 'investemoz-interest:' + name;
const [draft, setDraft] = useState<{form:T; key:string}>(() => {
try { const value = JSON.parse(sessionStorage.getItem(storage) ?? 'null');
if (value?.form && typeof value.key === 'string') return value;
} catch { /* unavailable storage leaves the current form usable */ }
return { form: initial, key: newSubmissionKey() };
});
useEffect(() => { try { sessionStorage.setItem(storage, JSON.stringify(draft)); } catch {} }, [storage, draft]);
const setForm = (value: T | ((old:T)=>T)) => setDraft(old => ({...old, form: typeof value === 'function' ? (value as (old:T)=>T)(old.form) : value}));
return { form: draft.form, setForm, key: draft.key,
clear: () => { sessionStorage.removeItem(storage); sessionStorage.removeItem('investemoz-participation-identity'); },
authenticate: () => { const identity = draft.form as {name?: string; fullName?: string; email?: string}; sessionStorage.setItem('investemoz-participation-identity', JSON.stringify({name: identity.fullName ?? identity.name ?? '', email: identity.email ?? ''})); sessionStorage.setItem(storage, JSON.stringify(draft)); window.location.assign('/participar/entrar?return=' + encodeURIComponent(location.pathname + location.search)); } };
}