import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import Button from '../../components/button';
import PrimaryButton from '../../components/primary-button';
import { Modal, ModalActions } from '../../components/modal';
import InlineNotice from '../../components/inline-notice';
import Link from '../../components/link/LinkBase';
import commonMessages from '../../common/messages';
import Classification from '../classification';
import VanityNameSection from './VanityNameSection';
import PasswordSection from './PasswordSection';
import ExpirationSection from './ExpirationSection';
import AllowDownloadSection from './AllowDownloadSection';
import messages from './messages';
import { PEOPLE_WITH_LINK, PEOPLE_IN_COMPANY, PEOPLE_IN_ITEM } from '../shared-link-modal/constants';
import './SharedLinkSettingsModal.scss';
/**
* Return the translation message based on the access level and whether the user can download or not
* @param {string} accessLevel one of 'peopleWithTheLink', 'peopleInYourCompany', or 'peopleInThisItem'
* @param {boolean} canDownload prop value for whether the user can currently download
*
* @return {object|undefined} message for the proper translation (or undefined if nothing matches)
*/
function getAccessNoticeMessageId(accessLevel, canDownload) {
let message;
switch (accessLevel) {
case PEOPLE_WITH_LINK:
message = canDownload ? messages.withLinkViewDownload : messages.withLinkView;
break;
case PEOPLE_IN_COMPANY:
message = canDownload ? messages.inCompanyViewDownload : messages.inCompanyView;
break;
case PEOPLE_IN_ITEM:
message = messages.inItem;
break;
default:
break;
}
return message;
}
class SharedLinkSettingsModal extends Component {
static propTypes = {
hideVanityNameSection: PropTypes.bool,
isOpen: PropTypes.bool,
onRequestClose: PropTypes.func,
submitting: PropTypes.bool,
/** Function called on form submission. Format is:
* ({
* expirationTimestamp: number (in milliseconds),
* isDownloadEnabled: true,
* isExpirationEnabled: true,
* isPasswordEnabled: true,
* password: string,
* vanityName: string,
* }) => void
*/
onSubmit: PropTypes.func.isRequired,
// access level props
/** the access level used for the item being shared */
accessLevel: PropTypes.string,
// Custom URL props
/** Whether or not user has permission to change/set vanity URL for this item */
canChangeVanityName: PropTypes.bool.isRequired,
/** Current vanity name for the item */
vanityName: PropTypes.string.isRequired,
/** Server URL prefix for vanity URL preview; should be something like http://company.box.com/v/ */
serverURL: PropTypes.string.isRequired,
vanityNameError: PropTypes.string,
// Password props
/** Whether or not user has permission to enable/disable/change password */
canChangePassword: PropTypes.bool.isRequired,
/** Whether or not the password section is visible to user */
isPasswordAvailable: PropTypes.bool.isRequired,
/** Whether or not password is currently enabled */
isPasswordEnabled: PropTypes.bool.isRequired,
passwordError: PropTypes.string,
// Expiration props
/** Whether or not user has permission to enable/disable/change expiration */
canChangeExpiration: PropTypes.bool.isRequired,
/** Current expiration timestamp, in milliseconds */
expirationTimestamp: PropTypes.number,
expirationError: PropTypes.string,
// Allow download props
/** Whether or not the download section is visible to user */
isDownloadAvailable: PropTypes.bool.isRequired,
/** Whether or not user has permission to enable/disable download */
canChangeDownload: PropTypes.bool.isRequired,
/** Whether or not download is currently enabled */
isDownloadEnabled: PropTypes.bool.isRequired,
// Direct link props
/** URL for direct link */
directLink: PropTypes.string.isRequired,
/** Whether or not direct link is available */
isDirectLinkAvailable: PropTypes.bool.isRequired,
/** Whether or not direct link is unavailable only due to download setting */
isDirectLinkUnavailableDueToDownloadSettings: PropTypes.bool.isRequired,
/** Whether or not direct link is unavailable only due to access policy setting */
isDirectLinkUnavailableDueToAccessPolicy: PropTypes.bool.isRequired,
// Classification props
item: PropTypes.object,
// Hooks for resin
cancelButtonProps: PropTypes.object,
directLinkInputProps: PropTypes.object,
downloadCheckboxProps: PropTypes.object,
expirationCheckboxProps: PropTypes.object,
expirationInputProps: PropTypes.object,
modalProps: PropTypes.object,
passwordCheckboxProps: PropTypes.object,
passwordInputProps: PropTypes.object,
saveButtonProps: PropTypes.object,
vanityNameInputProps: PropTypes.object,
};
static defaultProps = {
cancelButtonProps: {},
modalProps: {},
saveButtonProps: {},
};
constructor(props) {
super(props);
this.state = {
expirationDate: props.expirationTimestamp ? new Date(props.expirationTimestamp) : null,
expirationError: props.expirationError,
isVanityEnabled: !!props.vanityName,
isDownloadEnabled: props.isDownloadEnabled,
isExpirationEnabled: !!props.expirationTimestamp,
isPasswordEnabled: props.isPasswordEnabled,
password: '',
passwordError: props.passwordError,
vanityName: props.vanityName,
vanityNameError: props.vanityNameError,
};
}
componentWillReceiveProps(nextProps) {
const { expirationError, passwordError, vanityNameError } = nextProps;
if (
this.props.expirationError !== expirationError ||
this.props.passwordError !== passwordError ||
this.props.vanityNameError !== vanityNameError
) {
this.setState({
expirationError,
passwordError,
vanityNameError,
});
}
}
onSubmit = event => {
event.preventDefault();
const {
expirationDate,
isDownloadEnabled,
isExpirationEnabled,
isPasswordEnabled,
password,
vanityName,
} = this.state;
this.props.onSubmit({
expirationTimestamp: expirationDate ? expirationDate.getTime() : undefined,
isDownloadEnabled,
isExpirationEnabled,
isPasswordEnabled,
password,
vanityName,
});
};
onVanityNameChange = event => {
this.setState({
vanityName: event.target.value,
vanityNameError: undefined,
});
};
onPasswordChange = event => {
this.setState({
password: event.target.value,
passwordError: undefined,
});
};
onPasswordCheckboxChange = event => {
this.setState({ isPasswordEnabled: event.target.checked });
};
onExpirationDateChange = date => {
this.setState({ expirationDate: date, expirationError: undefined });
};
onExpirationCheckboxChange = event => {
this.setState({ isExpirationEnabled: event.target.checked });
};
onAllowDownloadChange = event => {
this.setState({ isDownloadEnabled: event.target.checked });
};
onVanityCheckboxChange = event => {
this.setState({
isVanityEnabled: event.target.checked,
vanityName: !event.target.checked ? '' : this.props.vanityName,
});
};
renderVanityNameSection() {
const {
canChangeVanityName,
hideVanityNameSection,
serverURL,
vanityNameInputProps,
warnOnPublic = false,
} = this.props;
const { vanityNameError, isVanityEnabled } = this.state;
if (hideVanityNameSection) {
return null;
}
return (