diff --git a/src/components/EventEdit.tsx b/src/components/EventEdit.tsx index 4709375..94cc579 100644 --- a/src/components/EventEdit.tsx +++ b/src/components/EventEdit.tsx @@ -20,6 +20,7 @@ import IconSave from '@material-ui/icons/Save'; import DateTimePicker from '../widgets/DateTimePicker'; import ConfirmationDialog from '../widgets/ConfirmationDialog'; +import TimezonePicker from '../widgets/TimezonePicker'; import { Location } from 'history'; import { withRouter } from 'react-router'; @@ -29,7 +30,9 @@ import * as ICAL from 'ical.js'; import * as EteSync from 'etesync'; -import { EventType } from '../pim-types'; +import { getCurrentTimezone } from '../helpers'; + +import { EventType, timezoneLoadFromName } from '../pim-types'; interface PropsType { collections: EteSync.CollectionInfo[]; @@ -48,6 +51,7 @@ class EventEdit extends React.PureComponent { allDay: boolean; start?: Date; end?: Date; + timezone: string | null; location: string; description: string; journalUid: string; @@ -64,6 +68,7 @@ class EventEdit extends React.PureComponent { allDay: false, location: '', description: '', + timezone: null, journalUid: '', showDeleteDialog: false, @@ -94,14 +99,17 @@ class EventEdit extends React.PureComponent { 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.start = event.startDate.convertToZone(ICAL.Timezone.localTimezone).toJSDate(); + this.state.end = endDate.convertToZone(ICAL.Timezone.localTimezone).toJSDate(); this.state.location = event.location ? event.location : ''; this.state.description = event.description ? event.description : ''; + this.state.timezone = event.timezone; } else { this.state.uid = uuid.v4(); } + this.state.timezone = this.state.timezone || getCurrentTimezone(); + if (props.initialCollection) { this.state.journalUid = props.initialCollection; } else if (props.collections[0]) { @@ -189,6 +197,13 @@ class EventEdit extends React.PureComponent { event.endDate = endDate; event.location = this.state.location; event.description = this.state.description; + if (this.state.timezone) { + const timezone = timezoneLoadFromName(this.state.timezone); + if (timezone) { + event.startDate = event.startDate?.convertToZone(timezone); + event.endDate = event.endDate?.convertToZone(timezone); + } + } event.component.updatePropertyWithValue('last-modified', ICAL.Time.now()); @@ -218,6 +233,7 @@ class EventEdit extends React.PureComponent { }; const recurring = this.props.item && this.props.item.isRecurring(); + const differentTimezone = this.state.timezone && (this.state.timezone !== getCurrentTimezone()) && timezoneLoadFromName(this.state.timezone); return ( @@ -267,6 +283,9 @@ class EventEdit extends React.PureComponent { value={this.state.start} onChange={(date?: Date) => this.setState({ start: date })} /> + {differentTimezone && this.state.start && ( + {ICAL.Time.fromJSDate(this.state.start, false).convertToZone(differentTimezone!).toJSDate().toString()} + )} @@ -277,6 +296,9 @@ class EventEdit extends React.PureComponent { value={this.state.end} onChange={(date?: Date) => this.setState({ end: date })} /> + {differentTimezone && this.state.end && ( + {ICAL.Time.fromJSDate(this.state.end, false).convertToZone(differentTimezone!).toJSDate().toString()} + )} @@ -293,6 +315,10 @@ class EventEdit extends React.PureComponent { /> + {(!this.state.allDay) && ( + this.setState({ timezone: zone })} /> + )} + { allDay: boolean; start?: Date; due?: Date; + timezone: string | null; location: string; description: string; journalUid: string; @@ -66,6 +70,7 @@ class TaskEdit extends React.PureComponent { allDay: false, location: '', description: '', + timezone: null, journalUid: '', showDeleteDialog: false, @@ -79,17 +84,20 @@ class TaskEdit extends React.PureComponent { this.state.status = event.status; if (event.startDate) { this.state.allDay = event.startDate.isDate; - this.state.start = event.startDate.toJSDate(); + this.state.start = event.startDate.convertToZone(ICAL.Timezone.localTimezone).toJSDate(); } if (event.dueDate) { - this.state.due = event.dueDate.toJSDate(); + this.state.due = event.dueDate.convertToZone(ICAL.Timezone.localTimezone).toJSDate(); } this.state.location = event.location ? event.location : ''; this.state.description = event.description ? event.description : ''; + this.state.timezone = event.timezone; } else { this.state.uid = uuid.v4(); } + this.state.timezone = this.state.timezone || getCurrentTimezone(); + if (props.initialCollection) { this.state.journalUid = props.initialCollection; } else if (props.collections[0]) { @@ -177,6 +185,14 @@ class TaskEdit extends React.PureComponent { event.dueDate = dueDate; event.location = this.state.location; event.description = this.state.description; + if (this.state.timezone) { + const timezone = timezoneLoadFromName(this.state.timezone); + if (timezone) { + event.startDate = event.startDate?.convertToZone(timezone); + event.dueDate = event.dueDate?.convertToZone(timezone); + event.completionDate = event.completionDate?.convertToZone(timezone); + } + } event.component.updatePropertyWithValue('last-modified', ICAL.Time.now()); @@ -206,6 +222,7 @@ class TaskEdit extends React.PureComponent { }; const recurring = this.props.item && this.props.item.isRecurring(); + const differentTimezone = this.state.timezone && (this.state.timezone !== getCurrentTimezone()) && timezoneLoadFromName(this.state.timezone); return ( @@ -271,6 +288,9 @@ class TaskEdit extends React.PureComponent { value={this.state.start} onChange={(date?: Date) => this.setState({ start: date })} /> + {differentTimezone && this.state.start && ( + {ICAL.Time.fromJSDate(this.state.start, false).convertToZone(differentTimezone!).toJSDate().toString()} + )} @@ -281,6 +301,9 @@ class TaskEdit extends React.PureComponent { value={this.state.due} onChange={(date?: Date) => this.setState({ due: date })} /> + {differentTimezone && this.state.due && ( + {ICAL.Time.fromJSDate(this.state.due, false).convertToZone(differentTimezone!).toJSDate().toString()} + )} @@ -297,6 +320,10 @@ class TaskEdit extends React.PureComponent { /> + {(!this.state.allDay) && ( + this.setState({ timezone: zone })} /> + )} +