Skip to content

Commit

Permalink
realm_user event: Fix custom_profile_field.value to be string | null
Browse files Browse the repository at this point in the history
See zulip/zulip#22103, which I sent after
observing that this was sometimes null.
  • Loading branch information
chrisbobbe committed May 23, 2022
1 parent 32da02a commit 5dc4770
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/api/eventTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ type PersonCommon =
| SubsetProperties<UserOrBot, {| +user_id: mixed, +delivery_email: mixed |}>
| {|
+user_id: $PropertyType<UserOrBot, 'user_id'>,
+custom_profile_field: {| +id: number, +value: string, +rendered_value?: string |},
+custom_profile_field: {| +id: number, +value: string | null, +rendered_value?: string |},
|}
| {| +user_id: $PropertyType<UserOrBot, 'user_id'>, +new_email: string |};

Expand Down
4 changes: 4 additions & 0 deletions src/users/__tests__/usersReducer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ describe('usersReducer', () => {
);
});

test('When the user clears one of their custom profile fields.', () => {
check({ custom_profile_field: { id: 4, value: null } }, { ...theUser, profile_data: {} });
});

test('When the Zulip display email address of a user changes', () => {
const new_email = randString();
check({ new_email }, { ...theUser, email: new_email });
Expand Down
30 changes: 23 additions & 7 deletions src/users/usersReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,29 @@ export default (
if (person.custom_profile_field) {
return {
...user,
profile_data: {
...(user.profile_data: $PropertyType<User, 'profile_data'>),
[person.custom_profile_field.id]: {
value: person.custom_profile_field.value,
rendered_value: person.custom_profile_field.rendered_value,
},
},
profile_data: (() => {
if (person.custom_profile_field.value !== null) {
return {
...(user.profile_data: $PropertyType<User, 'profile_data'>),
[person.custom_profile_field.id]: ({
value: person.custom_profile_field.value,
rendered_value: person.custom_profile_field.rendered_value,

// FlowIssue: This assertion is cumbersome. But
// it fills a gap in Flow's coverage…that
// apparently Flow doesn't announce by marking
// anything with `any`. Remove when doing so
// doesn't stop Flow from catching something
// wrong on `value` or `rendered_value`.
}: $Values<$NonMaybeType<$PropertyType<User, 'profile_data'>>>),
};
} else {
// eslint-disable-next-line no-unused-vars
const { [person.custom_profile_field.id.toString()]: _, ...rest } =
user.profile_data ?? {};
return rest;
}
})(),
};
} else if (person.new_email !== undefined) {
return { ...user, email: person.new_email };
Expand Down

0 comments on commit 5dc4770

Please sign in to comment.