-
Notifications
You must be signed in to change notification settings - Fork 1
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
[MPDX-8393] Add useUserPreference
hook
#1146
Conversation
Preview branch generated at https://8393-user-preference.d3dytjb8adxkk5.amplifyapp.com |
const temp = [...flowOptions]; | ||
if (originIndex > -1) { | ||
temp[originIndex] = { | ||
...temp[originIndex], | ||
statuses: temp[originIndex].statuses.filter( | ||
(status) => status !== draggedStatus, | ||
), | ||
}; | ||
} | ||
if (destinationIndex > -1) { | ||
temp[destinationIndex] = { | ||
...temp[destinationIndex], | ||
statuses: [...temp[destinationIndex].statuses, draggedStatus], | ||
}; | ||
} |
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.
Before this was updating cloning the array of flow options but still modifying the original column. This clones the column too before updating it.
@@ -0,0 +1,15 @@ | |||
query UserOption($key: String!) { |
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.
This query and mutation doesn't need to load the id
field anymore because I changed the cache to use the key
to generate Option
s' unique id.
@@ -80,6 +82,15 @@ export const createCache = () => | |||
people: paginationFieldPolicy, | |||
tasks: paginationFieldPolicy, | |||
userNotifications: paginationFieldPolicy, | |||
// When loading a user option, look it up from the cache by its key | |||
userOption: { |
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.
This makes it so that if some other component loaded all the user options with the userOptions
query, the userOption
(singular) query can pull the option out of the cache by its key instead of having to wait for it to load.
Bundle sizes [mpdx-react]Compared against d59ffdd No significant changes found |
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.
This is great!
} | ||
|
||
if (typeof defaultValue === 'string') { | ||
setValue((data.userOption.value ?? defaultValue) as T); |
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.
Why do you not check if data.userOption.value
needs to be pared as JSON here like you do below?
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.
This hook assumes that if defaultValue
is a string, the component just needs to save a simple string as the preference. For example the view mode, which could be "list", "flows", or "map". If defaultValue
isn't an object, it assumes that the component needs to save an object or array as the preference and does the automatic JSON serialization/deserialization. This branch is the simple string code path.
9c41fa4
to
0401c51
Compare
0401c51
to
d830fb2
Compare
Description
Create a new
useUserPreference
hook that is as ergonomic asuseState
but syncs its value with a user preference.Usage:
Features:
setValue
updatesvalue
immediately without having to wait for the server to respond.defaultValue
isn't a string, the value is transparently serialized and deserialized as JSON.userOptions
query, this hook can extract the relevant option from the Apollo cache immediately before theuserOption
query returns.Depends on https://github.com/CruGlobal/mpdx_api/pull/2872
I updated the code to use this new hook for the
setup_position
, andflows
(the flows columns) preferences to prove its usefulness. It could also be used for the view mode preference once #1131 is merged. I will also use it in the upcoming PR for the dismissible beacon.Checklist: