Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/elements/content-sharing/ContentSharingV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Internationalize from '../common/Internationalize';
import Providers from '../common/Providers';
import { withBlueprintModernization } from '../common/withBlueprintModernization';
import { fetchAvatars, fetchCollaborators, fetchCurrentUser, fetchItem } from './apis';
import { useSharingService } from './hooks/useSharingService';
import { useContactService, useSharingService } from './hooks';
import { convertCollabsResponse, convertItemResponse } from './utils';

import type { Collaborations, ItemType, StringMap } from '../../common/types/core';
Expand Down Expand Up @@ -61,6 +61,7 @@ function ContentSharingV2({
setItem,
setSharedLink,
});
const { contactService } = useContactService(api, itemID, currentUser?.id);

// Handle successful GET requests to /files or /folders
const handleGetItemSuccess = React.useCallback(itemData => {
Expand Down Expand Up @@ -165,6 +166,7 @@ function ContentSharingV2({
config={config}
collaborationRoles={collaborationRoles}
collaborators={collaborators}
contactService={contactService}
currentUser={currentUser}
item={item}
sharedLink={sharedLink}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function FakeComponent({
const [getContacts, setGetContacts] = React.useState(null);

const updatedGetContactsFn = useContacts(api, MOCK_ITEM_ID, {
currentUserId: '123',
handleSuccess,
handleError,
transformGroups,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { renderHook } from '@testing-library/react';

import { convertGroupContactsResponse, convertUserContactsResponse } from '../../utils';
import { useContactService } from '../useContactService';
import useContacts from '../useContacts';

jest.mock('../useContacts');
jest.mock('../../utils');

const mockApi = {
getMarkerBasedUsersAPI: jest.fn(),
getMarkerBasedGroupsAPI: jest.fn(),
};
const mockItemID = '123456789';
const mockCurrentUserID = '123';
const mockGetContacts = jest.fn();

describe('elements/content-sharing/hooks/useContactService', () => {
beforeEach(() => {
(useContacts as jest.Mock).mockReturnValue(mockGetContacts);
(convertGroupContactsResponse as jest.Mock).mockReturnValue([]);
(convertUserContactsResponse as jest.Mock).mockReturnValue([]);
});

afterEach(() => {
jest.clearAllMocks();
});

test('should return contactService with getContacts function', () => {
const { result } = renderHook(() => useContactService(mockApi, mockItemID, mockCurrentUserID));

expect(useContacts).toHaveBeenCalledWith(mockApi, mockItemID, {
currentUserId: mockCurrentUserID,
isContentSharingV2Enabled: true,
transformUsers: expect.any(Function),
transformGroups: expect.any(Function),
});
expect(result.current.contactService).toEqual({
getContacts: mockGetContacts,
});
});

test('should pass transform functions that call correct conversion functions with params', () => {
const mockTransformedUsers = [{ id: 'user1', email: 'user1@test.com' }];
const mockTransformedGroups = [{ id: 'group1', name: 'Test Group' }];
const mockUserData = { entries: mockTransformedUsers };
const mockGroupData = { entries: mockTransformedGroups };

(convertUserContactsResponse as jest.Mock).mockReturnValue(mockTransformedUsers);
(convertGroupContactsResponse as jest.Mock).mockReturnValue(mockTransformedGroups);

renderHook(() => useContactService(mockApi, mockItemID, mockCurrentUserID));

// Get the transform functions that were passed to useContacts
const transformUsersFn = useContacts.mock.calls[0][2].transformUsers;
const transformGroupsFn = useContacts.mock.calls[0][2].transformGroups;
const resultUsers = transformUsersFn(mockUserData);
const resultGroups = transformGroupsFn(mockGroupData);

expect(convertUserContactsResponse as jest.Mock).toHaveBeenCalledWith(mockUserData, mockCurrentUserID);
expect(convertGroupContactsResponse as jest.Mock).toHaveBeenCalledWith(mockGroupData);
expect(resultUsers).toBe(mockTransformedUsers);
expect(resultGroups).toBe(mockTransformedGroups);
});
});
2 changes: 2 additions & 0 deletions src/elements/content-sharing/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { useContactService } from './useContactService';
export { useSharingService } from './useSharingService';
13 changes: 13 additions & 0 deletions src/elements/content-sharing/hooks/useContactService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { convertGroupContactsResponse, convertUserContactsResponse } from '../utils';
import useContacts from './useContacts';

export const useContactService = (api, itemId, currentUserId) => {
const getContacts = useContacts(api, itemId, {
currentUserId,
isContentSharingV2Enabled: true,
transformUsers: data => convertUserContactsResponse(data, currentUserId),
transformGroups: data => convertGroupContactsResponse(data),
});

return { contactService: { getContacts } };
};
25 changes: 22 additions & 3 deletions src/elements/content-sharing/hooks/useContacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ import type { ContentSharingHooksOptions, GetContactsFnType } from '../types';
*/
function useContacts(api: API, itemID: string, options: ContentSharingHooksOptions): GetContactsFnType | null {
const [getContacts, setGetContacts] = React.useState<null | GetContactsFnType>(null);
const { handleSuccess = noop, handleError = noop, transformGroups, transformUsers } = options;
const {
currentUserId,
handleSuccess = noop,
handleError = noop,
isContentSharingV2Enabled,
transformGroups,
transformUsers,
} = options;

React.useEffect(() => {
if (getContacts) return;
if (getContacts || (isContentSharingV2Enabled && !currentUserId)) {
return;
}

const resolveAPICall = (
resolve: (result: Array<Object>) => void,
Expand Down Expand Up @@ -60,7 +69,17 @@ function useContacts(api: API, itemID: string, options: ContentSharingHooksOptio
return Promise.all([getUsers, getGroups]).then(contactArrays => [...contactArrays[0], ...contactArrays[1]]);
};
setGetContacts(updatedGetContactsFn);
}, [api, getContacts, handleError, handleSuccess, itemID, transformGroups, transformUsers]);
}, [
api,
currentUserId,
getContacts,
handleError,
handleSuccess,
isContentSharingV2Enabled,
itemID,
transformGroups,
transformUsers,
]);

return getContacts;
}
Expand Down
2 changes: 2 additions & 0 deletions src/elements/content-sharing/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ export type ContentSharingItemAPIResponse = {
};

export type ContentSharingHooksOptions = {
currentUserId?: string,
handleError?: Function,
handleRemoveSharedLinkError?: Function,
handleRemoveSharedLinkSuccess?: Function,
handleSuccess?: Function,
handleUpdateSharedLinkError?: Function,
handleUpdateSharedLinkSuccess?: Function,
isContentSharingV2Enabled?: boolean,
setIsLoading?: Function,
transformAccess?: Function,
transformGroups?: Function,
Expand Down
Loading