Skip to content

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

TSN305 EditTagGroupDialog migrate to hooks

Mladen Sapundshiev7e8f51a6d8b9
diff --git a/app/components/dialogs/EditTagGroupDialog.js b/app/components/dialogs/EditTagGroupDialog.js
index d5d2a67ce..1af1009f5 100644
--- a/app/components/dialogs/EditTagGroupDialog.js
+++ b/app/components/dialogs/EditTagGroupDialog.js
@@ -17,7 +17,7 @@
* @flow
*/
-import React from 'react';
+import React, { useState } from 'react';
import Button from '@material-ui/core/Button';
import moment from 'moment';
import TextField from '@material-ui/core/TextField';
@@ -34,13 +34,14 @@ import i18n from '../../services/i18n';
import TransparentBackground from '../TransparentBackground';
type Props = {
+ // classes: Object,
open?: boolean,
editTagGroup: (tagGroup: TagGroup) => void,
selectedTagGroupEntry?: TagGroup,
onClose: () => void
};
-type State = {
+/*type State = {
inputError?: boolean,
displayColorPicker?: boolean,
displayTextColorPicker?: boolean,
@@ -50,9 +51,9 @@ type State = {
color?: string,
textcolor?: string,
modifiedDate?: string
-};
+};*/
-class EditTagGroupDialog extends React.Component<Props, State> {
+/*class EditTagGroupDialog extends React.Component<Props, State> {
state = {
displayColorPicker: false,
displayTextColorPicker: false,
@@ -63,7 +64,18 @@ class EditTagGroupDialog extends React.Component<Props, State> {
color: '',
textcolor: '',
modifiedDate: ''
- };
+ };*/
+
+const EditTagGroupDialog = (props: Props) => {
+ const [displayColorPicker, setDisplayColorPicker] = useState(false);
+ const [displayTextColorPicker, setDisplayTextColorPicker] = useState(false);
+ cosnt [inputError, setInputError] = useState(false);
+ const [disableConfirmButton, setDisableConfirmButton] = useState(true);
+ const [applyChanges, setApplyChanges] = useState(false);
+ const [title, setTitle] = useState('');
+ const [color, setColor] = useState('');
+ const [textcolor, setTextcolor] = useState('');
+ const [modifiedDate, setModifiedData] = useState('');
componentWillReceiveProps = ({ open, selectedTagGroupEntry }) => {
if (open === true) {
@@ -76,7 +88,20 @@ class EditTagGroupDialog extends React.Component<Props, State> {
modifiedDate: selectedTagGroupEntry.modified_date
});
}
- };
+ }
+
+
+ /* function componentWillReceiveProps({ open, selectedTagGroupEntry }) {
+ if (open === true) {
+ setDisplayTextColorPicker(!selectedTagGroupEntry.title);
+ setApplyChanges(false);
+ setTitle(selectedTagGroupEntry.title);
+ setColor(selectedTagGroupEntry.color);
+ setTestcolor(selectedTagGroupEntry.textcolor);
+ setModifiedData(selectedTagGroupEntry.modified_date);
+ }
+ } */
+
handleInputChange = (event: Object) => {
const target = event.target;
@@ -86,17 +111,27 @@ class EditTagGroupDialog extends React.Component<Props, State> {
this.setState({
[name]: value
}, this.handleValidation);
- };
+ }
- handleValidation() {
+ /* handleValidation() {
if (this.state.title && this.state.title.length > 0) {
this.setState({ inputError: false, disableConfirmButton: false });
} else {
this.setState({ inputError: true, disableConfirmButton: true });
}
+ } */
+
+ function handleValidation() {
+ if (title && title.length > 0) {
+ setInputError(false);
+ setDisableConfirmButton(false);
+ } else {
+ setInputError(true);
+ setDisableConfirmButton(true);
+ }
}
- onConfirm = () => {
+ /* onConfirm = () => {
const { disableConfirmButton, applyChanges } = this.state;
const { selectedTagGroupEntry } = this.props;
if (disableConfirmButton) {
@@ -119,33 +154,114 @@ class EditTagGroupDialog extends React.Component<Props, State> {
this.setState({ inputError: false, disableConfirmButton: true });
this.props.onClose();
}
- };
+ }; */
+
+ function onConfirm() {
+ const { disableConfirmButton, applyChanges } = state;
+ const { selectedTagGroupEntry } = props;
+ if (disableConfirmButton) {
+ return;
+ }
- toggleDefaultTagBackgroundColorPicker = () => {
+ if (selectedTagGroupEntry && selectedTagGroupEntry.children) {
+ props.editTagGroup({
+ ...selectedTagGroupEntry,
+ title: title,
+ color: color,
+ textcolor: textcolor,
+ children: selectedTagGroupEntry.children.map((tag) => ({
+ ...tag,
+ color: applyChanges ? color : tag.color,
+ textcolor: applyChanges ? textcolor : tag.textcolor,
+ style: tag.style
+ }))
+ });
+ setInputError(false);
+ setDisableConfirmButton(true);
+ props.onClose();
+ }
+ }
+
+ /* toggleDefaultTagBackgroundColorPicker = () => {
this.setState({ displayColorPicker: !this.state.displayColorPicker });
- };
+ }; */
+
+ function toggleDefaultTagBackgroundColorPicker() {
+ setDisplayColorPicker(!displayColorPicker);
+ }
- toggleDefaultTagTextColorPicker = () => {
+ /* toggleDefaultTagTextColorPicker = () => {
this.setState({ displayTextColorPicker: !this.state.displayTextColorPicker });
- };
+ }; */
- handleChangeColor = (color: string) => {
+ function toggleDefaultTagTextColorPicker() {
+ setDisplayTextColorPicker(!displayTextColorPicker );
+ }
+
+ /* handleChangeColor = (color: string) => {
this.setState({ color });
- };
+ }; */
+
+ function handleChangeColor(color) {
+ setColor(color);
+ }
+
+ /* handleChangeTextColor = (color: string) => {
+ this.setState({ textcolor: color });
+ }; */
- handleChangeTextColor = (color: string) => {
- this.setState({ textcolor: color });
- };
+ function handleChangeTextColor(color) {
+ setTextcolor(color);
+ }
- setApplyChanges = (applyColorChanges: boolean) => {
- this.setState({ applyChanges: applyColorChanges });
- };
+ /* setApplyChanges = (applyColorChanges: boolean) => {
+ this.setState({ applyChanges: applyColorChanges });
+ }; */
- renderTitle = () => (
- <DialogTitle style={{ overflow: 'visible' }}>{i18n.t('core:editTagGroupTitle')}{` '${this.state.title}'`}</DialogTitle>
- );
+ function setApplyChanges() {
+ setApplyChanges(applyColorChanges);
+ }
+
+ /* renderTitle = () => (
+ <DialogTitle style={{ overflow: 'visible' }}>{i18n.t('core:editTagGroupTitle')}{` '${this.state.title}'`}</DialogTitle>
+ ); */
+
+ function renderTitle() {
+ return (
+ <DialogTitle style={{ overflow: 'visible' }}>{i18n.t('core:editTagGroupTitle')}{`'${title}'`}</DialogTitle>
+ );
+ }
- renderContent = () => {
+ /* renderContent = () => {
+ const { color, textcolor, modifiedDate } = this.state;
+ const styles = {
+ color: {
+ width: '100%',
+ height: 30,
+ borderRadius: 2,
+ borderWidth: 1,
+ borderStyle: 'solid',
+ borderColor: 'gray',
+ padding: '5px',
+ background: color
+ },
+ textcolor: {
+ width: '100%',
+ height: 30,
+ borderRadius: 2,
+ borderWidth: 1,
+ borderStyle: 'solid',
+ borderColor: 'gray',
+ padding: '5px',
+ background: textcolor
+ },
+ helpText: {
+ marginBottom: '5px',
+ fontSize: '1rem'
+ },
+ }; */
+
+ function renderContent() {
const { color, textcolor, modifiedDate } = this.state;
const styles = {
color: {
@@ -173,84 +289,85 @@ class EditTagGroupDialog extends React.Component<Props, State> {
fontSize: '1rem'
},
};
+ }
- return (
- <DialogContent style={{ overflow: 'visible' }}>
- <FormControl
- fullWidth={true}
+ return (
+ <DialogContent style={{ overflow: 'visible' }}>
+ <FormControl
+ fullWidth={true}
+ error={this.state.inputError}
+ style={{ overflow: 'visible' }}
+ >
+ {modifiedDate !== '' && (
+ <div className="tag-date" style={{ fontSize: 12, position: 'relative', bottom: 20, color: '#808080' }}>
+ <span className="text" style={{ fontWeight: 600 }}>{`${i18n.t('core:modifiedDate')}: `}</span>
+ <time>{moment(modifiedDate).format('MMM. DD, YYYY')}</time>
+ </div>
+ )}
+ <TextField
error={this.state.inputError}
- style={{ overflow: 'visible' }}
- >
- {modifiedDate !== '' && (
- <div className="tag-date" style={{ fontSize: 12, position: 'relative', bottom: 20, color: '#808080' }}>
- <span className="text" style={{ fontWeight: 600 }}>{`${i18n.t('core:modifiedDate')}: `}</span>
- <time>{moment(modifiedDate).format('MMM. DD, YYYY')}</time>
- </div>
- )}
- <TextField
- error={this.state.inputError}
- margin="dense"
- name="title"
- label={i18n.t('core:editTagGroupNewName')}
- onChange={this.handleInputChange}
- value={this.state.title}
- data-tid="editTagGroupInput"
- fullWidth={true}
- />
- {this.state.inputError && <FormHelperText>{i18n.t('core:taggroupTitleHelper')}</FormHelperText>}
- </FormControl>
- <FormControl
+ margin="dense"
+ name="title"
+ label={i18n.t('core:editTagGroupNewName')}
+ onChange={this.handleInputChange}
+ value={this.state.title}
+ data-tid="editTagGroupInput"
fullWidth={true}
- >
- <FormHelperText style={styles.helpText}>{i18n.t('core:tagBackgroundColor')}</FormHelperText>
- <TransparentBackground>
- <Button
- onClick={this.toggleDefaultTagBackgroundColorPicker}
- data-tid="editTagGroupBackgroundColor"
- style={styles.color}
- >&nbsp;
- </Button>
- </TransparentBackground>
- <ColorPickerDialog
- open={this.state.displayColorPicker}
- setColor={this.handleChangeColor}
- onClose={this.toggleDefaultTagBackgroundColorPicker}
- color={this.state.color}
- />
- </FormControl>
- <FormControl
- fullWidth={true}
- >
- <FormHelperText style={styles.helpText}>{i18n.t('core:tagForegroundColor')}</FormHelperText>
- <TransparentBackground>
- <Button
- onClick={this.toggleDefaultTagTextColorPicker}
- data-tid="editTagGroupForegroundColor"
- style={styles.textcolor}
- role="presentation"
- >&nbsp;
- </Button>
- </TransparentBackground>
- <ColorPickerDialog
- open={this.state.displayTextColorPicker}
- setColor={this.handleChangeTextColor}
- onClose={this.toggleDefaultTagTextColorPicker}
- color={this.state.textcolor}
- />
- </FormControl>
- <FormControl>
- <FormHelperText style={styles.helpText}>{i18n.t('core:colorChangesToAllTags')}</FormHelperText>
- <Switch
- data-tid="editTagGroupSwitch"
- onClick={() => this.setApplyChanges(!this.state.applyChanges)}
- checked={this.state.applyChanges}
- />
- </FormControl>
- </DialogContent>
- );
- };
+ />
+ {this.state.inputError && <FormHelperText>{i18n.t('core:taggroupTitleHelper')}</FormHelperText>}
+ </FormControl>
+ <FormControl
+ fullWidth={true}
+ >
+ <FormHelperText style={styles.helpText}>{i18n.t('core:tagBackgroundColor')}</FormHelperText>
+ <TransparentBackground>
+ <Button
+ onClick={this.toggleDefaultTagBackgroundColorPicker}
+ data-tid="editTagGroupBackgroundColor"
+ style={styles.color}
+ >&nbsp;
+ </Button>
+ </TransparentBackground>
+ <ColorPickerDialog
+ open={this.state.displayColorPicker}
+ setColor={this.handleChangeColor}
+ onClose={this.toggleDefaultTagBackgroundColorPicker}
+ color={this.state.color}
+ />
+ </FormControl>
+ <FormControl
+ fullWidth={true}
+ >
+ <FormHelperText style={styles.helpText}>{i18n.t('core:tagForegroundColor')}</FormHelperText>
+ <TransparentBackground>
+ <Button
+ onClick={this.toggleDefaultTagTextColorPicker}
+ data-tid="editTagGroupForegroundColor"
+ style={styles.textcolor}
+ role="presentation"
+ >&nbsp;
+ </Button>
+ </TransparentBackground>
+ <ColorPickerDialog
+ open={this.state.displayTextColorPicker}
+ setColor={this.handleChangeTextColor}
+ onClose={this.toggleDefaultTagTextColorPicker}
+ color={this.state.textcolor}
+ />
+ </FormControl>
+ <FormControl>
+ <FormHelperText style={styles.helpText}>{i18n.t('core:colorChangesToAllTags')}</FormHelperText>
+ <Switch
+ data-tid="editTagGroupSwitch"
+ onClick={() => this.setApplyChanges(!this.state.applyChanges)}
+ checked={this.state.applyChanges}
+ />
+ </FormControl>
+ </DialogContent>
+ );
+
- renderActions = () => (
+ /*renderActions = () => (
<DialogActions>
<Button
onClick={this.props.onClose}
@@ -267,17 +384,38 @@ class EditTagGroupDialog extends React.Component<Props, State> {
{i18n.t('core:ok')}
</Button>
</DialogActions>
- );
+ );*/
+
+ function renderActions() {
+ return (
+ <DialogActions>
+ <Button
+ onClick={props.onClose}
+ color="primary"
+ >
+ {i18n.t('core:cancel')}
+ </Button>
+ <Button
+ disabled={disableConfirmButton}
+ onClick={onConfirm}
+ data-tid="editTagGroupConfirmButton"
+ color="primary"
+ >
+ {i18n.t('core:ok')}
+ </Button>
+ </DialogActions>
+ );
+ }
- render() {
+ function render() {
return (
<GenericDialog
- open={this.props.open}
- onClose={this.props.onClose}
- onEnterKey={(event) => onEnterKeyHandler(event, this.onConfirm)}
- renderTitle={this.renderTitle}
- renderContent={this.renderContent}
- renderActions={this.renderActions}
+ open={props.open}
+ onClose={props.onClose}
+ onEnterKey={(event) => onEnterKeyHandler(event, onConfirm)}
+ renderTitle={renderTitle}
+ renderContent={renderContent}
+ renderActions={renderActions}
/>
);
}