-
Notifications
You must be signed in to change notification settings - Fork 11.7k
fix: SavePreferences being called multiple times and causing issues #35904
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
base: develop
Are you sure you want to change the base?
Conversation
Looks like this PR is not ready to merge, because of the following issues:
Please fix the issues and try again If you have any trouble, please check the PR guidelines |
🦋 Changeset detectedLatest commit: 5910dc5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 36 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Code Review Completed! 🔥The code review was successfully completed based on your current configurations. Kody Guide: Usage and ConfigurationInteracting with Kody
Current Kody ConfigurationReview OptionsThe following review options are enabled or disabled:
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #35904 +/- ##
===========================================
- Coverage 61.26% 61.26% -0.01%
===========================================
Files 3164 3164
Lines 74757 74757
Branches 16689 16689
===========================================
- Hits 45801 45800 -1
- Misses 25850 25852 +2
+ Partials 3106 3105 -1
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
|
Kody Review CompleteGreat news! 🎉 Keep up the excellent work! 🚀 Kody Guide: Usage and ConfigurationInteracting with Kody
Current Kody ConfigurationReview OptionsThe following review options are enabled or disabled:
|
Kody Review CompleteGreat news! 🎉 Keep up the excellent work! 🚀 Kody Guide: Usage and ConfigurationInteracting with Kody
Current Kody ConfigurationReview OptionsThe following review options are enabled or disabled:
|
Code Review Completed! 🔥The code review was successfully completed based on your current configurations. Kody Guide: Usage and ConfigurationInteracting with Kody
Current Kody ConfigurationReview OptionsThe following review options are enabled or disabled:
|
const setPreferencesEndpoint = useEndpoint('POST', '/v1/users.setPreferences'); | ||
const setPreferencesAction = useMutation({ | ||
mutationFn: setPreferencesEndpoint, | ||
onSuccess: () => { | ||
dispatchToastMessage({ type: 'success', message: t('Preferences_saved') }); | ||
}, | ||
onError: (error) => { | ||
dispatchToastMessage({ type: 'error', message: error }); | ||
}, | ||
onSettled: () => reset(currentData), | ||
}); | ||
|
||
const handleSaveData = async (formData: AccountPreferencesData) => { | ||
const { highlights, dontAskAgainList, ...data } = getDirtyFields(formData, dirtyFields); | ||
if (highlights || highlights === '') { | ||
Object.assign(data, { | ||
highlights: | ||
typeof highlights === 'string' && | ||
highlights | ||
.split(/,|\n/) | ||
.map((val) => val.trim()) | ||
.filter(Boolean), | ||
}); | ||
} | ||
|
||
if (dontAskAgainList) { | ||
const list = | ||
Array.isArray(dontAskAgainList) && dontAskAgainList.length > 0 | ||
? dontAskAgainList.map(([action, label]) => ({ action, label })) | ||
: []; | ||
Object.assign(data, { dontAskAgainList: list }); | ||
} | ||
|
||
setPreferencesAction.mutateAsync({ data }); | ||
}; | ||
const handleSaveData = useSavePreferences({ dirtyFields, currentData, reset }); |
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.
What changed in the logic here besides moving to a hook?
currentData: AccountPreferencesData; | ||
}; | ||
|
||
export const useSavePreferences = ({ dirtyFields, currentData, reset }: useSavePreferencesProps) => { |
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 would advise against refactoring the logic into a separate hook in a fix, unless it would be used in another component. Makes it hard to understand what the root cause and fix was.
describe('useSavePreferences', () => { | ||
it('should call setPreferencesEndpoint with correct data', async () => { | ||
const dirtyFields = { language: true }; | ||
const { result } = renderHook(() => useSavePreferences({ dirtyFields, reset: jest.fn(), currentData: {} }), { | ||
wrapper: mockAppRoot().withEndpoint('POST', '/v1/users.setPreferences', mockSetPreferencesEndpoint).build(), | ||
}); | ||
|
||
const formData: AccountPreferencesData = { language: 'en' }; | ||
await waitFor(() => result.current(formData)); | ||
|
||
expect(mockSetPreferencesEndpoint).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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 think this should be an e2e test instead. I wasn't able to make this test fail, even by copying the old logic into the new hook.
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 a refactor, the fix probably won't need this amount of code. I'd suggest separating it into a brand new PR in order to have a isolated fix
The preferences are being saved multiple times when you press save, this can cause some issues, like when selecting a new language, the page tries to reset the forms while the promise is being processed, causing the language to be saved as the previous value
Issue(s)
CORE-1106
Steps to test or reproduce
This issue happened more consistently with the
Portuguese
andBrazilan Portuguese
languagesFurther comments
This pull request addresses an issue where the
SavePreferences
function was being called multiple times, leading to potential problems. The changes include the introduction of a new React hook,useSavePreferences
, which is designed to handle the saving of user account preferences. This hook usesuseMutation
from@tanstack/react-query
to interact with the/v1/users.setPreferences
API endpoint, processes specific fields likehighlights
anddontAskAgainList
based on dirty fields, and provides user feedback through toast messages.Additionally, a new test suite for the
useSavePreferences
hook has been added using React Testing Library and Jest to ensure that the hook correctly triggers theusers.setPreferences
API endpoint when invoked. The preference saving logic in theAccountPreferencesPage
component has been refactored to use this new custom hook, simplifying the component and improving code organization by centralizing the saving mechanism.