-
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
base: dev
Are you sure you want to change the base?
Conversation
src/api/EpiData.ts
Outdated
@@ -9,7 +9,7 @@ import { | |||
fluViewRegions, | |||
gftLocations, | |||
ghtLocations, | |||
nidssDenqueLocations, | |||
nidssDengueLocations, |
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.
The NIDSS Dengue signal is referred to as "denque" in a few parts of the codebase. Because of this, it is currently broken on https://delphi.cmu.edu/epivis/. This PR also renames all mentions of "denque" to the correct "dengue".
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.
Nice catch, but it is preferable to have this set of changes in its own PR... Could we break them out?
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.
Done! #68
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.
Awesome! Can you merge #68 and then rebase this PR?
…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
@@ -17,6 +18,11 @@ export const isShowingPoints = writable(defaults.showPoints); | |||
export const initialViewport = writable(defaults.viewport); | |||
export const navMode = writable(NavMode.autofit); | |||
|
|||
export const formSelections = writable(getFormSelections()); | |||
formSelections.subscribe((val) => { |
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.
Should this register a handler to unsubscribe when it goes out of scope or gets collected or whatever?
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.
That should be handled automatically. (Edit: I doublechecked and the subscribe
function runs only once even if the "import API dialog" form is closed/opened/submitted a bunch of times)
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 |
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.