diff --git a/components/discussion-form.js b/components/discussion-form.js index 0335aad0..f3461ed3 100644 --- a/components/discussion-form.js +++ b/components/discussion-form.js @@ -74,6 +74,7 @@ export function DiscussionForm ({ router.push('/recent') } })} + storageKeyPrefix={item ? undefined : 'discussion'} > { if (overrideValue) { helpers.setValue(overrideValue) + if (storageKey) { + localStorage.setItem(storageKey, overrideValue) + } + } else if (storageKey) { + const draft = localStorage.getItem(storageKey) + if (draft) { + // for some reason we have to turn off validation to get formik to + // not assume this is invalid + helpers.setValue(draft, false) + } } }, [overrideValue]) @@ -142,6 +157,11 @@ function InputInner ({ prepend, append, hint, showValid, onChange, overrideValue {...field} {...props} onChange={(e) => { field.onChange(e) + + if (storageKey) { + localStorage.setItem(storageKey, e.target.value) + } + if (onChange) { onChange(formik, e) } @@ -204,7 +224,7 @@ export function Checkbox ({ children, label, extra, handleChange, inline, ...pro } export function Form ({ - initial, schema, onSubmit, children, initialError, validateImmediately, ...props + initial, schema, onSubmit, children, initialError, validateImmediately, storageKeyPrefix, ...props }) { const [error, setError] = useState(initialError) @@ -215,11 +235,24 @@ export function Form ({ initialTouched={validateImmediately && initial} validateOnBlur={false} onSubmit={async (...args) => - onSubmit && onSubmit(...args).catch(e => setError(e.message || e))} + onSubmit && onSubmit(...args).then(() => { + if (!storageKeyPrefix) return + console.log(...args) + Object.keys(...args).forEach(v => + localStorage.removeItem(storageKeyPrefix + '-' + v)) + }).catch(e => setError(e.message || e))} > {error && setError(undefined)} dismissible>{error}} - {children} + {storageKeyPrefix + ? React.Children.map(children, (child) => { + if (child) { + return React.cloneElement(child, { + storageKeyPrefix + }) + } + }) + : children} ) diff --git a/components/link-form.js b/components/link-form.js index a715f6ea..288dc45c 100644 --- a/components/link-form.js +++ b/components/link-form.js @@ -105,6 +105,7 @@ export function LinkForm ({ item, editThreshold }) { router.push('/recent') } }} + storageKeyPrefix={item ? undefined : 'link'} >