-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add persistence to API import forms #67
Conversation
…mu-delphi/www-epivis into rzatserkovnyi/persistent-forms
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of enumerating saved fields and setting their defaults for every endpoint component (plus the import dialog component) in src/components/dialogs/formSelections.ts
, can we do it piece-by-piece inside each of the individual component definition files? There is already more obfuscation than there needs to be in this app (there is a lot of single-endpoint specific stuff in src/data/data.ts
, /src/deriveLinkDefaults.ts
and /src/api/EpiData.ts
), and having structure and values in a location away from their only usage seems unnecessary. Do we even need all the EndpointFooSelections
classes to be defined, or can the components treat the formSelections
store as a generic object (where they can create and use their own sub-objects as they please)?
I came across this extension which you might be able to attach to the top-level form or to a <div>
that wraps each component to get the persistence for ~free: https://github.com/fawaz-alesayi/svelte-use-persist
export function getFormSelections() { | ||
try { | ||
if (sessionStorage.getItem('form')) { | ||
return JSON.parse(sessionStorage.getItem('form')!) as FormSelections; | ||
} | ||
return new FormSelections(); | ||
} catch { | ||
sessionStorage.removeItem('form'); | ||
return new FormSelections(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simpler:
export function getFormSelections() { | |
try { | |
if (sessionStorage.getItem('form')) { | |
return JSON.parse(sessionStorage.getItem('form')!) as FormSelections; | |
} | |
return new FormSelections(); | |
} catch { | |
sessionStorage.removeItem('form'); | |
return new FormSelections(); | |
} | |
} | |
export function getFormSelections() { | |
try { | |
return JSON.parse(sessionStorage.getItem('form')!) as FormSelections; | |
} catch { | |
sessionStorage.removeItem('form'); | |
} | |
return new FormSelections(); | |
} |
Also, this seems like it could/should go in src/store.ts
since its only ever used there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this to src/store.ts
. However, the if statement should be kept - otherwise it returns JSON.parse(null)
on initial load and causes some bad side effects.
Closing this PR - will implement svelte-use-persist in a separate PR instead; this uses |
And reopening as the
So let's iterate a bit further on this PR. A couple of notes:
I could add these defaults to the
This code also compiles and runs fine, but the current parser we use treats it as an error. (This can likely be fixed by modifying the config for it, though.)
TypeScript/Svelte is fairly insistent on defining these sorts of types - I don't believe it's possible to cleanly use a key/value store like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont love the new formSelections.ts
file that defines form default values separate from the forms themselves, but we have some precedent in that the https://github.com/cmu-delphi/www-epivis/blob/dev/src/data/data.ts file already enumerates form-/endpoint-specific choices for locations and dates and etc... What is here will work, though, with a few bits of cleanup!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closes #62 (but not #63; auth-related fields are omitted here).
formSelections
. This is populated when the application is initially launched, and saved in the browser'slocal storagesession storage.