|
1 | 1 | import * as React from 'react'; |
| 2 | +import isEmpty from 'lodash/isEmpty'; |
2 | 3 |
|
3 | 4 | import { UnifiedShareModal } from '@box/unified-share-modal'; |
| 5 | +import type { CollaborationRole, Item, SharedLink, User } from '@box/unified-share-modal'; |
4 | 6 |
|
| 7 | +import API from '../../api'; |
| 8 | +import { FIELD_ENTERPRISE, FIELD_HOSTNAME, TYPE_FILE, TYPE_FOLDER } from '../../constants'; |
5 | 9 | import Internationalize from '../common/Internationalize'; |
6 | 10 | import Providers from '../common/Providers'; |
| 11 | +import { CONTENT_SHARING_ITEM_FIELDS } from './constants'; |
| 12 | +import { convertItemResponse } from './utils'; |
7 | 13 |
|
8 | 14 | import type { ItemType, StringMap } from '../../common/types/core'; |
9 | 15 |
|
10 | 16 | export interface ContentSharingV2Props { |
| 17 | + /** api - API instance */ |
| 18 | + api: API; |
11 | 19 | /** children - Children for the element to open the Unified Share Modal */ |
12 | 20 | children?: React.ReactElement; |
13 | 21 | /** itemID - Box file or folder ID */ |
14 | 22 | itemID: string; |
15 | 23 | /** itemType - "file" or "folder" */ |
16 | 24 | itemType: ItemType; |
17 | 25 | /** hasProviders - Whether the element has providers for USM already */ |
18 | | - hasProviders: boolean; |
| 26 | + hasProviders?: boolean; |
19 | 27 | /** language - Language used for the element */ |
20 | 28 | language?: string; |
21 | 29 | /** messages - Localized strings used by the element */ |
22 | 30 | messages?: StringMap; |
23 | 31 | } |
24 | 32 |
|
25 | | -function ContentSharingV2({ children, itemID, itemType, hasProviders, language, messages }: ContentSharingV2Props) { |
26 | | - // Retrieve item from API later |
27 | | - const mockItem = { |
28 | | - id: itemID, |
29 | | - name: 'Box Development Guide.pdf', |
30 | | - type: itemType, |
| 33 | +function ContentSharingV2({ |
| 34 | + api, |
| 35 | + children, |
| 36 | + itemID, |
| 37 | + itemType, |
| 38 | + hasProviders, |
| 39 | + language, |
| 40 | + messages, |
| 41 | +}: ContentSharingV2Props) { |
| 42 | + const [item, setItem] = React.useState<Item | null>(null); |
| 43 | + const [sharedLink, setSharedLink] = React.useState<SharedLink | null>(null); |
| 44 | + const [currentUser, setCurrentUser] = React.useState<User | null>(null); |
| 45 | + const [collaborationRoles, setCollaborationRoles] = React.useState<CollaborationRole[] | null>(null); |
| 46 | + |
| 47 | + // Handle successful GET requests to /files or /folders |
| 48 | + const handleGetItemSuccess = React.useCallback(itemData => { |
| 49 | + const { |
| 50 | + collaborationRoles: collaborationRolesFromAPI, |
| 51 | + item: itemFromAPI, |
| 52 | + sharedLink: sharedLinkFromAPI, |
| 53 | + } = convertItemResponse(itemData); |
| 54 | + setItem(itemFromAPI); |
| 55 | + setSharedLink(sharedLinkFromAPI); |
| 56 | + setCollaborationRoles(collaborationRolesFromAPI); |
| 57 | + }, []); |
| 58 | + |
| 59 | + // Reset state if the API has changed |
| 60 | + React.useEffect(() => { |
| 61 | + setItem(null); |
| 62 | + setSharedLink(null); |
| 63 | + setCurrentUser(null); |
| 64 | + setCollaborationRoles(null); |
| 65 | + }, [api]); |
| 66 | + |
| 67 | + // Get initial data for the item |
| 68 | + React.useEffect(() => { |
| 69 | + const getItem = () => { |
| 70 | + if (itemType === TYPE_FILE) { |
| 71 | + api.getFileAPI().getFile( |
| 72 | + itemID, |
| 73 | + handleGetItemSuccess, |
| 74 | + {}, |
| 75 | + { |
| 76 | + fields: CONTENT_SHARING_ITEM_FIELDS, |
| 77 | + }, |
| 78 | + ); |
| 79 | + } else if (itemType === TYPE_FOLDER) { |
| 80 | + api.getFolderAPI().getFolderFields( |
| 81 | + itemID, |
| 82 | + handleGetItemSuccess, |
| 83 | + {}, |
| 84 | + { |
| 85 | + fields: CONTENT_SHARING_ITEM_FIELDS, |
| 86 | + }, |
| 87 | + ); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + if (api && !isEmpty(api) && !item && !sharedLink) { |
| 92 | + getItem(); |
| 93 | + } |
| 94 | + }, [api, item, itemID, itemType, sharedLink, handleGetItemSuccess]); |
| 95 | + |
| 96 | + // Get initial data for the user |
| 97 | + React.useEffect(() => { |
| 98 | + const getUserSuccess = userData => { |
| 99 | + const { enterprise, id } = userData; |
| 100 | + setCurrentUser({ |
| 101 | + id, |
| 102 | + enterprise: { |
| 103 | + name: enterprise ? enterprise.name : '', |
| 104 | + }, |
| 105 | + }); |
| 106 | + }; |
| 107 | + |
| 108 | + const getUserData = () => { |
| 109 | + api.getUsersAPI(false).getUser( |
| 110 | + itemID, |
| 111 | + getUserSuccess, |
| 112 | + {}, |
| 113 | + { |
| 114 | + params: { |
| 115 | + fields: [FIELD_ENTERPRISE, FIELD_HOSTNAME].toString(), |
| 116 | + }, |
| 117 | + }, |
| 118 | + ); |
| 119 | + }; |
| 120 | + |
| 121 | + if (api && !isEmpty(api) && item && sharedLink && !currentUser) { |
| 122 | + getUserData(); |
| 123 | + } |
| 124 | + }, [api, currentUser, item, itemID, itemType, sharedLink]); |
| 125 | + |
| 126 | + const config = { |
| 127 | + sharedLinkEmail: false, |
31 | 128 | }; |
32 | 129 |
|
33 | 130 | return ( |
34 | 131 | <Internationalize language={language} messages={messages}> |
35 | 132 | <Providers hasProviders={hasProviders}> |
36 | | - <UnifiedShareModal item={mockItem}>{children}</UnifiedShareModal> |
| 133 | + {item && ( |
| 134 | + <UnifiedShareModal |
| 135 | + config={config} |
| 136 | + collaborationRoles={collaborationRoles} |
| 137 | + currentUser={currentUser} |
| 138 | + item={item} |
| 139 | + sharedLink={sharedLink} |
| 140 | + > |
| 141 | + {children} |
| 142 | + </UnifiedShareModal> |
| 143 | + )} |
37 | 144 | </Providers> |
38 | 145 | </Internationalize> |
39 | 146 | ); |
|
0 commit comments