From 2f199ce0c9e46ffe10143d2190989efdf73963dc Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Fri, 14 Feb 2020 12:32:53 +0200 Subject: [PATCH] JournalEdit: change to react-hooks. --- src/Journals/JournalEdit.tsx | 266 ++++++++++++++++------------------- 1 file changed, 122 insertions(+), 144 deletions(-) diff --git a/src/Journals/JournalEdit.tsx b/src/Journals/JournalEdit.tsx index 52ad358..f05ec22 100644 --- a/src/Journals/JournalEdit.tsx +++ b/src/Journals/JournalEdit.tsx @@ -5,7 +5,6 @@ import Select from '@material-ui/core/Select'; import MenuItem from '@material-ui/core/MenuItem'; import FormControl from '@material-ui/core/FormControl'; import InputLabel from '@material-ui/core/InputLabel'; -import { Theme, withTheme } from '@material-ui/core/styles'; import IconDelete from '@material-ui/icons/Delete'; import IconCancel from '@material-ui/icons/Clear'; import IconSave from '@material-ui/icons/Save'; @@ -17,7 +16,6 @@ import ConfirmationDialog from '../widgets/ConfirmationDialog'; import * as EteSync from 'etesync'; import { SyncInfo } from '../SyncGate'; -import { handleInputChange } from '../helpers'; interface PropsType { syncInfo: SyncInfo; @@ -27,160 +25,140 @@ interface PropsType { onCancel: () => void; } -interface PropsTypeInner extends PropsType { - theme: Theme; -} +export default function JournalEdit(props: PropsType) { + const [showDeleteDialog, setShowDeleteDialog] = React.useState(false); + const [info, setInfo] = React.useState(); -class JournalEdit extends React.PureComponent { - public state = { - info: { - uid: '', - type: '', - displayName: '', - description: '', - } as EteSync.CollectionInfo, + React.useEffect(() => { + if (props.item !== undefined) { + setInfo(props.item); + } else { + setInfo({ + uid: EteSync.genUid(), + type: 'ADDRESS_BOOK', + displayName: '', + description: '', + } as EteSync.CollectionInfo); + } + }, [props.item]); - showDeleteDialog: false, - }; + if (info === undefined) { + return ; + } - private handleInputChange: any; + function onSubmit(e: React.FormEvent) { + e.preventDefault(); - constructor(props: PropsTypeInner) { - super(props); - this.handleInputChange = handleInputChange(this, 'info'); - this.onSubmit = this.onSubmit.bind(this); - this.onDeleteRequest = this.onDeleteRequest.bind(this); + const { onSave } = props; + const item = new EteSync.CollectionInfo(info); - if (this.props.item !== undefined) { - const collection = this.props.item; + onSave(item, props.item); + } - this.state.info = { ...collection }; - } else { - this.state.info.uid = EteSync.genUid(); - this.state.info.type = 'ADDRESS_BOOK'; - } + function onDeleteRequest() { + setShowDeleteDialog(true); } - public render() { - const { item, onDelete, onCancel } = this.props; - - const pageTitle = (item !== undefined) ? item.displayName : 'New Journal'; - - const styles = { - fullWidth: { - width: '100%', - }, - submit: { - marginTop: 40, - marginBottom: 20, - textAlign: 'right' as any, - }, - }; - - const journalTypes = { - ADDRESS_BOOK: 'Address Book', - CALENDAR: 'Calendar', - TASKS: 'Task List', - }; - - return ( - <> - - -
- - - Collection type - - - - - - -
- + const { item, onDelete, onCancel } = props; - {this.props.item && - - } + const pageTitle = (item !== undefined) ? item.displayName : 'New Journal'; + const styles = { + fullWidth: { + width: '100%', + }, + submit: { + marginTop: 40, + marginBottom: 20, + textAlign: 'right' as any, + }, + }; + + const journalTypes = { + ADDRESS_BOOK: 'Address Book', + CALENDAR: 'Calendar', + TASKS: 'Task List', + }; + + return ( + <> + + + + + + Collection type + + + + ) => setInfo({ ...info, displayName: event.target.value })} + style={styles.fullWidth} + margin="normal" + /> + ) => setInfo({ ...info, description: event.target.value })} + style={styles.fullWidth} + margin="normal" + /> + +
+ + + {props.item && -
- -
- onDelete(this.props.item!)} - onCancel={() => this.setState({ showDeleteDialog: false })} - > - Are you sure you would like to delete this journal? - - - ); - } - - private onSubmit(e: React.FormEvent) { - e.preventDefault(); - - const { onSave } = this.props; - const item = new EteSync.CollectionInfo(this.state.info); - - onSave(item, this.props.item); - } - - private onDeleteRequest() { - this.setState({ - showDeleteDialog: true, - }); - } - + } + + +
+ +
+ onDelete(props.item!)} + onCancel={() => setShowDeleteDialog(false)} + > + Are you sure you would like to delete this journal? + + + ); } - -export default withTheme(JournalEdit);