passbolt-styleguide
Version:
Passbolt styleguide contains common styling assets used by the different sites, plugin, etc.
358 lines (327 loc) • 10.3 kB
JavaScript
/**
* Passbolt ~ Open source password manager for teams
* Copyright (c) 2020 Passbolt SA (https://www.passbolt.com)
*
* Licensed under GNU Affero General Public License version 3 of the or any later version.
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) 2020 Passbolt SA (https://www.passbolt.com)
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
* @link https://www.passbolt.com Passbolt(tm)
* @since 2.12.0
*/
import React, { Component } from "react";
import PropTypes from "prop-types";
import { withAppContext } from "../../../../shared/context/AppContext/AppContext";
import NotifyError from "../../Common/Error/NotifyError/NotifyError";
import DialogWrapper from "../../Common/Dialog/DialogWrapper/DialogWrapper";
import FormSubmitButton from "../../Common/Inputs/FormSubmitButton/FormSubmitButton";
import FormCancelButton from "../../Common/Inputs/FormSubmitButton/FormCancelButton";
import { withDialog } from "../../../contexts/DialogContext";
import { withActionFeedback } from "../../../contexts/ActionFeedbackContext";
import { Trans, withTranslation } from "react-i18next";
import { maxSizeValidation } from "../../../lib/Error/InputValidator";
import { RESOURCE_FOLDER_NAME_MAX_LENGTH } from "../../../../shared/constants/inputs.const";
import AttentionSVG from "../../../../img/svg/attention.svg";
class RenameResourceFolder extends Component {
/**
* Constructor
* @param {Object} props
* @param {Object} context
*/
constructor(props) {
super(props);
this.state = this.getStateBasedOnContext(props, this.getDefaultState());
this.createInputRefs();
this.bindEventHandlers();
}
/**
* ComponentDidMount
* Invoked immediately after component is inserted into the tree
* @return {void}
*/
async componentDidMount() {
this.setState({ loading: false }, () => {
this.nameRef.current.focus();
});
}
/**
* Return default state
* @returns {Object} default state
*/
getDefaultState() {
return {
// Dialog states
loading: true,
processing: false,
inlineValidation: false,
// Fields and errors
name: this.translate("loading..."),
nameError: false,
nameWarning: "",
};
}
/**
* Return default state based on context and props
* For example if folder doesn't exist then we show an error message
* Otherwise set the input name value
*
* @param props
* @param defaultState
* @returns {*}
*/
getStateBasedOnContext(props, defaultState) {
const folders = props.context.folders;
const error = {
message: this.translate("The folder could not be found. Maybe it was deleted or you lost access."),
};
if (!folders) {
console.error(`No folders context defined.`);
this.handleError(error);
}
const folder = props.context.folders.find((item) => item.id === props.context.folder.id) || false;
if (!folder) {
console.error(`Folder ${props.context.folder.id} not found in context.`);
this.handleError(error);
} else {
defaultState.name = folder.name;
}
return defaultState;
}
/**
* Create references
* @returns {void}
*/
createInputRefs() {
this.nameRef = React.createRef();
}
/**
* Bind event handlers
* @returns {void}
*/
bindEventHandlers() {
this.handleClose = this.handleClose.bind(this);
this.handleFormSubmit = this.handleFormSubmit.bind(this);
this.handleInputChange = this.handleInputChange.bind(this);
this.handleNameInputKeyUp = this.handleNameInputKeyUp.bind(this);
}
/**
* Handle close button click.
* @returns {void}
*/
handleClose() {
this.props.onClose();
}
/**
* Handle form input changes.
* @params {ReactEvent} The react event
* @returns {void}
*/
handleInputChange(event) {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState(
{
[name]: value,
},
() => {
if (this.state.inlineValidation) {
this.hasErrors();
}
},
);
}
/**
* Handle form submit event.
* @params {ReactEvent} The react event
* @returns {void}
*/
async handleFormSubmit(event) {
event.preventDefault();
// Do not re-submit an already processing form
if (this.state.processing) {
return;
}
// After first submit, inline validation is on
this.setState({
inlineValidation: this.state.inlineValidation || true,
});
this.setState({ processing: true });
if (this.hasErrors()) {
this.setState({ processing: false });
this.focusFirstFieldError();
return;
}
try {
const folder = await this.updateFolder();
await this.handleSaveSuccess(folder.id);
} catch (error) {
this.handleSaveError(error);
}
}
/**
* Handle save operation success.
*/
async handleSaveSuccess(folderId) {
await this.props.actionFeedbackContext.displaySuccess(this.translate("The folder was renamed successfully"));
this.selectAndScrollToFolder(folderId);
this.props.onClose();
}
/**
* Handle save operation error.
* @param {object} error The returned error
*/
handleSaveError(error) {
// It can happen when the user has closed the passphrase entry dialog by instance.
if (error.name === "UserAbortsOperationError") {
this.setState({ processing: false });
} else {
// Unexpected error occurred.
console.error(error);
this.handleError(error);
this.setState({ processing: false });
}
}
/**
* handle error to display the error dialog
* @param error
*/
handleError(error) {
const errorDialogProps = {
error: error,
};
this.props.dialogContext.open(NotifyError, errorDialogProps);
}
/**
* Focus the first field of the form which is in error state.
* @returns {void}
*/
focusFirstFieldError() {
this.nameRef.current.focus();
}
/**
* Update the folder
* @returns {Promise<Object>} Folder entity or Error
*/
async updateFolder() {
const folderDto = {
id: this.props.context.folder.id,
name: this.state.name,
};
return await this.props.context.port.request("passbolt.folders.update", folderDto);
}
/**
* Select and scroll to a given resource.
* @param {string} id The resource id.
* @returns {void}
*/
selectAndScrollToFolder(id) {
this.props.context.port.emit("passbolt.folders.select-and-scroll-to", id);
}
/**
* Check if the form has errors.
* @returns {boolean}
*/
hasErrors() {
let nameError = false;
const name = this.state.name.trim();
if (!name.length) {
nameError = this.translate("A name is required.");
}
if (name.length > 256) {
nameError = this.translate("A name can not be more than 256 char in length.");
}
this.setState({ nameError: nameError });
return Boolean(nameError);
}
/**
* Handle name input keyUp event.
*/
handleNameInputKeyUp() {
const nameWarning = maxSizeValidation(this.state.name, RESOURCE_FOLDER_NAME_MAX_LENGTH, this.translate);
this.setState({ nameWarning });
}
/**
* Should input be disabled? True if state is loading or processing
* @returns {boolean}
*/
hasAllInputDisabled() {
return this.state.processing || this.state.loading;
}
/**
* Get the translate function
* @returns {function(...[*]=)}
*/
get translate() {
return this.props.t;
}
/**
* Render
* @returns {*}
*/
render() {
return (
<DialogWrapper
className="rename-folder-dialog"
title={this.translate("Rename a folder")}
onClose={this.handleClose}
disabled={this.hasAllInputDisabled()}
>
<form className="folder-rename-form" onSubmit={this.handleFormSubmit} noValidate>
<div className="form-content">
<div
className={`input text required ${this.state.nameError ? "error" : ""} ${this.hasAllInputDisabled() ? "disabled" : ""}`}
>
<label htmlFor="folder-name-input">
<Trans>Folder name</Trans>
{this.state.nameWarning && <AttentionSVG className="attention-required" />}
</label>
<input
id="folder-name-input"
name="name"
ref={this.nameRef}
type="text"
value={this.state.name}
placeholder={this.translate("Untitled folder")}
maxLength="256"
required="required"
onChange={this.handleInputChange}
onKeyUp={this.handleNameInputKeyUp}
disabled={this.hasAllInputDisabled()}
autoComplete="off"
autoFocus={true}
/>
{this.state.nameError && <div className="name error-message">{this.state.nameError}</div>}
{this.state.nameWarning && (
<div className="name warning-message">
<strong>
<Trans>Warning:</Trans>
</strong>{" "}
{this.state.nameWarning}
</div>
)}
</div>
</div>
<div className="submit-wrapper clearfix">
<FormCancelButton disabled={this.hasAllInputDisabled()} onClick={this.handleClose} />
<FormSubmitButton
disabled={this.hasAllInputDisabled()}
processing={this.state.processing}
value={this.translate("Rename")}
/>
</div>
</form>
</DialogWrapper>
);
}
}
RenameResourceFolder.propTypes = {
context: PropTypes.any, // The application context
onClose: PropTypes.func,
actionFeedbackContext: PropTypes.any, // The action feedback context
dialogContext: PropTypes.any, // The dialog context
t: PropTypes.func, // The translation function
};
export default withAppContext(withDialog(withActionFeedback(withTranslation("common")(RenameResourceFolder))));