@ -1,40 +1,16 @@
import * as React from 'react' ;
import * as React from 'react' ;
import { TextField , Select , MenuItem , Form Group, Form ControlLabel, Checkbox , InputLabel , FormControl } from '@material-ui/core' ;
import { TextField , Select , MenuItem , Form ControlLabel, InputLabel , FormControl } from '@material-ui/core' ;
import DateTimePicker from '../widgets/DateTimePicker' ;
import DateTimePicker from '../widgets/DateTimePicker' ;
import { isNumber } from 'util ';
import * as ICAL from 'ical.js ';
interface PropsType {
export type RRuleOptions = ICAL . RecurData ;
onChange : ( rrule : RRuleOptions ) = > void ;
rrule : RRuleOptions ;
}
export interface RRuleOptions {
freq : Frequency ;
interval : number ;
until? : Date ;
count? : number ;
byweekday? : Weekday [ ] ;
bymonthday? : number ;
byyearday? : number ;
byweekno? : number ;
bymonth? : Months ;
bysetpos? : number ;
wkst? : Weekday ;
bysecond? : number [ ] ;
byminute? : number [ ] ;
byday? : number [ ] ;
}
enum Frequency {
YEARLY ,
MONTHLY ,
WEEKLY ,
DAILY ,
}
enum Ends {
enum Ends {
Never ,
Never ,
Date ,
Date ,
After ,
After ,
}
}
enum Months {
enum Months {
Jan = 1 ,
Jan = 1 ,
Feb ,
Feb ,
@ -49,45 +25,64 @@ enum Months {
Nov ,
Nov ,
Dec ,
Dec ,
}
}
enum MonthRepeat {
enum MonthRepeat {
Bysetpos ,
Bysetpos ,
Bymonthday ,
Bymonthday ,
}
}
enum Weekday {
enum WeekDay {
Su = 1 ,
Mo ,
Mo ,
Tu ,
Tu ,
We ,
We ,
Th ,
Th ,
Fr ,
Fr ,
Sa ,
Sa ,
Su
}
}
const menuItemsMonths = Object . keys ( Months ) . filter ( ( key ) = > Number ( key ) ) . map ( ( key ) = > {
const disableComplex = true ;
const weekdays : WeekDay [ ] = Array . from ( Array ( 7 ) ) . map ( ( _ , i ) = > i + 1 ) ;
const months : Months [ ] = Array . from ( Array ( 12 ) ) . map ( ( _ , i ) = > i + 1 ) ;
const menuItemsEnds = [ Ends . Never , Ends . Date , Ends . After ] . map ( ( key ) = > {
return (
return (
< MenuItem key = { key } value = { key } > { Months [ key ] } < / MenuItem >
< MenuItem key = { key } value = { key } > { End s[ key ] } < / MenuItem >
) ;
) ;
} ) ;
} ) ;
const menuItemsEnds = [ Ends . Never , Ends . Date , Ends . After ] . map ( ( key ) = > {
const menuItems Frequency = [ 'YEARLY' , 'MONTHLY' , 'WEEKLY' , 'DAILY' ] . map ( ( value ) = > {
return (
return (
< MenuItem key = { key} value = { key } > { Ends [ key ] } < / MenuItem >
< MenuItem key = { value} value = { value } > { value . toLowerCase ( ) } < / MenuItem >
) ;
) ;
} ) ;
} ) ;
const weekdays = [ Weekday . Mo , Weekday . Tu , Weekday . We , Weekday . Th , Weekday . Fr , Weekday . Sa , Weekday . Su ] ;
const menuItemMonths = months . map ( ( month ) = > {
const menuItemsFrequency = [ Frequency . YEARLY , Frequency . MONTHLY , Frequency . WEEKLY , Frequency . DAILY ] . map ( ( value ) = > {
return (
return (
< MenuItem key = { value } value = { value } > { Frequency [ value ] } < / MenuItem >
< MenuItem key = { month } value = { month } > { Months [ month ] } < / MenuItem >
) ;
} ) ;
const menuItemsWeekDays = weekdays . map ( ( day ) = > {
return (
< MenuItem key = { day } value = { WeekDay [ day ] . toUpperCase ( ) } > { WeekDay [ day ] } < / MenuItem >
) ;
) ;
} ) ;
} ) ;
export default function RRuleEteSync ( props : PropsType ) {
const styles = {
const options = props . rrule ;
multiSelect : { minWidth : 120 , maxWidth : '100%' } ,
width : { width : 120 } ,
} ;
interface PropsType {
onChange : ( rrule : RRuleOptions ) = > void ;
rrule : RRuleOptions ;
}
export default function RRule ( props : PropsType ) {
const options = props . rrule ;
function updateRule ( newOptions : Partial < RRuleOptions > ) : void {
function updateRule ( newOptions : Partial < RRuleOptions > ) : void {
const updatedOptions = { . . . options , . . . newOptions } ;
const updatedOptions = { . . . options , . . . newOptions } ;
props . onChange ( updatedOptions ) ;
props . onChange ( updatedOptions ) ;
}
}
function getEnds ( ) : Ends {
function getEnds ( ) : Ends {
if ( options . until && ! options . count ) {
if ( options . until && ! options . count ) {
return Ends . Date ;
return Ends . Date ;
@ -97,63 +92,24 @@ export default function RRuleEteSync(props: PropsType) {
return Ends . Never ;
return Ends . Never ;
}
}
}
}
function handleCheckboxWeekday ( event : React.FormEvent < { value : unknown } > ) : void {
const checkbox = event . target as HTMLInputElement ;
const weekday = Number ( checkbox . value ) ;
let byweekdayArray = options . byweekday as Weekday [ ] ;
let byweekday ;
if ( ! checkbox . checked && byweekdayArray ) {
byweekday = byweekdayArray . filter ( ( day ) = > day !== weekday ) ;
} else if ( byweekdayArray ) {
byweekdayArray = byweekdayArray . filter ( ( day ) = > day !== weekday ) ;
byweekday = [ . . . byweekdayArray , weekday ] ;
} else {
byweekday = [ weekday ] ;
}
updateRule ( { byweekday : byweekday } ) ;
}
function isWeekdayChecked ( day : number ) : boolean {
const weekdayArray = options . byweekday ;
if ( weekdayArray ) {
return isNumber ( weekdayArray . find ( ( value ) = > Weekday [ value ] === Weekday [ day ] ) ) ;
} else {
return false ;
}
}
const checkboxWeekDays = weekdays . map ( ( _ , index ) = > {
return (
< FormControlLabel
control = {
< Checkbox
key = { index }
value = { index }
checked = { isWeekdayChecked ( index ) }
onChange = { handleCheckboxWeekday }
/ > }
key = { index }
label = { Weekday [ index ] } / >
) ;
} ) ;
return (
return (
< >
< >
< div style = { { display : 'flex' } } >
< div style = { { display : 'flex' } } >
< FormControlLabel
< FormControlLabel
style = { { marginRight : 0 } }
value = { options . freq }
value = { options . freq }
label = "Repeat every : "
label = "Repeat every"
labelPlacement = "start"
labelPlacement = "start"
control = { < TextField
control = { < TextField
style = { { marginLeft : '0.5em' , width : '4em' } }
type = "number"
type = "number"
inputProps = { { min : 1 , max : 1000 } }
inputProps = { { min : 1 , max : 1000 } }
value = { options . interval }
value = { options . interval ? ? 1 }
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
event . preventDefault ( ) ;
event . preventDefault ( ) ;
const inputNode = event . currentTarget as HTMLInputElement ;
const inputNode = event . currentTarget as HTMLInputElement ;
if ( inputNode . value === '' ) {
if ( inputNode . value === '' ) {
updateRule ( { interval : undefined } ) ;
updateRule ( { interval : 1 } ) ;
} else if ( inputNode . valueAsNumber ) {
} else if ( inputNode . valueAsNumber ) {
updateRule ( { interval : inputNode.valueAsNumber } ) ;
updateRule ( { interval : inputNode.valueAsNumber } ) ;
}
}
@ -162,70 +118,118 @@ export default function RRuleEteSync(props: PropsType) {
/ >
/ >
< Select
< Select
value = { options . freq }
value = { options . freq }
style = { { alignSelf: 'flex-end' , marginLeft : 20 } }
style = { { marginLeft: '0.5em' } }
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
const freq = Number ( ( event . target as HTMLSelectElement ) . value ) ;
const freq = ( event . target as HTMLSelectElement ) . value as ICAL . FrequencyValues ;
const updatedOptions = {
updateRule ( { freq : freq } ) ;
freq : freq ,
bysetpos : undefined ,
bymonthday : freq === Frequency . MONTHLY || Frequency . YEARLY === freq ? 1 : undefined ,
byweekday : undefined ,
bymonth : freq === Frequency . YEARLY ? Months.Jan : undefined ,
} ;
updateRule ( updatedOptions ) ;
} }
} }
>
>
{ menuItemsFrequency }
{ menuItemsFrequency }
< / Select >
< / Select >
< / div >
< / div >
< div style = { { display : 'flex' } } >
{ ! disableComplex && (
< div style = { { display : 'flex' } } >
{ ( options . freq === Frequency . MONTHLY ) &&
{ ( options . freq === 'MONTHLY' ) &&
< Select
< Select value = { options . bysetpos ? MonthRepeat.Bysetpos : MonthRepeat.Bymonthday }
value = { options . bysetpos ? MonthRepeat.Bysetpos : MonthRepeat.Bymonthday }
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
const value = Number ( ( event . target as HTMLInputElement ) . value ) ;
const value = Number ( ( event . target as HTMLInputElement ) . value ) ;
if ( value === MonthRepeat . Bymonthday ) {
if ( value === MonthRepeat . Bymonthday ) {
updateRule ( { bymonthday : [ 1 ] , bysetpos : undefined , bymonth : [ Months . Jan ] } ) ;
updateRule ( { bymonthday : 1 , bysetpos : undefined , bymonth : Months.Jan } ) ;
} else if ( value === MonthRepeat . Bysetpos ) {
} else if ( value === MonthRepeat . Bysetpos ) {
updateRule ( { bysetpos : [ 1 ] , bymonthday : undefined , bymonth : undefined } ) ;
updateRule ( { bysetpos : 1 , bymonthday : undefined , bymonth : undefined } ) ;
}
}
} } >
} }
< MenuItem value = { MonthRepeat . Bymonthday } > On < / MenuItem >
>
< MenuItem value = { MonthRepeat . Bysetpos } > On the < / MenuItem >
< MenuItem value = { MonthRepeat . Bymonthday } > On < / MenuItem >
< / Select >
< MenuItem value = { MonthRepeat . Bysetpos } > On the < / MenuItem >
}
< / Select >
{ options . bysetpos &&
}
< Select value = { options . bysetpos [ 0 ] }
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
updateRule ( { bysetpos : [ Number ( ( event . target as HTMLInputElement ) . value ) ] } ) ;
{ options . bysetpos &&
} } >
< Select
< MenuItem value = { 1 } > First < / MenuItem >
value = { options . bysetpos }
< MenuItem value = { 2 } > Second < / MenuItem >
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
< MenuItem value = { 3 } > Third < / MenuItem >
updateRule ( { bysetpos : Number ( ( event . target as HTMLInputElement ) . value ) } ) ;
< MenuItem value = { 4 } > Fourth < / MenuItem >
} } >
< MenuItem value = { - 1 } > Last < / MenuItem >
< MenuItem value = { 1 } > First < / MenuItem >
< / Select >
< MenuItem value = { 2 } > Second < / MenuItem >
}
< MenuItem value = { 3 } > Third < / MenuItem >
< / div >
< MenuItem value = { 4 } > Fourth < / MenuItem >
) }
< MenuItem value = { - 1 } > Last < / MenuItem >
< FormControl >
< / Select >
< InputLabel > Ends < / InputLabel >
}
< Select
{ ( options . freq === Frequency . YEARLY && options . bymonth ) &&
value = { getEnds ( ) }
< Select
style = { styles . width }
value = { options . bymonth }
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
const value = Number ( ( event . target as HTMLSelectElement ) . value ) ;
updateRule ( { bymonth : Number ( ( event . target as HTMLInputElement ) . value ) } ) ;
let updateOptions ;
} } >
if ( value === Ends . Date ) {
{ menuItemsMonths }
updateOptions = { count : undefined , until : ICAL.Time.now ( ) } ;
< / Select >
} else if ( value === Ends . After ) {
updateOptions = { until : undefined , count : 1 } ;
} else {
updateOptions = { count : undefined , until : undefined } ;
}
updateRule ( updateOptions ) ;
} } >
{ menuItemsEnds }
< / Select >
< / FormControl >
{ options . until &&
< DateTimePicker
dateOnly
value = { options . until ? . toJSDate ( ) || undefined }
placeholder = "Ends"
onChange = { ( date? : Date ) = > {
const value = date ? date : null ;
updateRule ( { until : ICAL.Time.fromJSDate ( value , true ) } ) ;
} }
/ >
}
{ options . count &&
< TextField
type = "number"
value = { options . count }
label = "Count"
style = { { width : 60 } }
inputProps = { { min : 1 , step : 1 } }
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
event . preventDefault ( ) ;
const inputNode = event . currentTarget as HTMLInputElement ;
if ( inputNode . value === '' ) {
updateRule ( { count : 1 } ) ;
} else if ( inputNode . valueAsNumber ) {
updateRule ( { count : inputNode.valueAsNumber } ) ;
}
} }
/ >
}
< div >
{ ( options . freq && options . freq !== 'DAILY' ) &&
< div >
< FormControl >
< InputLabel > Weekdays < / InputLabel >
< Select
value = { options . byday ? options . byday : [ ] }
multiple
style = { styles . multiSelect }
onChange = { ( event : React.ChangeEvent < { value : unknown } > ) = > {
const value = event . target . value as string [ ] ;
updateRule ( { byday : value } ) ;
} } >
{ menuItemsWeekDays }
< / Select >
< / FormControl >
< / div >
}
}
{ options . bymonthday &&
{ options . freq === 'MONTHLY' &&
< TextField
< TextField
type = "number"
type = "number"
value = { options . bymonthday }
value = { options . bymonthday ? options . bymonthday [ 0 ] : undefined }
label = "Month day"
label = "Month day"
style = { { width : 100 } }
style = { styles . width }
inputProps = { { min : 1 , step : 1 , max : 31 } }
inputProps = { { min : 1 , step : 1 , max : 31 } }
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
event . preventDefault ( ) ;
event . preventDefault ( ) ;
@ -234,61 +238,27 @@ export default function RRuleEteSync(props: PropsType) {
if ( value === '' ) {
if ( value === '' ) {
updateRule ( { bymonthday : undefined } ) ;
updateRule ( { bymonthday : undefined } ) ;
} else if ( numberValue < 32 && numberValue > 0 ) {
} else if ( numberValue < 32 && numberValue > 0 ) {
updateRule ( { bymonthday : numberValue } ) ;
updateRule ( { bymonthday : [ numberValue ] } ) ;
}
}
} }
} }
/ >
/ >
}
}
< / div >
{ options . freq === 'YEARLY' &&
< div >
< div >
{ options . freq !== Frequency . DAILY &&
< FormControl >
< FormGroup row > { checkboxWeekDays } < / FormGroup >
< InputLabel > Months < / InputLabel >
}
< Select
< FormControl >
style = { styles . multiSelect }
< InputLabel > Ends < / InputLabel >
value = { options . bymonth ? options . bymonth : [ ] }
< Select
multiple
value = { getEnds ( ) }
onChange = { ( event : React.ChangeEvent < { value : unknown } > ) = > {
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
const value = event . target . value as string [ ] ;
const value = Number ( ( event . target as HTMLSelectElement ) . value ) ;
updateRule ( { bymonth : value.map ( ( month ) = > Number ( month ) ) } ) ;
let updateOptions ;
} } >
if ( value === Ends . Date ) {
{ menuItemMonths }
updateOptions = { count : undefined , until : new Date ( ) } ;
< / Select >
} else if ( value === Ends . After ) {
< / FormControl >
updateOptions = { until : undefined , count : 1 } ;
< / div >
} else {
updateOptions = { count : undefined , until : undefined } ;
}
updateRule ( updateOptions ) ;
} } >
{ menuItemsEnds }
< / Select >
< / FormControl >
{ options . until &&
< DateTimePicker
dateOnly
value = { options . until || undefined }
placeholder = "Ends"
onChange = { ( date? : Date ) = > updateRule ( { until : date } ) }
/ >
}
{ options . count &&
< TextField
type = "number"
value = { options . count }
label = "Count"
style = { { width : 60 } }
inputProps = { { min : 1 , step : 1 } }
onChange = { ( event : React.FormEvent < { value : unknown } > ) = > {
event . preventDefault ( ) ;
const inputNode = event . currentTarget as HTMLInputElement ;
if ( inputNode . value === '' ) {
updateRule ( { count : 1 } ) ;
} else if ( inputNode . valueAsNumber ) {
updateRule ( { count : inputNode.valueAsNumber } ) ;
}
} }
/ >
}
}
< / div >
< / div >
< / >
< / >