Skip to content

TagSpaces is an offline, open source, document manager with tagging support

TSN305 adding hooks to CreatDirecctoryDialog.js

Mladen Sapundshievc1ebc216781e
diff --git a/app/components/dialogs/CreateDirectoryDialog.js b/app/components/dialogs/CreateDirectoryDialog.js
index bacbab3e5..81ec6a92a 100644
--- a/app/components/dialogs/CreateDirectoryDialog.js
+++ b/app/components/dialogs/CreateDirectoryDialog.js
@@ -17,7 +17,7 @@
* @flow
*/
-import React from 'react';
+import React, { useState } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import Button from '@material-ui/core/Button';
@@ -33,129 +33,125 @@ import AppConfig from '../../config';
import { actions as AppActions } from '../../reducers/app';
type Props = {
+ classes: Object,
open: boolean,
onClose: () => void,
selectedDirectoryPath: string,
createDirectory: (directoryPath: string) => void
};
-type State = {
- disableConfirmButton?: boolean,
- inputError?: boolean,
- name?: string
-};
-
-
-class CreateDirectoryDialog extends React.Component<Props, State> {
- state = {
- inputError: false,
- disableConfirmButton: true,
- name: '',
- };
+const CreateDirectoryDialog = (props: Props) => {
+ const [inputError, setInputError] = useState(false);
+ const [disableConfirmButton, setDisableConfirmButton] = useState(true);
+ const [name, setName] = useState('');
- handleInputChange = (event: Object) => {
+ function handleInputChange(event: Object) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value
- }, this.handleValidation());
- };
+ }, handleValidation());
+ }
- handleValidation() {
+ function handleValidation() {
// const pathRegex = '^((\.\./|[a-zA-Z0-9_/\-\\])*\.[a-zA-Z0-9]+)$';
// const nameRegex = '^[A-Z][-a-zA-Z]+$';
- if (this.state.name.length > 0) {
- this.setState({ inputError: false, disableConfirmButton: false });
+ if (name.length > 0) {
+ setInputError(false);
+ setDisableConfirmButton(false); // this.setState({ inputError: false, disableConfirmButton: false });
} else {
- this.setState({ inputError: true, disableConfirmButton: true });
+ setInputError(true); // this.setState({ inputError: true, disableConfirmButton: true });
+ setDisableConfirmButton(true);
}
}
- onConfirm = () => {
- if (!this.state.disableConfirmButton && this.state.name) {
- const dirPath = this.props.selectedDirectoryPath + AppConfig.dirSeparator + this.state.name;
- this.props.createDirectory(dirPath);
- this.resetState();
- this.props.onClose();
+ function onConfirm() {
+ if (!disableConfirmButton && name) {
+ const dirPath = props.selectedDirectoryPath + AppConfig.dirSeparator + name;
+ props.createDirectory(dirPath);
+ resetState();
+ props.onClose();
}
- };
-
- onCancel = () => {
- this.resetState();
- this.props.onClose();
}
- resetState = () => {
- this.setState({
- name: '',
- inputError: false,
- disableConfirmButton: true
- });
+ function onCancel() {
+ resetState();
+ props.onClose();
}
- renderTitle = () => (
- <DialogTitle>{i18n.t('core:createNewDirectoryTitle')}</DialogTitle>
- );
+ function resetState() { // this.setState({
+ setName(''); // name: '',
+ setInputError(false); // inputError: false,
+ setDisableConfirmButton(true); // disableConfirmButton: true,
+ }
- renderContent = () => (
- <DialogContent>
- <FormControl
- fullWidth={true}
- error={this.state.inputError}
- >
- <TextField
- fullWidth
- error={this.state.inputError}
- margin="dense"
- autoFocus
- name="name"
- label={i18n.t('core:createNewDirectoryTitleName')}
- onChange={this.handleInputChange}
- value={this.state.name}
- data-tid="directoryName"
- id="directoryName"
- />
- <FormHelperText>{i18n.t('core:directoryNameHelp')}</FormHelperText>
- </FormControl>
- </DialogContent>
- );
+ function renderTitle() {
+ return (
+ <DialogTitle>{i18n.t('core:createNewDirectoryTitle')}</DialogTitle>
+ );
+ }
- renderActions = () => (
- <DialogActions>
- <Button
- data-tid="closeCreateNewDirectory"
- onClick={this.onCancel}
- color="primary"
- >
- {i18n.t('core:cancel')}
- </Button>
- <Button
- disabled={this.state.disableConfirmButton}
- onClick={this.onConfirm}
- data-tid="confirmCreateNewDirectory"
- id="confirmCreateNewDirectory"
- color="primary"
- >
- {i18n.t('core:ok')}
- </Button>
- </DialogActions>
- );
+ function renderContent() {
+ return (
+ <DialogContent>
+ <FormControl
+ fullWidth={true}
+ error={inputError}
+ >
+ <TextField
+ fullWidth
+ error={inputError}
+ margin="dense"
+ autoFocus
+ name="name"
+ label={i18n.t('core:createNewDirectoryTitleName')}
+ onChange={handleInputChange}
+ value={name}
+ data-tid="directoryName"
+ id="directoryName"
+ />
+ <FormHelperText>{i18n.t('core:directoryNameHelp')}</FormHelperText>
+ </FormControl>
+ </DialogContent>
+ );
+ }
- render() {
+ function renderActions() {
return (
- <GenericDialog
- open={this.props.open}
- onClose={this.onCancel}
- onEnterKey={(event) => onEnterKeyHandler(event, this.onConfirm)}
- renderTitle={this.renderTitle}
- renderContent={this.renderContent}
- renderActions={this.renderActions}
- />
+ <DialogActions>
+ <Button
+ data-tid="closeCreateNewDirectory"
+ onClick={onCancel}
+ color="primary"
+ >
+ {i18n.t('core:cancel')}
+ </Button>
+ <Button
+ disabled={disableConfirmButton}
+ onClick={onConfirm}
+ data-tid="confirmCreateNewDirectory"
+ id="confirmCreateNewDirectory"
+ color="primary"
+ >
+ {i18n.t('core:ok')}
+ </Button>
+ </DialogActions>
);
}
-}
+
+ return (
+ <GenericDialog
+ open={props.open}
+ onClose={onCancel}
+ onEnterKey={(event) => onEnterKeyHandler(event, onConfirm)}
+ renderTitle={renderTitle}
+ renderContent={renderContent}
+ renderActions={renderActions}
+ />
+ );
+};
function mapActionCreatorsToProps(dispatch) {
return bindActionCreators({