Skip to content

Commit c5532e3

Browse files
committed
fix: types and improve code
1 parent 8f8aa55 commit c5532e3

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
lines changed

src/elements/content-sharing/__tests__/useContactsByEmail.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ describe('elements/content-sharing/hooks/useContactsByEmail', () => {
134134

135135
test('should set the value of getContactsByEmail() and retrieve contacts when isContentSharingV2Enabled is true and email is provided', async () => {
136136
const mockUser1 = MOCK_CONTACTS_API_RESPONSE.entries[0];
137-
const { id, email, name, type } = mockUser1;
137+
const { id, login: email, name, type } = mockUser1;
138138
const expectedTransformedResult = {
139139
id,
140140
email,

src/elements/content-sharing/hooks/useContactsByEmail.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import * as React from 'react';
44
import noop from 'lodash/noop';
55
import API from '../../../api';
66
import type { UserCollection, UserMini } from '../../../common/types/core';
7-
import type { ContactByEmailObject, ContentSharingHooksOptions, GetContactsByEmailFnType } from '../types';
7+
import type {
8+
ContactByEmailObject,
9+
ContentSharingHooksOptions,
10+
GetContactByEmailFnType,
11+
GetContactsByEmailFnType,
12+
} from '../types';
813

914
/**
1015
* Generate the getContactsByEmail() function, which is used for looking up contacts added to the collaborators field in the USM.
@@ -38,41 +43,43 @@ function useContactsByEmail(
3843
return resolve({});
3944
};
4045

41-
const updatedGetContactsByEmailFn: GetContactsByEmailFnType =
42-
() => (filterTerm: { [emails: string]: string }) => {
43-
if (!filterTerm || !Array.isArray(filterTerm.emails) || !filterTerm.emails.length) {
46+
if (isContentSharingV2Enabled) {
47+
const getContactsByEmailV2: GetContactByEmailFnType = () => email => {
48+
if (!email) {
4449
return Promise.resolve({});
4550
}
46-
const parsedFilterTerm = filterTerm.emails[0];
4751

48-
return new Promise((resolve: (result: ContactByEmailObject | Array<UserMini>) => void) => {
52+
return new Promise(resolve => {
4953
api.getMarkerBasedUsersAPI(false).getUsersInEnterprise(
5054
itemID,
51-
(response: UserCollection) => resolveAPICall(resolve, response, transformUsers),
55+
response => resolveAPICall(resolve, response, transformUsers),
5256
handleError,
53-
{ filter_term: parsedFilterTerm },
57+
{ filter_term: email },
5458
);
5559
});
5660
};
5761

58-
const getContactsByEmailV2 = () => email => {
59-
if (!email) {
60-
return Promise.resolve({});
61-
}
62+
setGetContactsByEmail(getContactsByEmailV2);
63+
} else {
64+
const updatedGetContactsByEmailFn: GetContactsByEmailFnType =
65+
() => (filterTerm: { [emails: string]: string }) => {
66+
if (!filterTerm || !Array.isArray(filterTerm.emails) || !filterTerm.emails.length) {
67+
return Promise.resolve({});
68+
}
69+
const parsedFilterTerm = filterTerm.emails[0];
6270

63-
return new Promise(resolve => {
64-
api.getMarkerBasedUsersAPI(false).getUsersInEnterprise(
65-
itemID,
66-
response => resolveAPICall(resolve, response, transformUsers),
67-
handleError,
68-
{ filter_term: email },
69-
);
70-
});
71-
};
71+
return new Promise((resolve: (result: ContactByEmailObject | Array<UserMini>) => void) => {
72+
api.getMarkerBasedUsersAPI(false).getUsersInEnterprise(
73+
itemID,
74+
(response: UserCollection) => resolveAPICall(resolve, response, transformUsers),
75+
handleError,
76+
{ filter_term: parsedFilterTerm },
77+
);
78+
});
79+
};
7280

73-
isContentSharingV2Enabled
74-
? setGetContactsByEmail(getContactsByEmailV2)
75-
: setGetContactsByEmail(updatedGetContactsByEmailFn);
81+
setGetContactsByEmail(updatedGetContactsByEmailFn);
82+
}
7683
}, [api, getContactsByEmail, handleError, handleSuccess, isContentSharingV2Enabled, itemID, transformUsers]);
7784

7885
return getContactsByEmail;

src/elements/content-sharing/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ export type GetContactsByEmailFnType = () => (filterTerm: {
140140
[emails: string]: string,
141141
}) => Promise<ContactByEmailObject | Array<UserMini>> | null;
142142

143+
export type GetContactByEmailFnType = () => (email: string) => Promise<ContactByEmailObject | Array<UserMini>> | null;
144+
143145
export type SendInvitesFnType = () => InviteCollaboratorsRequest => Promise<null | Array<Function>>;
144146

145147
export type ConnectToItemShareFnType = ({

src/elements/content-sharing/utils/convertContactServiceData.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ export const convertGroupContactsResponse = contactsApiData => {
5757
};
5858

5959
/**
60-
* Convert an enterprise users API response into an object of internal USM contacts, keyed by email, which is
61-
* then passed to the mergeContacts function.
60+
* Convert an enterprise users API response into a single internal USM contact object (from the first entry).
6261
*/
6362
export const convertUserContactByEmailResponse = contactsApiData => {
6463
const { entries = [] } = contactsApiData;

0 commit comments

Comments
 (0)