← Back
Editing: sagas.js
/* eslint-disable max-len */ /** * External Dependencies */ import { select as wpSelect, dispatch as wpDispatch } from '@wordpress/data'; import { put, call, all, select, takeEvery, take, fork, cancel } from 'redux-saga/effects'; /** * Internal dependencies */ import * as types from './types'; import * as actions from './actions'; import * as selectors from './selectors'; import { MOVE_TICKET_SUCCESS } from '../../shared/move/types'; import * as moveSelectors from '../../shared/move/selectors'; import { isTribeEventPostType, createDates } from '../../shared/sagas'; import { getRsvpDateFormat } from './utils/rsvp-date-format'; import { moment as momentUtil, time as timeUtil } from '@moderntribe/common/utils'; /** * Creates dates like the shared createDates, but formats the input string with * the RSVP display format (WordPress date format) instead of TEC's compact * datepicker format. * * @param {string|Date} date The source date. * @return {Object} { moment, date, dateInput, time, timeInput } */ export function* createRSVPDates( date ) { const { moment, date: databaseDate, time, timeInput } = yield call( createDates, date ); const dateInput = yield call( momentUtil.toDate, moment, getRsvpDateFormat() ); return { moment, date: databaseDate, dateInput, time, timeInput }; } // // ─── RSVP DETAILS ─────────────────────────────────────────────────────────────── // /** * Set details for current RSVP * * @yield * @param {Object} action redux action */ export function* setRSVPDetails( action ) { const { title, description, capacity, notGoingResponses, showAttendees, startDate, startDateInput, startDateMoment, startTime, endDate, endDateInput, endDateMoment, endTime, startTimeInput, endTimeInput, } = action.payload; yield all( [ put( actions.setRSVPTitle( title ) ), put( actions.setRSVPDescription( description ) ), put( actions.setRSVPCapacity( capacity ) ), put( actions.setRSVPNotGoingResponses( notGoingResponses ) ), put( actions.setRSVPShowAttendees( showAttendees ) ), put( actions.setRSVPStartDate( startDate ) ), put( actions.setRSVPStartDateInput( startDateInput ) ), put( actions.setRSVPStartDateMoment( startDateMoment ) ), put( actions.setRSVPStartTime( startTime ) ), put( actions.setRSVPEndDate( endDate ) ), put( actions.setRSVPEndDateInput( endDateInput ) ), put( actions.setRSVPEndDateMoment( endDateMoment ) ), put( actions.setRSVPEndTime( endTime ) ), put( actions.setRSVPStartTimeInput( startTimeInput ) ), put( actions.setRSVPEndTimeInput( endTimeInput ) ), ] ); } /** * Set details for current temp RSVP * * @yield * @param {Object} action redux action */ export function* setRSVPTempDetails( action ) { const { tempTitle, tempDescription, tempCapacity, tempNotGoingResponses, tempShowAttendees, tempStartDate, tempStartDateInput, tempStartDateMoment, tempStartTime, tempEndDate, tempEndDateInput, tempEndDateMoment, tempEndTime, tempStartTimeInput, tempEndTimeInput, } = action.payload; yield all( [ put( actions.setRSVPTempTitle( tempTitle ) ), put( actions.setRSVPTempDescription( tempDescription ) ), put( actions.setRSVPTempCapacity( tempCapacity ) ), put( actions.setRSVPTempNotGoingResponses( tempNotGoingResponses ) ), put( actions.setRSVPTempShowAttendees( tempShowAttendees ) ), put( actions.setRSVPTempStartDate( tempStartDate ) ), put( actions.setRSVPTempStartDateInput( tempStartDateInput ) ), put( actions.setRSVPTempStartDateMoment( tempStartDateMoment ) ), put( actions.setRSVPTempStartTime( tempStartTime ) ), put( actions.setRSVPTempEndDate( tempEndDate ) ), put( actions.setRSVPTempEndDateInput( tempEndDateInput ) ), put( actions.setRSVPTempEndDateMoment( tempEndDateMoment ) ), put( actions.setRSVPTempEndTime( tempEndTime ) ), put( actions.setRSVPTempStartTimeInput( tempStartTimeInput ) ), put( actions.setRSVPTempEndTimeInput( tempEndTimeInput ) ), ] ); } // // ─── INITIALIZE ───────────────────────────────────────────────────────────────── // /** * Initializes RSVP that has not been created * * Optional functionality requires TEC to be enabled and post type to be event. * @yield */ export function* initializeRSVP() { const publishDate = yield call( [ wpSelect( 'core/editor' ), 'getEditedPostAttribute' ], 'date' ); const { moment: startMoment, date: startDate, dateInput: startDateInput, time: startTime, timeInput: startTimeInput, } = yield call( createRSVPDates, publishDate ); yield all( [ put( actions.setRSVPStartDate( startDate ) ), put( actions.setRSVPStartDateInput( startDateInput ) ), put( actions.setRSVPStartDateMoment( startMoment ) ), put( actions.setRSVPStartTime( startTime ) ), put( actions.setRSVPStartTimeInput( startTimeInput ) ), put( actions.setRSVPTempStartDate( startDate ) ), put( actions.setRSVPTempStartDateInput( startDateInput ) ), put( actions.setRSVPTempStartDateMoment( startMoment ) ), put( actions.setRSVPTempStartTime( startTime ) ), put( actions.setRSVPTempStartTimeInput( startTimeInput ) ), ] ); try { if ( yield call( isTribeEventPostType ) ) { // NOTE: This requires TEC to be installed, if not installed, do not set an end date const eventStart = yield select( window.tec.events.app.main.data.blocks.datetime.selectors.getStart ); // RSVP window should end when event starts... ideally const endData = yield call( createRSVPDates, eventStart ); // Only sync the RSVP end to the event start when the event starts after the // RSVP start time. Syncing to an earlier event start would invert the window // (end before start), which blocks RSVP persistence with a duration error. if ( endData.moment.isAfter( startMoment ) ) { const { moment: endMoment, date: endDate, dateInput: endDateInput, time: endTime, timeInput: endTimeInput, } = endData; yield all( [ put( actions.setRSVPEndDate( endDate ) ), put( actions.setRSVPEndDateInput( endDateInput ) ), put( actions.setRSVPEndDateMoment( endMoment ) ), put( actions.setRSVPEndTime( endTime ) ), put( actions.setRSVPEndTimeInput( endTimeInput ) ), put( actions.setRSVPTempEndDate( endDate ) ), put( actions.setRSVPTempEndDateInput( endDateInput ) ), put( actions.setRSVPTempEndDateMoment( endMoment ) ), put( actions.setRSVPTempEndTime( endTime ) ), put( actions.setRSVPTempEndTimeInput( endTimeInput ) ), ] ); } } } catch ( error ) { // ¯\_(ツ)_/¯ console.error( error ); } yield call( handleRSVPDurationError ); } /** * Will sync RSVP sale end to be the same as event start date and time, if field has not been manually edited * * Functionality requires TEC to be enabled. * @param {string} prevStartDate Previous start date before latest set date time changes * @yield */ export function* syncRSVPSaleEndWithEventStart( prevStartDate ) { try { const tempEndMoment = yield select( selectors.getRSVPTempEndDateMoment ); const endMoment = yield select( selectors.getRSVPEndDateMoment ); const { moment: prevEventStartMoment } = yield call( createDates, prevStartDate ); // NOTE: Mutation // Convert to use local timezone yield all( [ call( [ tempEndMoment, 'local' ] ), call( [ endMoment, 'local' ] ), call( [ prevEventStartMoment, 'local' ] ), ] ); // If initial end and current end are the same, the RSVP has not been modified const isNotManuallyEdited = yield call( [ tempEndMoment, 'isSame' ], endMoment, 'minute' ); const isSyncedToEventStart = yield call( [ tempEndMoment, 'isSame' ], prevEventStartMoment, 'minute' ); if ( isNotManuallyEdited && isSyncedToEventStart ) { const eventStart = yield select( window.tec.events.app.main.data.blocks.datetime.selectors.getStart ); const { moment: endDateMoment, date: endDate, dateInput: endDateInput, time: endTime, timeInput: endTimeInput, } = yield call( createRSVPDates, eventStart ); yield all( [ put( actions.setRSVPTempEndDate( endDate ) ), put( actions.setRSVPTempEndDateInput( endDateInput ) ), put( actions.setRSVPTempEndDateMoment( endDateMoment ) ), put( actions.setRSVPTempEndTime( endTime ) ), put( actions.setRSVPTempEndTimeInput( endTimeInput ) ), // Sync RSVP end items as well so as not to make state 'manually edited' put( actions.setRSVPEndDate( endDate ) ), put( actions.setRSVPEndDateInput( endDateInput ) ), put( actions.setRSVPEndDateMoment( endDateMoment ) ), put( actions.setRSVPEndTime( endTime ) ), put( actions.setRSVPEndTimeInput( endTimeInput ) ), // Trigger UI button put( actions.setRSVPHasChanges( true ) ), // Handle RSVP duration error call( handleRSVPDurationError ), ] ); yield put( { type: types.RSVP_SALE_END_SYNCED_TO_EVENT_START } ); } } catch ( error ) { // ¯\_(ツ)_/¯ console.error( error ); } } /** * Listens for event start date and time changes after RSVP block is loaded. * * Functionality requires TEC to be enabled and post type to be event. * @yield */ export function* handleEventStartDateChanges() { try { // Proceed after creating dummy RSVP or after fetching yield take( [ types.INITIALIZE_RSVP, types.SET_RSVP_DETAILS ] ); const isEvent = yield call( isTribeEventPostType ); if ( isEvent && window.tec.events ) { const { SET_START_DATE_TIME, SET_START_TIME } = window.tec.events.app.main.data.blocks.datetime.types; let syncTask; while ( true ) { // Cache current event start date for comparison const eventStart = yield select( window.tec.events.app.main.data.blocks.datetime.selectors.getStart ); // Wait til use changes date or time on TEC datetime block yield take( [ SET_START_DATE_TIME, SET_START_TIME ] ); // Important to cancel any pre-existing forks to prevent bad data from being sent if ( syncTask ) { yield cancel( syncTask ); } syncTask = yield fork( syncRSVPSaleEndWithEventStart, eventStart ); } } } catch ( error ) { // ¯\_(ツ)_/¯ console.error( error ); } } // // ─── DATE & TIME ──────────────────────────────────────────────────────────────── // export function* handleRSVPDurationError() { let hasDurationError = false; const startDateMoment = yield select( selectors.getRSVPTempStartDateMoment ); const endDateMoment = yield select( selectors.getRSVPTempEndDateMoment ); if ( ! startDateMoment || ! endDateMoment ) { hasDurationError = true; } else { const startTime = yield select( selectors.getRSVPTempStartTime ); const endTime = yield select( selectors.getRSVPTempEndTime ); const startTimeSeconds = yield call( timeUtil.toSeconds, startTime, timeUtil.TIME_FORMAT_HH_MM_SS ); const endTimeSeconds = yield call( timeUtil.toSeconds, endTime, timeUtil.TIME_FORMAT_HH_MM_SS ); const startDateTimeMoment = yield call( momentUtil.setTimeInSeconds, startDateMoment.clone(), startTimeSeconds ); const endDateTimeMoment = yield call( momentUtil.setTimeInSeconds, endDateMoment.clone(), endTimeSeconds ); const durationHasError = yield call( [ startDateTimeMoment, 'isSameOrAfter' ], endDateTimeMoment ); if ( durationHasError ) { hasDurationError = true; } } yield put( actions.setRSVPHasDurationError( hasDurationError ) ); } export function* handleRSVPStartDate( action ) { const { date, dayPickerInput } = action.payload; const startDateMoment = yield date ? call( momentUtil.toMoment, date ) : undefined; const startDate = yield date ? call( momentUtil.toDatabaseDate, startDateMoment ) : ''; yield put( actions.setRSVPTempStartDate( startDate ) ); yield put( actions.setRSVPTempStartDateInput( dayPickerInput ) ); yield put( actions.setRSVPTempStartDateMoment( startDateMoment ) ); } export function* handleRSVPEndDate( action ) { const { date, dayPickerInput } = action.payload; const endDateMoment = yield date ? call( momentUtil.toMoment, date ) : undefined; const endDate = yield date ? call( momentUtil.toDatabaseDate, endDateMoment ) : ''; yield put( actions.setRSVPTempEndDate( endDate ) ); yield put( actions.setRSVPTempEndDateInput( dayPickerInput ) ); yield put( actions.setRSVPTempEndDateMoment( endDateMoment ) ); } export function* handleRSVPStartTime( action ) { const startTime = yield call( timeUtil.fromSeconds, action.payload.seconds, timeUtil.TIME_FORMAT_HH_MM ); yield put( actions.setRSVPTempStartTime( `${ startTime }:00` ) ); } export function* handleRSVPStartTimeInput( action ) { const startTime = yield call( timeUtil.fromSeconds, action.payload.seconds, timeUtil.TIME_FORMAT_HH_MM ); const startTimeMoment = yield call( momentUtil.toMoment, startTime, momentUtil.TIME_FORMAT, false ); const startTimeInput = yield call( momentUtil.toTime, startTimeMoment ); yield put( actions.setRSVPTempStartTimeInput( startTimeInput ) ); } export function* handleRSVPEndTime( action ) { const endTime = yield call( timeUtil.fromSeconds, action.payload.seconds, timeUtil.TIME_FORMAT_HH_MM ); yield put( actions.setRSVPTempEndTime( `${ endTime }:00` ) ); } export function* handleRSVPEndTimeInput( action ) { const endTime = yield call( timeUtil.fromSeconds, action.payload.seconds, timeUtil.TIME_FORMAT_HH_MM ); const endTimeMoment = yield call( momentUtil.toMoment, endTime, momentUtil.TIME_FORMAT, false ); const endTimeInput = yield call( momentUtil.toTime, endTimeMoment ); yield put( actions.setRSVPTempEndTimeInput( endTimeInput ) ); } /** * Handles proper RSVP deletion and RSVP block removal upon moving RSVP * * @yield */ export function* handleRSVPMove() { const rsvpId = yield select( selectors.getRSVPId ); const modalTicketId = yield select( moveSelectors.getModalTicketId ); if ( rsvpId === modalTicketId ) { const clientId = yield select( moveSelectors.getModalClientId ); yield put( actions.deleteRSVP() ); yield call( [ wpDispatch( 'core/block-editor' ), 'removeBlocks' ], [ clientId ] ); } } // // ─── HANDLERS ─────────────────────────────────────────────────────────────────── // export function* handler( action ) { switch ( action.type ) { case types.SET_RSVP_DETAILS: yield call( setRSVPDetails, action ); break; case types.SET_RSVP_TEMP_DETAILS: yield call( setRSVPTempDetails, action ); break; case types.INITIALIZE_RSVP: yield call( initializeRSVP ); break; case types.HANDLE_RSVP_START_DATE: yield call( handleRSVPStartDate, action ); yield call( handleRSVPDurationError ); yield put( actions.setRSVPHasChanges( true ) ); break; case types.HANDLE_RSVP_END_DATE: yield call( handleRSVPEndDate, action ); yield call( handleRSVPDurationError ); yield put( actions.setRSVPHasChanges( true ) ); break; case types.HANDLE_RSVP_START_TIME: yield call( handleRSVPStartTime, action ); yield call( handleRSVPStartTimeInput, action ); yield call( handleRSVPDurationError ); yield put( actions.setRSVPHasChanges( true ) ); break; case types.HANDLE_RSVP_END_TIME: yield call( handleRSVPEndTime, action ); yield call( handleRSVPEndTimeInput, action ); yield call( handleRSVPDurationError ); yield put( actions.setRSVPHasChanges( true ) ); break; case MOVE_TICKET_SUCCESS: yield call( handleRSVPMove ); break; default: break; } } /** * Temporary bandaid until datepickers allow blank state * * @yield */ export function* setNonEventPostTypeEndDate() { yield take( [ types.INITIALIZE_RSVP ] ); if ( yield call( isTribeEventPostType ) ) { return; } const tempEndMoment = yield select( selectors.getRSVPTempEndDateMoment ); const endMoment = yield call( [ tempEndMoment, 'clone' ] ); const { date, dateInput, moment, time } = yield call( createRSVPDates, endMoment.toDate() ); yield all( [ put( actions.setRSVPTempEndDate( date ) ), put( actions.setRSVPTempEndDateInput( dateInput ) ), put( actions.setRSVPTempEndDateMoment( moment ) ), put( actions.setRSVPTempEndTime( time ) ), put( actions.setRSVPEndDate( date ) ), put( actions.setRSVPEndDateInput( dateInput ) ), put( actions.setRSVPEndDateMoment( moment ) ), put( actions.setRSVPEndTime( time ) ), ] ); } // // ─── WATCHERS ─────────────────────────────────────────────────────────────────── // export default function* watchers() { yield takeEvery( [ types.SET_RSVP_DETAILS, types.SET_RSVP_TEMP_DETAILS, types.INITIALIZE_RSVP, types.HANDLE_RSVP_START_DATE, types.HANDLE_RSVP_END_DATE, types.HANDLE_RSVP_START_TIME, types.HANDLE_RSVP_END_TIME, MOVE_TICKET_SUCCESS, ], handler ); yield fork( handleEventStartDateChanges ); yield fork( setNonEventPostTypeEndDate ); }
Save File
Cancel