import * as React from 'react'; import FormGroup from '@material-ui/core/FormGroup'; import FormControlLabel from '@material-ui/core/FormControlLabel'; import Switch from '@material-ui/core/Switch'; import Button from '@material-ui/core/Button'; import TextField from '@material-ui/core/TextField'; 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 * as colors from '@material-ui/core/colors'; import IconDelete from '@material-ui/icons/Delete'; import IconCancel from '@material-ui/icons/Clear'; import IconSave from '@material-ui/icons/Save'; import DateTimePicker from '../widgets/DateTimePicker'; import ConfirmationDialog from '../widgets/ConfirmationDialog'; import { Location } from 'history'; import { withRouter } from 'react-router'; import * as uuid from 'uuid'; import * as ICAL from 'ical.js'; import * as EteSync from '../api/EteSync'; import { EventType } from '../pim-types'; interface PropsType { collections: EteSync.CollectionInfo[]; initialCollection?: string; item?: EventType; onSave: (event: EventType, journalUid: string, originalEvent?: EventType) => void; onDelete: (event: EventType, journalUid: string) => void; onCancel: () => void; location: Location; } class EventEdit extends React.PureComponent { public state: { uid: string, title: string; allDay: boolean; start?: Date; end?: Date; location: string; description: string; journalUid: string; error?: string; showDeleteDialog: boolean; }; constructor(props: any) { super(props); this.state = { uid: '', title: '', allDay: false, location: '', description: '', journalUid: '', showDeleteDialog: false, }; const locState = this.props.location.state; if (locState) { // FIXME: Hack to determine if all day. Should be passed as a proper state. this.state.allDay = (locState.start && (locState.start.getHours() === 0) && (locState.start.getMinutes() === 0) && (locState.start.getHours() === locState.end.getHours()) && (locState.start.getMinutes() === locState.end.getMinutes())); this.state.start = (locState.start) ? locState.start : undefined; this.state.end = (locState.end) ? locState.end : undefined; } if (this.props.item !== undefined) { const event = this.props.item; const allDay = event.startDate.isDate; const endDate = event.endDate.clone(); if (allDay) { endDate.adjust(-1, 0, 0, 0); } this.state.uid = event.uid; this.state.title = event.title ? event.title : ''; this.state.allDay = allDay; this.state.start = event.startDate.toJSDate(); this.state.end = endDate.toJSDate(); this.state.location = event.location ? event.location : ''; this.state.description = event.description ? event.description : ''; } else { this.state.uid = uuid.v4(); } if (props.initialCollection) { this.state.journalUid = props.initialCollection; } else if (props.collections[0]) { this.state.journalUid = props.collections[0].uid; } this.onSubmit = this.onSubmit.bind(this); this.handleChange = this.handleChange.bind(this); this.handleInputChange = this.handleInputChange.bind(this); this.toggleAllDay = this.toggleAllDay.bind(this); this.onDeleteRequest = this.onDeleteRequest.bind(this); } public componentWillReceiveProps(nextProps: any) { if ((this.props.collections !== nextProps.collections) || (this.props.initialCollection !== nextProps.initialCollection)) { if (nextProps.initialCollection) { this.state.journalUid = nextProps.initialCollection; } else if (nextProps.collections[0]) { this.state.journalUid = nextProps.collections[0].uid; } } } public handleChange(name: string, value: string) { this.setState({ [name]: value, }); } public handleInputChange(event: React.ChangeEvent) { const name = event.target.name; const value = event.target.value; this.handleChange(name, value); } public toggleAllDay() { this.setState({allDay: !this.state.allDay}); } public onSubmit(e: React.FormEvent) { e.preventDefault(); if ((!this.state.start) || (!this.state.end)) { this.setState({error: 'Both start and end time must be set!'}); return; } function fromDate(date: Date, allDay: boolean) { const ret = ICAL.Time.fromJSDate(date, false); if (!allDay) { return ret; } else { const data = ret.toJSON(); data.isDate = allDay; return ICAL.Time.fromData(data); } } const startDate = fromDate(this.state.start, this.state.allDay); const endDate = fromDate(this.state.end, this.state.allDay); if (this.state.allDay) { endDate.adjust(1, 0, 0, 0); } if (startDate.compare(endDate) >= 0) { this.setState({error: 'End time must be later than start time!'}); return; } const event = (this.props.item) ? this.props.item.clone() : new EventType() ; event.uid = this.state.uid; event.summary = this.state.title; event.startDate = startDate; event.endDate = endDate; event.location = this.state.location; event.description = this.state.description; event.component.updatePropertyWithValue('last-modified', ICAL.Time.now()); this.props.onSave(event, this.state.journalUid, this.props.item); } public onDeleteRequest() { this.setState({ showDeleteDialog: true, }); } public render() { const styles = { form: { }, fullWidth: { width: '100%', boxSizing: 'border-box' as any, marginTop: 16, }, submit: { marginTop: 40, marginBottom: 20, textAlign: 'right' as any, }, }; const recurring = this.props.item && this.props.item.isRecurring(); return (

{this.props.item ? 'Edit Event' : 'New Event'}

{recurring && (
IMPORTANT: This is a recurring event, for now, only editing the whole series (by editing the first instance) is supported.
)} {this.state.error && (
ERROR! {this.state.error}
)}
Saving to } label="All Day" />
this.setState({start: date})} />
this.setState({end: date})} />
{this.props.item && }
Not all types are supported at the moment. If you are editing a contact, the unsupported types will be copied as is.
this.props.onDelete(this.props.item!, this.props.initialCollection!)} onCancel={() => this.setState({showDeleteDialog: false})} > Are you sure you would like to delete this event?
); } } export default withRouter(EventEdit);