Skip to content
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

STCOR-936 - implement App-reordering, user preference management in stripes-core. #1584

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

JohnC-80
Copy link
Contributor

@JohnC-80 JohnC-80 commented Jan 27, 2025

STCOR-936

This PR Implements 2 things -

  1. A centralized mechanism for managing user preferences and interacting with mod-settings.
  2. A context that reads/writes a persisted user preference for the order of application icons in the main navigation of the UI.

1 is fairly straightforward. A hook - usePreferences - that accepts an object of options with the key and scope of a user preference. It will automatically apply the logged in user's id as well as maintain the id of an existing preference in its internal state. As per typical, it try/catches the requests. TODO: Better error handling than console.log.

usePreferences supplies methods getPreference, setPreference, and deletePreference.

getPreference/setPreference queries the preferences with the userId, scope, and key. If the preference is found, it will grab the id and store that in local state to use when setPreference is called. If a preference is not found, then the id remains null and when setPreference is called, a new preference is created according to mod-settings' API. deletePreference removes the preference and resets the found preference id if there is any.

2 involved setting up a context with a Provider - AppOrderProvider- that wraps at a high level - outside of the main navigation and around the ui-module container. It access the user preference and provides an ordered list of apps filtered by user permissions (named apps). It also provides the persisted preference value - an array containing objects with app information - only name as well as an isNew field if a platform app does not have a corresponding list order value.

The app list -building logic was lifted from MainNav.js and <MainNav> refactored as a functional component, trading out HOC's for hooks. One detail that's subject to further inspection is line 58 where it uses logout instead of an apparently undefined returnToLogin function.

ResizeContainer and AppListDropdown were two components that had to be touched as they were forcing alphabetical order of the list - switching from Object.keys to using the order of the array of apps/preference.

Still Remaining:

  • Tests!
  • Cleanup!
  • Anything else that comes up in review...

Copy link

github-actions bot commented Jan 27, 2025

Bigtest Unit Test Results

189 tests  ±0   184 ✅ ±0   6s ⏱️ ±0s
  1 suites ±0     5 💤 ±0 
  1 files   ±0     0 ❌ ±0 

Results for commit 0c6e189. ± Comparison against base commit 2049911.

This pull request removes 191 and adds 189 tests. Note that renamed tests count towards both.
      equal to check email label in english translation
      equal to check email precautions label in english translation
      equal to sent email precautions label in english translation
Chrome_131_0_0_0_(Linux_x86_64).AppIcon Passing a string to the tag-prop ‑ AppIcon Passing a string to the tag-prop Should render an AppIcon with a HTML tag of "div"
Chrome_131_0_0_0_(Linux_x86_64).AppIcon Passing a string using the children-prop ‑ AppIcon Passing a string using the children-prop Should render an AppIcon with a label
Chrome_131_0_0_0_(Linux_x86_64).AppIcon Rendering an AppIcon using Stripes-context ‑ AppIcon Rendering an AppIcon using Stripes-context Should render an <img>
Chrome_131_0_0_0_(Linux_x86_64).AppIcon Rendering an AppIcon using Stripes-context ‑ AppIcon Rendering an AppIcon using Stripes-context Should render an img with an alt-attribute
Chrome_131_0_0_0_(Linux_x86_64).AppIcon Rendering an AppIcon using an icon-object ‑ AppIcon Rendering an AppIcon using an icon-object Should render an <img>
Chrome_131_0_0_0_(Linux_x86_64).AppIcon Rendering an AppIcon using an icon-object ‑ AppIcon Rendering an AppIcon using an icon-object Should render an img with an alt-attribute
Chrome_131_0_0_0_(Linux_x86_64).AppIcon Rendering an AppIcon using an icon-object ‑ AppIcon Rendering an AppIcon using an icon-object Should render with a className of "My className"
…
Chrome_132_0_0_0_(Linux_x86_64).AppIcon Passing a string to the tag-prop ‑ AppIcon Passing a string to the tag-prop Should render an AppIcon with a HTML tag of "div"
Chrome_132_0_0_0_(Linux_x86_64).AppIcon Passing a string using the children-prop ‑ AppIcon Passing a string using the children-prop Should render an AppIcon with a label
Chrome_132_0_0_0_(Linux_x86_64).AppIcon Rendering an AppIcon using Stripes-context ‑ AppIcon Rendering an AppIcon using Stripes-context Should render an <img>
Chrome_132_0_0_0_(Linux_x86_64).AppIcon Rendering an AppIcon using Stripes-context ‑ AppIcon Rendering an AppIcon using Stripes-context Should render an img with an alt-attribute
Chrome_132_0_0_0_(Linux_x86_64).AppIcon Rendering an AppIcon using an icon-object ‑ AppIcon Rendering an AppIcon using an icon-object Should render an <img>
Chrome_132_0_0_0_(Linux_x86_64).AppIcon Rendering an AppIcon using an icon-object ‑ AppIcon Rendering an AppIcon using an icon-object Should render an img with an alt-attribute
Chrome_132_0_0_0_(Linux_x86_64).AppIcon Rendering an AppIcon using an icon-object ‑ AppIcon Rendering an AppIcon using an icon-object Should render with a className of "My className"
Chrome_132_0_0_0_(Linux_x86_64).AppIcon Size tests Passing a size of "large" ‑ AppIcon Size tests Passing a size of "large" Should render an icon into a large-sized container
Chrome_132_0_0_0_(Linux_x86_64).AppIcon Size tests Passing a size of "medium" ‑ AppIcon Size tests Passing a size of "medium" Should render an icon into a medium-sized container
Chrome_132_0_0_0_(Linux_x86_64).AppIcon Size tests Passing a size of "small" ‑ AppIcon Size tests Passing a size of "small" Should render an icon into a small-sized container
…
This pull request removes 5 skipped tests and adds 5 skipped tests. Note that renamed tests count towards both.
Chrome_131_0_0_0_(Linux_x86_64).PasswordValidationField with an invalid password ‑ PasswordValidationField with an invalid password shows a validation error
Chrome_131_0_0_0_(Linux_x86_64).useCustomFields hook requests for existing custom fields ‑ useCustomFields hook requests for existing custom fields should have custom fields
Chrome_131_0_0_0_(Linux_x86_64).useCustomFields hook requests for non-existing custom fields ‑ useCustomFields hook requests for non-existing custom fields should have error
Chrome_131_0_0_0_(Linux_x86_64).useCustomFields hook requests for version-compatible custom fields ‑ useCustomFields hook requests for version-compatible custom fields should have custom fields
Chrome_131_0_0_0_(Linux_x86_64).useCustomFields hook requests for version-incompatible custom fields ‑ useCustomFields hook requests for version-incompatible custom fields should have error
Chrome_132_0_0_0_(Linux_x86_64).PasswordValidationField with an invalid password ‑ PasswordValidationField with an invalid password shows a validation error
Chrome_132_0_0_0_(Linux_x86_64).useCustomFields hook requests for existing custom fields ‑ useCustomFields hook requests for existing custom fields should have custom fields
Chrome_132_0_0_0_(Linux_x86_64).useCustomFields hook requests for non-existing custom fields ‑ useCustomFields hook requests for non-existing custom fields should have error
Chrome_132_0_0_0_(Linux_x86_64).useCustomFields hook requests for version-compatible custom fields ‑ useCustomFields hook requests for version-compatible custom fields should have custom fields
Chrome_132_0_0_0_(Linux_x86_64).useCustomFields hook requests for version-incompatible custom fields ‑ useCustomFields hook requests for version-incompatible custom fields should have error

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Jan 27, 2025

