Skip to content

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

Refactoring SettingsDialog to functional component (#1274)

Stanimir9960ac66aae7
diff --git a/app/components/dialogs/settings/SettingsDialog.tsx b/app/components/dialogs/settings/SettingsDialog.tsx
index 32b4d5892..a4fbf6a46 100644
--- a/app/components/dialogs/settings/SettingsDialog.tsx
+++ b/app/components/dialogs/settings/SettingsDialog.tsx
@@ -16,7 +16,7 @@
*
*/
-import React from 'react';
+import React, { useEffect, useRef, useState } from 'react';
import { connect } from 'react-redux';
import Button from '@material-ui/core/Button';
import { withStyles } from '@material-ui/core/styles';
@@ -28,13 +28,14 @@ import DialogContent from '@material-ui/core/DialogContent';
import DialogTitle from '@material-ui/core/DialogTitle';
import withMobileDialog from '@material-ui/core/withMobileDialog';
import uuidv1 from 'uuid';
+import { useStateWithCallbackLazy } from 'use-state-with-callback';
import ConfirmDialog from '../ConfirmDialog';
import GenericDialog from '../GenericDialog';
import SettingsGeneral from '../settings/SettingsGeneral';
import SettingsKeyBindings from '../settings/SettingsKeyBindings';
import SettingsFileTypes from '../settings/SettingsFileTypes';
import i18n from '-/services/i18n';
-import { getSettings, actions } from '-/reducers/settings';
+import { actions, getSupportedFileTypes } from '-/reducers/settings';
import { extend } from '-/utils/misc';
import AppConfig from '-/config';
@@ -49,32 +50,24 @@ interface Props {
fullScreen?: boolean;
classes?: any;
onClose: () => void;
- setSupportedFileTypes?: (fileTypes: Array<Object>) => void;
- supportedFileTypes?: Array<Object>;
- settings?: Array<Object>;
+ setSupportedFileTypes?: (fileTypes: Array<any>) => void;
+ supportedFileTypes?: Array<any>;
}
-interface State {
- currentTab: number;
- items: Array<Object>;
- selectedItem: any;
- activeItem?: any;
- isValidationInProgress: boolean;
- isConfirmDialogOpened: boolean;
-}
-
-class SettingsDialog extends React.Component<Props, State> {
- state = {
- currentTab: 0,
- items: [],
- selectedItem: {},
- isValidationInProgress: false,
- isConfirmDialogOpened: false
- };
+const SettingsDialog = (props: Props) => {
+ const [items, setItems] = useStateWithCallbackLazy<Array<any>>([]);
+ const [currentTab, setCurrentTab] = useState<number>(0);
+ const [selectedItem, setSelectedItem] = useState<any>({});
+ const [isValidationInProgress, setIsValidationInProgress] = useState<boolean>(
+ false
+ );
+ const [isConfirmDialogOpened, setIsConfirmDialogOpened] = useState<boolean>(
+ false
+ );
+ const settingsFileTypeRef = useRef<HTMLDivElement>(null);
- componentWillReceiveProps = nextProps => {
- const { settings } = nextProps;
- const supportedFileTypes = settings.supportedFileTypes.reduce(
+ useEffect(() => {
+ const initSupportedFileTypes = props.supportedFileTypes.reduce(
(accumulator, fileType) => {
const modifiedFileType = extend({}, fileType, {
id: fileType.id || uuidv1()
@@ -86,40 +79,32 @@ class SettingsDialog extends React.Component<Props, State> {
},
[]
);
+ setItems(initSupportedFileTypes, undefined);
+ setIsValidationInProgress(false);
+ }, [props.supportedFileTypes]);
- this.setState({
- items: supportedFileTypes,
- isValidationInProgress: false
- });
+ const handleTabClick = (event, tab) => {
+ setCurrentTab(tab);
};
- settingsFileTypeRef;
-
- handleTabClick = (event, currentTab) => {
- this.setState({ currentTab });
- };
-
- onAddFileType = item => {
- const { items } = this.state;
- this.setState({ items: [...items, item] }, () => {
- const { settingsFileTypeRef } = this;
- const lastFileType = settingsFileTypeRef.querySelector('li:last-child');
- lastFileType.scrollIntoView({ behavior: 'smooth' });
+ const onAddFileType = (item = defaultFileTypeObject) => {
+ setItems([...items, item], () => {
+ if (settingsFileTypeRef && settingsFileTypeRef.current) {
+ const lastFileType = settingsFileTypeRef.current.querySelector(
+ 'li:last-child'
+ );
+ lastFileType.scrollIntoView({ behavior: 'smooth' });
+ }
});
};
- setSelectedItem = selectedItem => {
- this.setState({ selectedItem });
- };
-
- updateItems = (
+ const updateItems = (
identifierKey,
identifierValue,
targetKey,
targetValue,
disableSave = false
) => {
- const { items } = this.state;
let isSaveable = false;
let hasViewer = false;
const modifiedItems = items.reduce((accumulator, item) => {
@@ -135,48 +120,39 @@ class SettingsDialog extends React.Component<Props, State> {
return accumulator;
}, []);
- this.setState(
- {
- items: modifiedItems
- },
- () => {
- if (
- (targetKey !== 'type' && isSaveable && !disableSave) ||
- (targetKey === 'type' && hasViewer && isSaveable && !disableSave)
- ) {
- this.saveFileTypes();
- }
+ setItems(modifiedItems, () => {
+ if (
+ (targetKey !== 'type' && isSaveable && !disableSave) ||
+ (targetKey === 'type' && hasViewer && isSaveable && !disableSave)
+ ) {
+ saveFileTypes(modifiedItems);
}
- );
+ });
};
- getDefaultFileTypeObject = () => ({
+ const defaultFileTypeObject = {
id: uuidv1(),
type: '',
viewer: '',
editor: '',
color: '#2196f3'
- });
+ };
- saveFileTypes = () => {
- const { setSupportedFileTypes } = this.props;
- const { items } = this.state;
+ const saveFileTypes = newItems => {
+ const { setSupportedFileTypes } = props;
- this.setState({
- isValidationInProgress: true
- });
+ setIsValidationInProgress(true);
- const isValid = this.validateSelectedFileTypes();
+ const isValid = validateSelectedFileTypes();
if (!isValid) {
return false;
}
- setSupportedFileTypes(items);
+ setSupportedFileTypes(newItems);
};
- validateSelectedFileTypes = () => {
- const { items } = this.state;
+ const validateSelectedFileTypes = () => {
let isValid = true;
items.map(item => {
@@ -195,21 +171,21 @@ class SettingsDialog extends React.Component<Props, State> {
return isValid;
};
- removeItem = (itemForRemoval: any) => {
- const { items } = this.state;
+ const removeItem = (itemForRemoval: any) => {
const filteredItems = items.filter(
item => item.type !== itemForRemoval.type
);
- this.setState({ items: filteredItems }, () => this.saveFileTypes());
+ // setItems(filteredItems, undefined);
+ saveFileTypes(filteredItems);
};
- renderTitle = () => (
+ const renderTitle = () => (
<React.Fragment>
<DialogTitle>{i18n.t('core:options')}</DialogTitle>
<AppBar position="static" color="default">
<Tabs
- value={this.state.currentTab}
- onChange={this.handleTabClick}
+ value={currentTab}
+ onChange={handleTabClick}
indicatorColor="primary"
variant="fullWidth"
>
@@ -230,68 +206,61 @@ class SettingsDialog extends React.Component<Props, State> {
</React.Fragment>
);
- renderContent = () => (
- <DialogContent className={this.props.classes.mainContent}>
- <ConfirmDialog
- open={this.state.isConfirmDialogOpened}
- onClose={() => {
- this.setState({
- activeItem: {},
- isConfirmDialogOpened: false
- });
- }}
- title="Confirm"
- content={i18n.t('core:confirmFileTypeDeletion')}
- confirmCallback={result => {
- if (result) {
- const { selectedItem } = this.state;
- this.removeItem(selectedItem);
- }
- }}
- cancelDialogTID="cancelDeleteFileTypeDialog"
- confirmDialogTID="confirmDeleteFileTypeDialog"
- confirmDialogContentTID="confirmDeleteFileTypeDialogContent"
- />
+ const renderContent = () => (
+ <DialogContent className={props.classes.mainContent}>
+ {isConfirmDialogOpened && (
+ <ConfirmDialog
+ open={isConfirmDialogOpened}
+ onClose={() => {
+ setIsConfirmDialogOpened(false);
+ }}
+ title="Confirm"
+ content={i18n.t('core:confirmFileTypeDeletion')}
+ confirmCallback={result => {
+ if (result) {
+ removeItem(selectedItem);
+ }
+ }}
+ cancelDialogTID="cancelDeleteFileTypeDialog"
+ confirmDialogTID="confirmDeleteFileTypeDialog"
+ confirmDialogContentTID="confirmDeleteFileTypeDialogContent"
+ />
+ )}
<div
data-tid="settingsDialog"
- className={this.props.classes.mainContent}
- ref={el => {
- this.settingsFileTypeRef = el;
- }}
+ className={props.classes.mainContent}
+ ref={settingsFileTypeRef}
>
- {this.state.currentTab === 0 && <SettingsGeneral />}
- {this.state.currentTab === 1 && (
+ {currentTab === 0 && <SettingsGeneral />}
+ {currentTab === 1 && (
<SettingsFileTypes
- items={this.state.items}
- selectedItem={this.state.selectedItem}
- setSelectedItem={this.setSelectedItem}
- updateItems={this.updateItems}
- isValidationInProgress={this.state.isValidationInProgress}
- onRemoveItem={selectedItem => {
- this.setState({
- selectedItem,
- isConfirmDialogOpened: true
- });
+ items={items}
+ selectedItem={selectedItem}
+ setSelectedItem={item => setSelectedItem(item)}
+ updateItems={updateItems}
+ isValidationInProgress={isValidationInProgress}
+ onRemoveItem={item => {
+ setSelectedItem(item);
+ setIsConfirmDialogOpened(true);
}}
/>
)}
- {this.state.currentTab === 2 && <SettingsKeyBindings />}
+ {currentTab === 2 && <SettingsKeyBindings />}
</div>
</DialogContent>
);
- renderActions = () => (
+ const renderActions = () => (
<DialogActions
style={{
- justifyContent:
- this.state.currentTab === 1 ? 'space-between' : 'flex-end'
+ justifyContent: currentTab === 1 ? 'space-between' : 'flex-end'
}}
>
- {this.state.currentTab === 1 && (
+ {currentTab === 1 && (
<Button
- data-tid="closeSettingsDialog"
- onClick={() => this.onAddFileType(this.getDefaultFileTypeObject())}
+ data-tid="addNewFileTypeTID"
+ onClick={() => onAddFileType()}
color="secondary"
>
{i18n.t('core:addNewFileType')}
@@ -300,7 +269,7 @@ class SettingsDialog extends React.Component<Props, State> {
<Button
data-tid="closeSettingsDialog"
- onClick={this.props.onClose}
+ onClick={props.onClose}
color="primary"
>
{i18n.t('core:closeButton')}
@@ -308,23 +277,21 @@ class SettingsDialog extends React.Component<Props, State> {
</DialogActions>
);
- render() {
- const { fullScreen, open, onClose } = this.props;
- return (
- <GenericDialog
- open={open}
- onClose={onClose}
- fullScreen={fullScreen}
- renderTitle={this.renderTitle}
- renderContent={this.renderContent}
- renderActions={this.renderActions}
- />
- );
- }
-}
+ const { fullScreen, open, onClose } = props;
+ return (
+ <GenericDialog
+ open={open}
+ onClose={onClose}
+ fullScreen={fullScreen}
+ renderTitle={renderTitle}
+ renderContent={renderContent}
+ renderActions={renderActions}
+ />
+ );
+};
const mapStateToProps = state => ({
- settings: getSettings(state)
+ supportedFileTypes: getSupportedFileTypes(state)
});
const mapDispatchToProps = dispatch => ({
diff --git a/app/containers/MainPage.tsx b/app/containers/MainPage.tsx
index 574e94587..6f26c9eab 100644
--- a/app/containers/MainPage.tsx
+++ b/app/containers/MainPage.tsx
@@ -674,10 +674,12 @@ class MainPage extends Component<Props, State> {
onClose={toggleCreateFileDialog}
/>
)}
- <SettingsDialog
- open={isSettingsDialogOpened}
- onClose={toggleSettingsDialog}
- />
+ {isSettingsDialogOpened && (
+ <SettingsDialog
+ open={isSettingsDialogOpened}
+ onClose={toggleSettingsDialog}
+ />
+ )}
<Snackbar
data-tid={this.props.notificationStatus.tid}
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
diff --git a/package.json b/package.json
index 884387f61..1001d81a6 100644
--- a/package.json
+++ b/package.json
@@ -286,6 +286,7 @@
"semver": "^5.5.0",
"source-map-support": "^0.5.11",
"tiff.js": "^1.0.0",
+ "use-state-with-callback": "^2.0.3",
"winattr": "^3.0.0"
},
"devEngines": {
diff --git a/yarn.lock b/yarn.lock
index 02be76c1d..9dc053172 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -16596,6 +16596,11 @@ use-memo-one@^1.1.1:
resolved "https://registry.yarnpkg.com/use-memo-one/-/use-memo-one-1.1.1.tgz#39e6f08fe27e422a7d7b234b5f9056af313bd22c"
integrity sha512-oFfsyun+bP7RX8X2AskHNTxu+R3QdE/RC5IefMbqptmACAA/gfol1KDD5KRzPsGMa62sWxGZw+Ui43u6x4ddoQ==
+use-state-with-callback@^2.0.3:
+ version "2.0.3"
+ resolved "https://registry.yarnpkg.com/use-state-with-callback/-/use-state-with-callback-2.0.3.tgz#3be56ca52b6afcfe364e0579749dfe588c47c01f"
+ integrity sha512-bJXNAtJZW0xz16GIW54aDbYxRkZa2c8RdoKGQnJqE7GmRdVkZkKQBTR/9vH6T5tZX/7yCIhywE09NyeTQ9EJJQ==
+
use@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"