fix: complete InvesteMoz validation and error corrections
This commit is contained in:
@@ -2,15 +2,54 @@ 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');
|
||||
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 */ }
|
||||
} 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)); } };
|
||||
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),
|
||||
);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user