Jest Unit Test Results

  1 files  ±0   64 suites  +2   1m 34s ⏱️ -2s
370 tests +6  357 ✅  - 7  0 💤 ±0  13 ❌ +13 
373 runs  +6  360 ✅  - 7  0 💤 ±0  13 ❌ +13 

For more details on these failures, see this check.

Results for commit 0c6e189. ± Comparison against base commit 2049911.

♻️ This comment has been updated with latest results.

Comment on lines 83 to 60
this.returnToLogin();
// this.returnToLogin();
logout(state.okapi.url, store, queryClient);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In RTR-land where tokens only exist in HTTPOnly cookies, we don't have to worry about this condition because state.okapi.token will never evaluate to true. IOW, I think the right clean-up here is "remove this entire conditional".

It looks like the bad commit is a9b860dfe591f7e7b236640fc1ceb6d25401b30c, itself a "clean-up" commit from mega-merge PR #1532. The comment there alludes to fixing up logout, but unfortunately doesn't offer any details about why, specifically, returnToLogin was removed. If anybody knows this @zburke character we should find him and tell him to write better commit messages.

Copy link
Contributor

@MikeTaylor MikeTaylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not read every line of this, but it looks good.

},
{
"permissionName": "mod-settings.owner.read.stripes-core.prefs.manage",
"displayName": "UI: read the user's own preference for the order of links in the main navigation.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we say something like "UI: read the user's own preferences, such as the order of links in the main navigation"? We are surely going to want to use this for lots of other things, too, once it's in place.

},
{
"permissionName": "mod-settings.owner.write.stripes-core.prefs.manage",
"displayName": "UI: update the user's own preference for the order of links in the main navigation.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment.

@@ -91,7 +91,7 @@ class AppList extends Component {
{
apps.map(app => {
const isHidden = hiddenItemIds.includes(app.id);

// const isHidden = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this about?

const id = `clickable-${name}-module`;

const pathRoot = pathname.split('/')[1];
const entryRoot = entry.route.split('/')[1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks this means each FOLIO UI module has to have a unique top-level path component — e.g. you can't have ui-foobar that uses /foo/bar and ui-foobaz that uses /foo/baz. Am I reading that right? If so, is that limitation new, or was it there in the old code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, @MikeTaylor. It is not a new limitation; old code is identical and hasn't changed in 7+ years. Before that, history is too murky to easily assess, so I don't know if the limitation has always been there but certainly it's been there for a long time.

const { getPreference, setPreference, removePreference } = usePreferences();

const { data: userAppList, isLoading, refetch: refetchAppList } = useQuery('UserPrefsAppListing', () => {
return getPreference({ key: APPORDER_PREF_NAME, scope: APPORDER_PREF_SCOPE });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use an expression here rather than a block that does nothing but return the expression?

respJSON = await resp.json();
if (respJSON.items.length === 1) {
setId(respJSON.items[0].id);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else ... ?

const userId = user.user.id;

const getPreference = useCallback(async ({ scope, key, callback }) => {
// const res = await localforage.getItem(key, callback);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's going on with this commented-out line?

// so we use the `POST` endpoint for saving... 'PUT', and including the id in the path is used for updating
if (!id) {
try {
setId(prefId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make more sense to to move this line after the ky.post, so we only set the ID if the operation was successful?

try {
return ky.put(`settings/entries/${prefId}`, { json: payload });
} catch (err) {
console.log(`error updating preference at scope: ${scope}, and key: ${key} for user: ${userId} - ${err.message}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move this outside the try block to avoid duplicating it.

try {
if (id) {
await ky.delete(`settings/entries/${id}`);
setId(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see we use ky.delete and then setId for deletion, as I think we should for creation.

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
60.7% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants