Skip to content

Commit

Permalink
Move registrationMode-related logic from updateImageUserAvatar to `…
Browse files Browse the repository at this point in the history
…nativeUpdateUserImageAvatar`

Summary:
Effectively D8343, but for `updateImageUserAvatar` instead of `setUserAvatar`.

The registrationMode-related code is `native-specific` and was pretty interwoven with the logic in `setUserAvatar`/`updateImageUserAvatar`.

This diff decouples `native`-specific from `updateImageUserAvatar` and moves it to `nativeUpdateImageUserAvatar`.

As of this diff, `updateImageUserAvatar` is NOT yet platform-agnostic. We still need to pull out `uploadSelectedMedia` and want the function to take `ImageAvatarDBContent` instead of the `native`-specific `NativeMediaSelection`. The next diff will address this.

---

Depends on D8344

Test Plan: Continue to be able to set image avatars on `native` as expected.

Reviewers: ashoat, ginsu, rohan

Reviewed By: ashoat, rohan

Subscribers: tomek

Differential Revision: https://phab.comm.dev/D8345
  • Loading branch information
atulsmadhugiri committed Jul 28, 2023
1 parent db1e952 commit f78ae44
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
8 changes: 0 additions & 8 deletions lib/components/base-edit-user-avatar-provider.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@ function BaseEditUserAvatarProvider(props: Props): React.Node {

const updateImageUserAvatar = React.useCallback(
async (selection: NativeMediaSelection) => {
if (registrationModeRef.current.registrationMode === 'on') {
registrationModeRef.current.successCallback({
needsUpload: true,
mediaSelection: selection,
});
return;
}

const imageAvatarUpdateRequest = await uploadSelectedMedia(selection);
if (!imageAvatarUpdateRequest) {
return;
Expand Down
26 changes: 24 additions & 2 deletions native/avatars/avatar-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,39 @@ function useNativeUpdateUserImageAvatar(): (
) => Promise<void> {
const editUserAvatarContext = React.useContext(EditUserAvatarContext);
invariant(editUserAvatarContext, 'editUserAvatarContext must be defined');
const { updateImageUserAvatar } = editUserAvatarContext;
const {
updateImageUserAvatar,
getRegistrationModeEnabled,
getRegistrationModeSuccessCallback,
} = editUserAvatarContext;

const nativeUpdateUserImageAvatar = React.useCallback(
async (selection: NativeMediaSelection) => {
const registrationModeEnabled = getRegistrationModeEnabled();
if (registrationModeEnabled) {
const successCallback = getRegistrationModeSuccessCallback();
invariant(
successCallback,
'successCallback must be defined if registrationModeEnabled is true',
);
successCallback({
needsUpload: true,
mediaSelection: selection,
});
return;
}

try {
await updateImageUserAvatar(selection);
} catch {
displayAvatarUpdateFailureAlert();
}
},
[updateImageUserAvatar],
[
getRegistrationModeEnabled,
getRegistrationModeSuccessCallback,
updateImageUserAvatar,
],
);

return nativeUpdateUserImageAvatar;
Expand Down

0 comments on commit f78ae44

Please sign in to comment.