Skip to content

Query membership endpoints for resources belonging to user lists and learning paths #1846

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

Merged
merged 13 commits into from
Nov 25, 2024

Conversation

jonkafton
Copy link
Contributor

@jonkafton jonkafton commented Nov 21, 2024

What are the relevant tickets?

Closes https://github.com/mitodl/hq/issues/6032

Description (What does it do?)

Provides hooks for components to determine a resource's User List and Learning Path memberships. This replaces relationships being specified on resources in their user_list_parents and learning_path_parents arrays, so we have been able to remove all the query cache invalidation logic that determined which resources were stale when change were made to the parent lists. We now just invalidate the respective membershipList caches (on userLists and learningPaths).

How can this be tested?

Resource card and learning drawer action buttons should highlight according to User List and Learning Path memberships and should be responsive to changes made in the Add to List dialogs.

Changes made in the Add to List dialogs in one screen should be reflected in other screens while navigating without page refresh (Homepage, Search, User List (/dashboard/my-lists/1) and Learning Path details (/learningpaths/1) pages.

If the only User List or Learning Path that a Learning Resource is a member of is deleted, the action button should no longer be highlighted.

Additional Context

The membership lists previously lived on user_list_parents and learning_path_parents on learning resources, however these are specific to a user so cannot be cached on public CDN. Membership lists endpoints have been added to the API with #1808 so that they can be queried for independently of the learning resources.

These do still exist on the API and can be cleaned out, but for now are overriden to [] in the query hooks to ensure no remaining stray dependency.

  • /api/v1/learning_resources/{id}/
  • /api/v1/learning_resources/
  • /api/v1/featured/
  • /api/v1/learning_resources_search/
  • /api/v1/userlists/{userlist_id}/items/
  • /api/v1/learningpaths/{learning_resource_id}/items/

Also fixes (by bypass) an issue in Prod where resources are not bookmarked in the User List details screen as user_list_parents on /api/v1/userlists/{userlist_id}/items/ is always empty.

@jonkafton jonkafton marked this pull request as draft November 21, 2024 20:23
@jonkafton jonkafton marked this pull request as ready for review November 22, 2024 13:30
Comment on lines 226 to 231
const checkbox = await screen.findByLabelText<HTMLInputElement>(title)

expect(checkbox.checked).toBe(true)
await waitFor(() => {
expect(checkbox.checked).toBe(true)
})

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: Drop the waitFor, and change the query to

      const checkbox = await screen.findByRole<HTMLInputElement>("checkbox", {
        name: title,
        checked: true,
      })

(findBy whats, and if you switch it to findByRole you can add the checked option)

await setLearningPathRelationships({
id: resource.id,
learning_path_id: newParents,
})
} else if (listType === ListType.UserList) {
const newParents = values.user_lists.map((id) => parseInt(id))
const newParents = values.user_lists!.map((id) => parseInt(id))
Copy link
Contributor

Choose a reason for hiding this comment

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

Requested change: Here and above, let's get rid of the non-null assertion. My rec instead would be to set defaults on the user_lists and learning_paths:

  const formik = useFormik({
    enableReinitialize: true,
    initialValues: {
      learning_paths: learningPathValues ?? [],
      user_lists: userListValues ?? [],
    },
    // etc ...
  })

Opinion: We should not use obj!.prop. This has caused UI crashes in the past (New Resource Card: null image crashes page#4530) that would have been caught by TS had we not used a non-null assertion.

Additionally

I think the checkboxes should be disabled while membership info is still loading. This should be as simple as saying

<CheckboxChoiceField disabled={
  !userListValues /* or maybe grab isLoading off the query*/
} />

but apparently CheckboxChoiceField doesn't have a disabled prop. Adding one should be easy—just a matter of passing the prop to the underlying html checkbox.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was reluctant to disable checkboxes here as these lists can never not be loaded by the time we open the dialog - the membership lists will have been fetched to fill the resource card action buttons; these hooks just select the value for the resource. I added for good measure as we could in theory use the dialog from some other entrypoint in the future, plus the checkboxes do need to be disableable (is that a word?) generally for shared use.

Copy link
Contributor

@ChristopherChudzicki ChristopherChudzicki Nov 25, 2024

Choose a reason for hiding this comment

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

as these lists can never not be loaded by the time

In principle they can be—if the membership endpoint response is slower than others for some reason. And there's no disadvantage (as far as I can tell) to disabling them while the data is loading.

plus the checkboxes do need to be disableable

👍 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

🎉🎉🎉🎉🎉🎉🎉🎉 x 100

Copy link
Contributor

@ChristopherChudzicki ChristopherChudzicki left a comment

Choose a reason for hiding this comment

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

👍 Looks good!

@jonkafton jonkafton merged commit 48cb5e0 into main Nov 25, 2024
11 checks passed
@jonkafton jonkafton deleted the jk/6032-membership-lists branch November 25, 2024 14:31
@odlbot odlbot mentioned this pull request Nov 25, 2024
19 tasks
@ChristopherChudzicki ChristopherChudzicki restored the jk/6032-membership-lists branch November 26, 2024 17:23
mbertrand pushed a commit that referenced this pull request Dec 2, 2024
…learning paths (#1846)

* Retrieve user list and learning path memberships from new endpoints. Replace resource cache invalidation logic for membership keys

* Remove commented dead code

* Update Learning path hook and AddToListDialog tests

* Null list memeberships from results

* Pass in list booleans to the resource drawer

* Remove redundant mock patch body

* Fetch memeberships only when authenticated. Update tests.

* Mock responses for tests

* Clear list memberships from items list endpoints

* Replace wait in test

* Checkbox disabled state

* Disable checkboxes if memberships not loaded
@rhysyngsun rhysyngsun deleted the jk/6032-membership-lists branch February 7, 2025 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Review An open Pull Request that is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants