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

Refactor user feedback logic #2534

Merged
merged 17 commits into from
Aug 11, 2023
Merged

Refactor user feedback logic #2534

merged 17 commits into from
Aug 11, 2023

Conversation

MonkeyDo
Copy link
Member

@MonkeyDo MonkeyDo commented Aug 7, 2023

When we first implemented feedback on Listen Cards, we only did so on one page.
As the ListenCard component was refactored from many different components into a single all-powerful ListenCard, the feedback logic has been copied over to every top-level page.
This meant each top-level page had logic for:

  • fetching feedback from the API
  • storing the feedback in the component state
  • accessing the feedback in the state for a specific recording
  • updating/submitting feedback for a recording

I've finally had enough of duplicating the code, and refactored it all into a custom react hook which is only used in one single component (ListenFeedbackComponent).
Each ListenCard, when instantiated, now manages its own feedback automatically using the recording MBID or MSID in its listen.

One of the reason this wasn't done before is that I wanted to avoid pelting our API with 50 separate requests for each page of listens.
Now the useFeedbackMap hook handles fetching feedback in batches (throttled) as well as saving the feedback in a singleton Map object to avoid more API calls.

Note: I completely removed ListenFeedbackComponent.test.tsx test file. I am hitting some issues with Enzyme again which doesn't handle React functional components. I'm working on the tests but they'll probably come in a separate PR as we might need to start running two separate test suites in parallel…

Allows us to fetch and get recording MBIDs simply, with implementation details in the hook itself, and prevents hammering our API with a million calls by batching calls (throttling)
for playing_now listens, unmatched listens and others that might not have an MBID in the listen metadata
This is the feedback fetching and management logic that was duplicated for each entry page.
ES6 module interop blablablabla
… from parent components that were previously handling all the feedback logic.
Rewriting the tests currently, but it's basically rewriting from scratch and with extra issues due to going from a class component to a functional component (issues with the Enzyme library)
@MonkeyDo MonkeyDo requested a review from amCap1712 August 7, 2023 16:22
Copy link
Member

@mayhem mayhem 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 -- I love how much code is removed!

React hooks should be pure, and this one wasn't.
Instead, create a RecordingFeedbackManager class and store an instance in the global context

Also adds a subscription system to update other ListenCard for the same MBID/MSID so that changes to one listencard will sync with other listencard for the same recording
Used in pretty much all our test files… Something else to refactor !
@MonkeyDo
Copy link
Member Author

MonkeyDo commented Aug 8, 2023

Tests are not passing. Something the enzyme library didn't like. Will investigate.

Copy link
Member

@amCap1712 amCap1712 left a comment

Choose a reason for hiding this comment

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

Looks awesome, thanks!!

instead of handling JSX and the toast library in the RecordingFeedbackManager which shouldn't need to know about any of those details (on top of creating issues with Enzyme tests…)
Enzyme really doesn't like functional components, and the whole test suite was breaking because it doesn't support it.
Mocked the hook instead.
Not sure why the tests are failing in CI but passing locally…
@MonkeyDo MonkeyDo merged commit f20424c into master Aug 11, 2023
3 checks passed
@MonkeyDo MonkeyDo deleted the refactor-user-feedback branch August 11, 2023 10:34
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