@orfeas126/box-ui-elements
Version:
Box UI Elements
135 lines (127 loc) • 5.81 kB
JavaScript
import * as React from 'react';
import noop from 'lodash/noop';
import API from '../../../api';
import { ACCESS_NONE, TYPE_FILE, TYPE_FOLDER } from '../../../constants';
import { CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS } from '../constants';
/**
* Generate CRUD functions for shared links.
*
* @param {API} api
* @param {string} itemID
* @param {ItemType} itemType
* @param {BoxItemPermission} permissions
* @param {string} accessLevel
* @param {ContentSharingHooksOptions} [options]
*/
function useSharedLink(api, itemID, itemType, permissions, accessLevel, options = {}) {
const [onAddLink, setOnAddLink] = React.useState(null);
const [onRemoveLink, setOnRemoveLink] = React.useState(null);
const [changeSharedLinkAccessLevel, setChangeSharedLinkAccessLevel] = React.useState(null);
const [changeSharedLinkPermissionLevel, setChangeSharedLinkPermissionLevel] = React.useState(null);
const [onSubmitSettings, setOnSubmitSettings] = React.useState(null);
const [generatedFunctions, setGeneratedFunctions] = React.useState(false);
/**
* Storing the access level in a ref allows us to update settings, which depend on the access level, in the following potential scenarios:
* - After changing the shared link's access level
* - After removing and recreating the shared link
*/
const currentAccessLevel = React.useRef(accessLevel);
const {
handleRemoveSharedLinkError = noop,
handleRemoveSharedLinkSuccess = arg => arg,
handleUpdateSharedLinkError = noop,
handleUpdateSharedLinkSuccess = arg => arg,
setIsLoading = noop,
transformAccess = arg => arg,
transformPermissions = arg => arg,
transformSettings = (data, access) => data // eslint-disable-line no-unused-vars
} = options;
React.useEffect(() => {
if (!permissions || generatedFunctions) return;
const itemData = {
id: itemID,
permissions
};
let itemAPIInstance;
if (itemType === TYPE_FILE) {
itemAPIInstance = api.getFileAPI();
} else if (itemType === TYPE_FOLDER) {
itemAPIInstance = api.getFolderAPI();
}
// Create functions that alter the access level of a shared link
const connectToItemShare = ({
access,
requestOptions = CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS,
successFn = handleUpdateSharedLinkSuccess,
errorFn = handleUpdateSharedLinkError
}) => {
setIsLoading(true);
return itemAPIInstance.share(itemData, access, successFn, errorFn, requestOptions);
};
/**
* Set the shared link creation function.
*
* The backend will determine the default access level for the shared link, so we should not pass a value for "access."
* The "open" and "company" access levels may be disabled due to certain policies, and attempting to set a disabled
* access level will throw a 400. The only access level that we can reliably set is "collaborators," but defaulting
* to that level diverges from existing shared link creation behavior in the WebApp.
*
* After a shared link is successfully created, we save the access level from the API response into our ref.
*/
const updatedOnAddLinkFn = () => () => connectToItemShare({
successFn: data => {
const {
shared_link: {
access
}
} = data;
currentAccessLevel.current = access;
handleUpdateSharedLinkSuccess(data);
}
});
setOnAddLink(updatedOnAddLinkFn);
// Shared link removal function
const updatedOnRemoveLinkFn = () => () => connectToItemShare({
access: ACCESS_NONE,
successFn: handleRemoveSharedLinkSuccess,
errorFn: handleRemoveSharedLinkError
});
setOnRemoveLink(updatedOnRemoveLinkFn);
// Shared link access level change function
const updatedChangeSharedLinkAccessLevelFn = () => newAccessLevel => connectToItemShare({
// $FlowFixMe
access: transformAccess(newAccessLevel),
successFn: data => {
currentAccessLevel.current = newAccessLevel;
handleUpdateSharedLinkSuccess(data);
}
});
setChangeSharedLinkAccessLevel(updatedChangeSharedLinkAccessLevelFn);
// Create functions that update shared link settings aside from the access level
const connectToUpdateSharedLink = newSharedLinkData => {
setIsLoading(true);
return itemAPIInstance.updateSharedLink(itemData, newSharedLinkData, handleUpdateSharedLinkSuccess, handleUpdateSharedLinkError, CONTENT_SHARING_SHARED_LINK_UPDATE_PARAMS);
};
// Shared link permission level change function
const updatedChangeSharedLinkPermissionLevelFn = () => newSharedLinkPermissionLevel => connectToUpdateSharedLink({
permissions: transformPermissions(newSharedLinkPermissionLevel)
});
setChangeSharedLinkPermissionLevel(updatedChangeSharedLinkPermissionLevelFn);
/**
* Set the shared link settings update function. This is currently used in the Shared Link Settings Modal,
* but it may also be used to update any settings not covered by the above functions.
*/
const updatedOnSubmitSettingsFn = () => newSettings => connectToUpdateSharedLink(transformSettings(newSettings, currentAccessLevel.current));
setOnSubmitSettings(updatedOnSubmitSettingsFn);
setGeneratedFunctions(true);
}, [permissions, generatedFunctions, itemID, itemType, handleUpdateSharedLinkSuccess, handleRemoveSharedLinkSuccess, transformAccess, accessLevel, transformPermissions, transformSettings, currentAccessLevel, api, setIsLoading, handleRemoveSharedLinkError, handleUpdateSharedLinkError]);
return {
changeSharedLinkAccessLevel,
changeSharedLinkPermissionLevel,
onAddLink,
onRemoveLink,
onSubmitSettings
};
}
export default useSharedLink;
//# sourceMappingURL=useSharedLink.js.map