diff --git a/src/components/task.vue b/src/components/task.vue index d93bf1d34..7e2dbec89 100644 --- a/src/components/task.vue +++ b/src/components/task.vue @@ -717,67 +717,194 @@ export default { redirectToRequest(requestId) { window.location.href = `/requests/${requestId}`; }, + // initSocketListeners() { + // this.addSocketListener( + // `updated-${this.requestId}`, + // `ProcessMaker.Models.ProcessRequest.${this.requestId}`, + // '.ProcessUpdated', + // (data) => { + // if (data.event === 'ACTIVITY_EXCEPTION') { + // this.$emit('error', this.requestId); + // } + // } + // ); + // // v2 redirect to listener + // this.addSocketListener( + // `redirect-${this.requestId}`, + // `ProcessMaker.Models.ProcessRequest.${this.requestId}`, + // '.RedirectTo', + // (data) => { + // switch (data.method) { + // case 'javascript:redirectToTask': + // if (data?.params[0]?.tokenId) { + // this.loadingTask = true; + // this.loadTask(data.params[0].tokenId); + // this.taskId = data.params[0].tokenId; + // this.reload(); + // } + // break; + // case 'javascript:processUpdated': + // if ( + // ['ACTIVITY_ACTIVATED', 'ACTIVITY_COMPLETED'].includes(data.event) + // && data.elementType === 'task' + // ) { + // if (!this.task.elementDestination?.type) { + // this.taskId = data.taskId; + // } + // this.reload(); + // } + // if (data.event === 'ACTIVITY_EXCEPTION') { + // this.$emit('error', this.requestId); + // } + // break; + // case 'javascript:processCompletedRedirect': + // this.processCompletedRedirect( + // data.params[0], + // this.userId, + // this.requestId + // ); + // break; + // default: + // console.warn('redirect', data); + // window.location.href = `/requests/${this.requestId}`; + // } + // } + // ); + // this.loadingListeners = false; + + // // We might have missed an event before initSocketListeners + // // was called so reload to check if there's a task waiting + // if (!this.taskId) { + // this.reload(); + // } + // }, + /** + * Initializes socket listeners for process updates and redirects. + * This method sets up listeners to handle specific events and reloads + * the task if necessary. + */ initSocketListeners() { + this.addProcessUpdateListener(); + this.addRedirectListener(); + this.loadingListeners = false; + + // Reload to check if there's a task waiting, in case an event was missed + if (!this.taskId) { + this.reload(); + } + }, + + /** + * Adds a socket listener for process updates. + * Listens for specific events related to the process and triggers appropriate actions. + */ + addProcessUpdateListener() { this.addSocketListener( `updated-${this.requestId}`, `ProcessMaker.Models.ProcessRequest.${this.requestId}`, '.ProcessUpdated', - (data) => { - if (data.event === 'ACTIVITY_EXCEPTION') { - this.$emit('error', this.requestId); - } - } + (data) => this.handleProcessUpdate(data) ); - // v2 redirect to listener + }, + + /** + * Handles process update events. + * Emits an error if an activity exception event is detected. + * + * @param {Object} data - The event data received from the socket listener. + */ + handleProcessUpdate(data) { + if (data.event === 'ACTIVITY_EXCEPTION') { + this.$emit('error', this.requestId); + window.location.href = `/requests/${this.requestId}`; + } + }, + + /** + * Adds a socket listener for redirect events. + * Listens for specific redirect actions and handles them according to the method provided. + */ + addRedirectListener() { this.addSocketListener( `redirect-${this.requestId}`, `ProcessMaker.Models.ProcessRequest.${this.requestId}`, '.RedirectTo', - (data) => { - switch (data.method) { - case 'javascript:redirectToTask': - if (data?.params[0]?.tokenId) { - this.loadingTask = true; - this.loadTask(data.params[0].tokenId); - this.taskId = data.params[0].tokenId; - this.reload(); - } - break; - case 'javascript:processUpdated': - if ( - ['ACTIVITY_ACTIVATED', 'ACTIVITY_COMPLETED'].includes(data.event) - && data.elementType === 'task' - ) { - if (!this.task.elementDestination?.type) { - this.taskId = data.taskId; - } - this.reload(); - } - if (data.event === 'ACTIVITY_EXCEPTION') { - this.$emit('error', this.requestId); - } - break; - case 'javascript:processCompletedRedirect': - this.processCompletedRedirect( - data.params[0], - this.userId, - this.requestId - ); - break; - default: - console.warn('redirect', data); - window.location.href = `/requests/${this.requestId}`; - } - } + (data) => this.handleRedirect(data) ); - this.loadingListeners = false; + }, - // We might have missed an event before initSocketListeners - // was called so reload to check if there's a task waiting - if (!this.taskId) { + /** + * Handles redirect events based on the method provided in the event data. + * Calls specific handlers for different redirect methods or falls back to a default redirect. + * + * @param {Object} data - The event data received from the socket listener. + */ + handleRedirect(data) { + switch (data.method) { + case 'javascript:redirectToTask': + this.handleRedirectToTask(data); + break; + case 'javascript:processUpdated': + this.handleProcessUpdated(data); + break; + case 'javascript:processCompletedRedirect': + this.processCompletedRedirect( + data.params[0], + this.userId, + this.requestId + ); + break; + default: + this.handleDefaultRedirect(data); + } + }, + + /** + * Handles the 'redirectToTask' event by loading the specified task. + * Updates the current task ID and reloads the task data. + * + * @param {Object} data - The event data containing the tokenId of the task. + */ + handleRedirectToTask(data) { + if (data?.params[0]?.tokenId) { + this.loadingTask = true; + this.loadTask(data.params[0].tokenId); + this.taskId = data.params[0].tokenId; + this.reload(); + } + }, + + /** + * Handles the 'processUpdated' event by checking the event type and updating the task if necessary. + * Reloads the task data if the event is relevant. + * + * @param {Object} data - The event data containing the process update information. + */ + handleProcessUpdated(data) { + if ( + ['ACTIVITY_ACTIVATED', 'ACTIVITY_COMPLETED'].includes(data.event) + && data.elementType === 'task' + ) { + if (!this.task.elementDestination?.type) { + this.taskId = data.taskId; + } this.reload(); } + if (data.event === 'ACTIVITY_EXCEPTION') { + this.$emit('error', this.requestId); + } + }, + + /** + * Handles the default redirect action by logging a warning and redirecting the user to the request page. + * + * @param {Object} data - The event data that triggered the default redirect. + */ + handleDefaultRedirect(data) { + console.warn('redirect', data); + window.location.href = `/requests/${this.requestId}`; }, + existsEventMessage(id, data) { if (sessionStorage.getItem(id)) { return true;