-
Notifications
You must be signed in to change notification settings - Fork 346
Expand file tree
/
Copy pathuseSharingService.ts
More file actions
44 lines (34 loc) · 1.37 KB
/
useSharingService.ts
File metadata and controls
44 lines (34 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import * as React from 'react';
import { TYPE_FILE, TYPE_FOLDER } from '../../../constants';
import { convertItemResponse } from '../utils/convertItemResponse';
import { createSharingService } from '../sharingService';
export const useSharingService = (api, item, itemId, itemType, setItem, setSharedLink) => {
const itemApiInstance = React.useMemo(() => {
if (!item) {
return null;
}
if (itemType === TYPE_FILE) {
return api.getFileAPI();
}
if (itemType === TYPE_FOLDER) {
return api.getFolderAPI();
}
return null;
}, [api, item, itemType]);
const sharingService = React.useMemo(() => {
if (!itemApiInstance) {
return null;
}
const itemData = {
id: itemId,
permissions: item.permissions,
};
const handleSuccess = updatedItemData => {
const { item: updatedItem, sharedLink: updatedSharedLink } = convertItemResponse(updatedItemData);
setItem(prevItem => ({ ...prevItem, ...updatedItem }));
setSharedLink(prevSharedLink => ({ ...prevSharedLink, ...updatedSharedLink }));
};
return createSharingService({ itemApiInstance, itemData, onSuccess: handleSuccess });
}, [itemApiInstance, item, itemId, setItem, setSharedLink]);
return { sharingService };
};