JournalEdit: change to react-hooks.

master
Tom Hacohen 5 years ago
parent b76a909b7a
commit 2f199ce0c9

@ -5,7 +5,6 @@ import Select from '@material-ui/core/Select';
import MenuItem from '@material-ui/core/MenuItem'; import MenuItem from '@material-ui/core/MenuItem';
import FormControl from '@material-ui/core/FormControl'; import FormControl from '@material-ui/core/FormControl';
import InputLabel from '@material-ui/core/InputLabel'; import InputLabel from '@material-ui/core/InputLabel';
import { Theme, withTheme } from '@material-ui/core/styles';
import IconDelete from '@material-ui/icons/Delete'; import IconDelete from '@material-ui/icons/Delete';
import IconCancel from '@material-ui/icons/Clear'; import IconCancel from '@material-ui/icons/Clear';
import IconSave from '@material-ui/icons/Save'; import IconSave from '@material-ui/icons/Save';
@ -17,7 +16,6 @@ import ConfirmationDialog from '../widgets/ConfirmationDialog';
import * as EteSync from 'etesync'; import * as EteSync from 'etesync';
import { SyncInfo } from '../SyncGate'; import { SyncInfo } from '../SyncGate';
import { handleInputChange } from '../helpers';
interface PropsType { interface PropsType {
syncInfo: SyncInfo; syncInfo: SyncInfo;
@ -27,160 +25,140 @@ interface PropsType {
onCancel: () => void; onCancel: () => void;
} }
interface PropsTypeInner extends PropsType { export default function JournalEdit(props: PropsType) {
theme: Theme; const [showDeleteDialog, setShowDeleteDialog] = React.useState(false);
} const [info, setInfo] = React.useState<EteSync.CollectionInfo>();
class JournalEdit extends React.PureComponent<PropsTypeInner> { React.useEffect(() => {
public state = { if (props.item !== undefined) {
info: { setInfo(props.item);
uid: '', } else {
type: '', setInfo({
displayName: '', uid: EteSync.genUid(),
description: '', type: 'ADDRESS_BOOK',
} as EteSync.CollectionInfo, displayName: '',
description: '',
} as EteSync.CollectionInfo);
}
}, [props.item]);
showDeleteDialog: false, if (info === undefined) {
}; return <React.Fragment />;
}
private handleInputChange: any; function onSubmit(e: React.FormEvent<any>) {
e.preventDefault();
constructor(props: PropsTypeInner) { const { onSave } = props;
super(props); const item = new EteSync.CollectionInfo(info);
this.handleInputChange = handleInputChange(this, 'info');
this.onSubmit = this.onSubmit.bind(this);
this.onDeleteRequest = this.onDeleteRequest.bind(this);
if (this.props.item !== undefined) { onSave(item, props.item);
const collection = this.props.item; }
this.state.info = { ...collection }; function onDeleteRequest() {
} else { setShowDeleteDialog(true);
this.state.info.uid = EteSync.genUid();
this.state.info.type = 'ADDRESS_BOOK';
}
} }
public render() { const { item, onDelete, onCancel } = props;
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 (
<>
<AppBarOverride title={pageTitle} />
<Container style={{ maxWidth: '30rem' }}>
<form onSubmit={this.onSubmit}>
<FormControl disabled={this.props.item !== undefined} style={styles.fullWidth}>
<InputLabel>
Collection type
</InputLabel>
<Select
name="type"
required
value={this.state.info.type}
onChange={this.handleInputChange}
>
{Object.keys(journalTypes).map((x) => (
<MenuItem key={x} value={x}>{journalTypes[x]}</MenuItem>
))}
</Select>
</FormControl>
<TextField
name="displayName"
required
label="Display name of this collection"
value={this.state.info.displayName}
onChange={this.handleInputChange}
style={styles.fullWidth}
margin="normal"
/>
<TextField
name="description"
label="Description (optional)"
value={this.state.info.description}
onChange={this.handleInputChange}
style={styles.fullWidth}
margin="normal"
/>
<div style={styles.submit}>
<Button
variant="contained"
onClick={onCancel}
>
<IconCancel style={{ marginRight: 8 }} />
Cancel
</Button>
{this.props.item && const pageTitle = (item !== undefined) ? item.displayName : 'New Journal';
<Button
variant="contained"
style={{ marginLeft: 15, backgroundColor: colors.red[500], color: 'white' }}
onClick={this.onDeleteRequest}
>
<IconDelete style={{ marginRight: 8 }} />
Delete
</Button>
}
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 (
<>
<AppBarOverride title={pageTitle} />
<Container style={{ maxWidth: '30rem' }}>
<form onSubmit={onSubmit}>
<FormControl disabled={props.item !== undefined} style={styles.fullWidth}>
<InputLabel>
Collection type
</InputLabel>
<Select
name="type"
required
value={info.type}
onChange={(event: React.ChangeEvent<{ value: string }>) => setInfo({ ...info, type: event.target.value })}
>
{Object.keys(journalTypes).map((x) => (
<MenuItem key={x} value={x}>{journalTypes[x]}</MenuItem>
))}
</Select>
</FormControl>
<TextField
name="displayName"
required
label="Display name of this collection"
value={info.displayName}
onChange={(event: React.ChangeEvent<{ value: string }>) => setInfo({ ...info, displayName: event.target.value })}
style={styles.fullWidth}
margin="normal"
/>
<TextField
name="description"
label="Description (optional)"
value={info.description}
onChange={(event: React.ChangeEvent<{ value: string }>) => setInfo({ ...info, description: event.target.value })}
style={styles.fullWidth}
margin="normal"
/>
<div style={styles.submit}>
<Button
variant="contained"
onClick={onCancel}
>
<IconCancel style={{ marginRight: 8 }} />
Cancel
</Button>
{props.item &&
<Button <Button
type="submit"
variant="contained" variant="contained"
color="secondary" style={{ marginLeft: 15, backgroundColor: colors.red[500], color: 'white' }}
style={{ marginLeft: 15 }} onClick={onDeleteRequest}
> >
<IconSave style={{ marginRight: 8 }} /> <IconDelete style={{ marginRight: 8 }} />
Save Delete
</Button> </Button>
</div> }
</form>
</Container> <Button
<ConfirmationDialog type="submit"
title="Delete Confirmation" variant="contained"
labelOk="Delete" color="secondary"
open={this.state.showDeleteDialog} style={{ marginLeft: 15 }}
onOk={() => onDelete(this.props.item!)} >
onCancel={() => this.setState({ showDeleteDialog: false })} <IconSave style={{ marginRight: 8 }} />
> Save
Are you sure you would like to delete this journal? </Button>
</ConfirmationDialog> </div>
</> </form>
); </Container>
} <ConfirmationDialog
title="Delete Confirmation"
private onSubmit(e: React.FormEvent<any>) { labelOk="Delete"
e.preventDefault(); open={showDeleteDialog}
onOk={() => onDelete(props.item!)}
const { onSave } = this.props; onCancel={() => setShowDeleteDialog(false)}
const item = new EteSync.CollectionInfo(this.state.info); >
Are you sure you would like to delete this journal?
onSave(item, this.props.item); </ConfirmationDialog>
} </>
);
private onDeleteRequest() {
this.setState({
showDeleteDialog: true,
});
}
} }
export default withTheme(JournalEdit);

Loading…
Cancel
Save