Entrega dashboard e correccoes transversais preservando Ofertas instalado
tests / ci (push) Canceled after 0s
tests / ci (push) Canceled after 0s
This commit is contained in:
@@ -2,16 +2,26 @@ 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 }>(() => {
|
||||
const read = () => {
|
||||
try {
|
||||
const value = JSON.parse(sessionStorage.getItem(storage) ?? 'null');
|
||||
if (value?.form && typeof value.key === 'string') return value;
|
||||
if (value?.form && typeof value.key === 'string')
|
||||
return { ...value, storage } as {
|
||||
form: T;
|
||||
key: string;
|
||||
storage: string;
|
||||
};
|
||||
} catch {
|
||||
/* unavailable storage leaves the current form usable */
|
||||
}
|
||||
return { form: initial, key: newSubmissionKey() };
|
||||
});
|
||||
return { form: initial, key: newSubmissionKey(), storage };
|
||||
};
|
||||
const [draft, setDraft] = useState(read);
|
||||
// Inertia can reuse this component for a different opportunity. Never write
|
||||
// the previous opportunity's answers into the new opportunity's storage key.
|
||||
if (draft.storage !== storage) setDraft(read());
|
||||
useEffect(() => {
|
||||
if (draft.storage !== storage) return;
|
||||
try {
|
||||
sessionStorage.setItem(storage, JSON.stringify(draft));
|
||||
} catch {}
|
||||
|
||||
Reference in New Issue
Block a user