diff --git a/src/customizations/volto/components/manage/Widgets/RecurrenceWidget/EndField.jsx b/src/customizations/volto/components/manage/Widgets/RecurrenceWidget/EndField.jsx new file mode 100644 index 000000000..d5954d97b --- /dev/null +++ b/src/customizations/volto/components/manage/Widgets/RecurrenceWidget/EndField.jsx @@ -0,0 +1,133 @@ +/** + * EndField component. + * @module components/manage/Widgets/RecurrenceWidget/EndField + * + * + * * CUSTOMIZATIONS: + * - added customization to have this changes https://github.com/plone/volto/pull/5555/files + */ + +import React from 'react'; +import PropTypes from 'prop-types'; +import { defineMessages, injectIntl } from 'react-intl'; +import { Form, Grid, Input, Radio } from 'semantic-ui-react'; +import DatetimeWidget from '../DatetimeWidget'; + +const messages = defineMessages({ + recurrenceEnds: { id: 'Recurrence ends', defaultMessage: 'Ends' }, + recurrenceEndsCount: { id: 'Recurrence ends after', defaultMessage: 'after' }, + recurrenceEndsUntil: { id: 'Recurrence ends on', defaultMessage: 'on' }, + occurrences: { id: 'Occurences', defaultMessage: 'occurrence(s)' }, +}); +/** + * EndField component class. + * @function EndField + * @returns {string} Markup of the component. + */ +const EndField = ({ value, count, until, onChange, intl }) => { + return ( + + + + +
+ +
+
+ + + + onChange('recurrenceEnds', value)} + /> + + + {intl.formatMessage(messages.recurrenceEndsCount)} + + + { + onChange( + target.id, + target.value === '' ? undefined : target.value, + ); + }} + /> + + + {intl.formatMessage(messages.occurrences)} + + + + + onChange('recurrenceEnds', value)} + /> + + + + { + onChange(id, value === '' ? undefined : value); + }} + /> + + + +
+
+
+ ); +}; + +/** + * Property types. + * @property {Object} propTypes Property types. + * @static + */ +EndField.propTypes = { + value: PropTypes.string, + count: PropTypes.any, + until: PropTypes.any, + onChange: PropTypes.func, +}; + +/** + * Default properties. + * @property {Object} defaultProps Default properties. + * @static + */ +EndField.defaultProps = { + value: null, + count: null, + until: null, + onChange: null, +}; + +export default injectIntl(EndField); diff --git a/src/customizations/volto/components/manage/Widgets/RecurrenceWidget/RecurrenceWidget.jsx b/src/customizations/volto/components/manage/Widgets/RecurrenceWidget/RecurrenceWidget.jsx index 2fc15287c..ba32fae7a 100644 --- a/src/customizations/volto/components/manage/Widgets/RecurrenceWidget/RecurrenceWidget.jsx +++ b/src/customizations/volto/components/manage/Widgets/RecurrenceWidget/RecurrenceWidget.jsx @@ -4,6 +4,7 @@ * CUSTOMIZATIONS: * - add date field open calendar on top * - fix all imports and rrulei18n use + * - added customization to have this changes https://github.com/plone/volto/pull/5555/files */ import React, { Component } from 'react'; @@ -216,26 +217,41 @@ class RecurrenceWidget extends Component { componentDidUpdate(prevProps) { if (this.props.value) { - if (prevProps.formData?.start !== this.props.formData?.start) { - let start = this.getUTCDate(this.props.formData?.start) - .startOf('day') - .toDate(); - - this.setState((prevState) => { - let rruleSet = prevState.rruleSet; - - rruleSet = this.updateRruleSet( - rruleSet, - prevState.formValues, - 'dtstart', - start, - ); - - return { - ...prevState, - rruleSet, - }; - }); + const changedStart = + prevProps.formData?.start !== this.props.formData?.start; + const changedEnd = prevProps.formData?.end !== this.props.formData?.end; + + if (changedStart || changedEnd) { + let start = this.getUTCDate(this.props.formData?.start).toDate(); + // let end = this.getUTCDate(this.props.formData?.end).toDate(); + + let changeFormValues = {}; + if (changedEnd) { + changeFormValues.until = this.getUTCDate( + this.props.formData?.end, + ).toDate(); + } + this.setState( + (prevState) => { + let rruleSet = prevState.rruleSet; + + rruleSet = this.updateRruleSet( + rruleSet, + { ...prevState.formValues, ...changeFormValues }, + 'dtstart', + start, + ); + + return { + ...prevState, + rruleSet, + }; + }, + () => { + //then, after set state, set recurrence rrule value + this.saveRrule(); + }, + ); } } } @@ -247,7 +263,7 @@ class RecurrenceWidget extends Component { setRecurrenceStartEnd = () => { const start = this.props.formData?.start; - let _start = this.getUTCDate(start).startOf('day').toDate(); + const _start = new Date(start); //The date is already in utc from plone, so this is not necessary: this.getUTCDate(start).startOf('day').toDate(); this.setState((prevState) => { let rruleSet = prevState.rruleSet; @@ -336,7 +352,7 @@ class RecurrenceWidget extends Component { case 'until': if (value != null) { formValues['recurrenceEnds'] = option; - formValues[option] = toISOString(value); + formValues[option] = value; } break; case 'byweekday': @@ -419,7 +435,24 @@ class RecurrenceWidget extends Component { } break; case 'until': - value = value ? moment(new Date(value)).utc().toDate() : null; + let mDate = null; + if (value) { + mDate = this.moment(new Date(value)); + if (typeof value === 'string') { + mDate = this.moment(new Date(value)); + } else { + //object-->Date() + mDate = this.moment(value); + } + + if (this.props.formData.end) { + //set time from formData.end + const mEnd = this.moment(new Date(this.props.formData.end)); + mDate.set('hour', mEnd.get('hour')); + mDate.set('minute', mEnd.get('minute')); + } + } + value = value ? mDate.toDate() : null; break; default: break; @@ -443,8 +476,8 @@ class RecurrenceWidget extends Component { field === 'dtstart' ? value : rruleSet.dtstart() - ? rruleSet.dtstart() - : moment().utc().toDate(); + ? rruleSet.dtstart() + : new Date(); var exdates = field === 'exdates' ? value : Object.assign([], rruleSet.exdates()); @@ -465,12 +498,12 @@ class RecurrenceWidget extends Component { getDefaultUntil = (freq) => { var end = this.props.formData?.end - ? toISOString(this.getUTCDate(this.props.formData.end).toDate()) + ? moment(new Date(this.props.formData.end)) : null; - var tomorrow = toISOString(moment().add(1, 'days').utc().toDate()); - var nextWeek = toISOString(moment().add(7, 'days').utc().toDate()); - var nextMonth = toISOString(moment().add(1, 'months').utc().toDate()); - var nextYear = toISOString(moment().add(1, 'years').utc().toDate()); + var tomorrow = moment().add(1, 'days'); + var nextWeek = moment().add(7, 'days'); + var nextMonth = moment().add(1, 'months'); + var nextYear = moment().add(1, 'years'); var until = end; switch (freq) { @@ -495,6 +528,19 @@ class RecurrenceWidget extends Component { default: break; } + if (this.props.formData.end) { + //set default end time + until.set('hour', end.get('hour')); + until.set('minute', end.get('minute')); + } + + until = new Date( + until.get('year'), + until.get('month'), + until.get('date'), + until.get('hour'), + until.get('minute'), + ); return until; }; @@ -708,9 +754,13 @@ class RecurrenceWidget extends Component { } }; - save = () => { + saveRrule = () => { var value = this.state.rruleSet.toString(); this.props.onChange(this.props.id, value); + }; + + save = () => { + this.saveRrule(); this.close(); };