Skip to content

Commit

Permalink
updateUser in userHelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
borisyankov committed Mar 15, 2019
1 parent 28efc83 commit 2e8d601
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
88 changes: 88 additions & 0 deletions src/users/__tests__/userHelpers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
filterUserMatchesEmail,
getUniqueUsers,
groupUsersByStatus,
updateUser,
} from '../userHelpers';

describe('filterUserList', () => {
Expand Down Expand Up @@ -356,3 +357,90 @@ describe('getUniqueUsers', () => {
expect(getUniqueUsers(users)).toEqual(expectedUsers);
});
});

describe('updateUser', () => {
test('updates full name if that is the form of the payload', () => {
const initialValue = {
user_id: 1,
full_name: 'Some Guy',
};
const person = {
user_id: 1,
full_name: 'New Name',
};
const expectedValue = {
user_id: 1,
full_name: 'New Name',
};

const updatedValue = updateUser(initialValue, person);

expect(updatedValue).toEqual(expectedValue);
});

test('updating avatar using only the `avatar_url` value', () => {
const initialValue = {
user_id: 1,
full_name: 'Some Guy',
avatar_url: 'https://example.com/avatar.png',
};
const person = {
user_id: 1,
avatar_source: 'G',
avatar_url: 'https://example.com/new-avatar.png',
avatar_url_medium: 'https://example.com/new-avatar-medium.png',
};
const expectedValue = {
user_id: 1,
full_name: 'Some Guy',
avatar_url: 'https://example.com/new-avatar.png',
};

const updatedValue = updateUser(initialValue, person);

expect(updatedValue).toEqual(expectedValue);
});

test('updating custom profile field', () => {
const initialValue = {
user_id: 1,
full_name: 'Some Guy',
profile_data: {
1: {
value: 'CA',
rendered_value: null,
},
2: {
value: 'Blue',
rendered_value: null,
},
},
};
const person = {
user_id: 1,
custom_profile_field: {
id: 2,
value: 'Red',
rendered_value: null,
},
};
const expectedValue = {
user_id: 1,
full_name: 'Some Guy',
profile_data: {
1: {
value: 'CA',
rendered_value: null,
},
2: {
value: 'Red',
rendered_value: null,
},
},
};

const updatedValue = updateUser(initialValue, person);

expect(updatedValue).toEqual(expectedValue);
});
});
33 changes: 32 additions & 1 deletion src/users/userHelpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow strict-local */
import uniqby from 'lodash.uniqby';

import type { UserPresence, User, UserGroup, PresenceState } from '../types';
import type { UserPresence, User, UserGroup, UserUpdatePayload, PresenceState } from '../types';
import { NULL_USER } from '../nullObjects';
import { statusFromPresence } from '../utils/presence';

Expand Down Expand Up @@ -124,3 +124,34 @@ export const getAutocompleteUserGroupSuggestions = (
userGroup.name.toLowerCase().includes(filter.toLowerCase())
|| userGroup.description.toLowerCase().includes(filter.toLowerCase()),
);

export const updateUser = (oldValue: User, updateData: UserUpdatePayload): User => {
if (updateData.full_name !== undefined) {
// update full name
return {
...oldValue,
full_name: updateData.full_name,
};
} else if (updateData.avatar_url !== undefined) {
// update avatar
return {
...oldValue,
avatar_url: updateData.avatar_url,
};
} else if (updateData.custom_profile_field) {
// update a profile field
return {
...oldValue,
profile_data: {
...oldValue.profile_data,
[updateData.custom_profile_field.id]: {
value: updateData.custom_profile_field.value,
rendered_value: updateData.custom_profile_field.rendered_value,
},
},
};
}

// unsupported/unrecognized update, do nothing
return oldValue;
};

0 comments on commit 2e8d601

Please sign in to comment.