Event Edit: simplify code and make it persist the rrule

While at it, also fix an issue with byday needing to be string[] and not
number[]
master
Tom Hacohen 5 years ago
parent 3897d38fb0
commit 4b20a0db5f

@ -54,11 +54,11 @@ class EventEdit extends React.PureComponent<PropsType> {
start?: Date; start?: Date;
end?: Date; end?: Date;
timezone: string | null; timezone: string | null;
rrule?: RRuleOptions;
location: string; location: string;
description: string; description: string;
journalUid: string; journalUid: string;
rrule?: RRuleOptions;
error?: string; error?: string;
showDeleteDialog: boolean; showDeleteDialog: boolean;
}; };
@ -219,6 +219,9 @@ class EventEdit extends React.PureComponent<PropsType> {
event.endDate = event.endDate?.convertToZone(timezone); event.endDate = event.endDate?.convertToZone(timezone);
} }
} }
if (this.state.rrule) {
event.component.updatePropertyWithValue('rrule', new ICAL.Recur(this.state.rrule!));
}
event.component.updatePropertyWithValue('last-modified', ICAL.Time.now()); event.component.updatePropertyWithValue('last-modified', ICAL.Time.now());
@ -250,7 +253,6 @@ class EventEdit extends React.PureComponent<PropsType> {
const recurring = this.props.item && this.props.item.isRecurring(); const recurring = this.props.item && this.props.item.isRecurring();
const differentTimezone = this.state.timezone && (this.state.timezone !== getCurrentTimezone()) && timezoneLoadFromName(this.state.timezone); const differentTimezone = this.state.timezone && (this.state.timezone !== getCurrentTimezone()) && timezoneLoadFromName(this.state.timezone);
return ( return (
<> <>
<h2> <h2>

@ -7,24 +7,10 @@ interface PropsType {
onChange: (rrule: RRuleOptions) => void; onChange: (rrule: RRuleOptions) => void;
rrule: RRuleOptions; rrule: RRuleOptions;
} }
type Frequency = 'YEARLY' | 'MONTHLY' | 'WEEKLY' | 'DAILY' | 'HOURLY' | 'MINUTELY' | 'SECONDLY';
const disableComplex = true; const disableComplex = true;
export interface RRuleOptions {
freq: Frequency; export type RRuleOptions = ICAL.RecurData;
interval?: number;
wkst?: WeekDay;
until?: ICAL.Time;
count?: number;
bysecond?: number[];
byminute?: number[];
byhour?: number[];
byday?: number[];
bymonthday?: number[];
byyearday?: number[];
byweekno?: number[];
bymonth?: number[];
bysetpos?: number[];
}
enum Ends { enum Ends {
Never, Never,
Date, Date,
@ -57,34 +43,15 @@ enum WeekDay {
Fr, Fr,
Sa, Sa,
} }
const menuItemsEnds = [Ends.Never, Ends.Date, Ends.After].map((key) => { const menuItemsEnds = [Ends.Never, Ends.Date, Ends.After].map((key) => {
return ( return (
<MenuItem key={key} value={key}>{Ends[key]}</MenuItem> <MenuItem key={key} value={key}>{Ends[key]}</MenuItem>
); );
}); });
const weekdays = [ const weekdays: WeekDay[] = Array.from(Array(7)).map((_, i) => i + 1);
WeekDay.Su, const months: Months[] = Array.from(Array(12)).map((_, i) => i + 1);
WeekDay.Mo,
WeekDay.Tu,
WeekDay.We,
WeekDay.Th,
WeekDay.Fr,
WeekDay.Sa,
];
const months = [
Months.Jan,
Months.Feb,
Months.Mar,
Months.Apr,
Months.May,
Months.Jun,
Months.Jul,
Months.Aug,
Months.Sep,
Months.Oct,
Months.Nov,
Months.Dec,
];
const menuItemsFrequency = ['YEARLY', 'MONTHLY', 'WEEKLY', 'DAILY'].map((value) => { const menuItemsFrequency = ['YEARLY', 'MONTHLY', 'WEEKLY', 'DAILY'].map((value) => {
return ( return (
<MenuItem key={value} value={value}>{value.toLowerCase()}</MenuItem> <MenuItem key={value} value={value}>{value.toLowerCase()}</MenuItem>
@ -97,7 +64,7 @@ const menuItemMonths = months.map((month) => {
}); });
const menuItemsWeekDays = weekdays.map((day) => { const menuItemsWeekDays = weekdays.map((day) => {
return ( return (
<MenuItem key={day} value={day}>{WeekDay[day]}</MenuItem> <MenuItem key={day} value={WeekDay[day].toUpperCase()}>{WeekDay[day]}</MenuItem>
); );
}); });
const styles = { const styles = {
@ -145,7 +112,7 @@ export default function RRule(props: PropsType) {
value={options.freq} value={options.freq}
style={{ alignSelf: 'flex-end', marginLeft: 20 }} style={{ alignSelf: 'flex-end', marginLeft: 20 }}
onChange={(event: React.FormEvent<{ value: unknown }>) => { onChange={(event: React.FormEvent<{ value: unknown }>) => {
const freq = (event.target as HTMLSelectElement).value as Frequency; const freq = (event.target as HTMLSelectElement).value as ICAL.FrequencyValues;
updateRule({ freq: freq }); updateRule({ freq: freq });
}} }}
> >
@ -239,7 +206,7 @@ export default function RRule(props: PropsType) {
onChange={(event: React.ChangeEvent<{ value: unknown }>) => { onChange={(event: React.ChangeEvent<{ value: unknown }>) => {
const value = event.target.value as string[]; const value = event.target.value as string[];
if (value) { if (value) {
updateRule({ byday: value.map((day) => Number(day)) }); updateRule({ byday: value });
} }
}}> }}>
{menuItemsWeekDays} {menuItemsWeekDays}

Loading…
Cancel
Save