Skip to content

Commit

Permalink
feat: added ajax error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang authored and NiedielnitsevIvan committed Apr 8, 2024
1 parent f2fc1c3 commit 5f124d8
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion cms/templates/container_editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@
// Serves to initialize the rendering of a xblock edit modal window.
setTimeout(() => $('.button-edit').trigger('click'), 300);

const iframeMessageHandler = (method, msg) => {
return window.parent.postMessage({ method, msg }, '*');
}

/**
* Callback function for the MutationObserver to handle mutations
* and send information when the modal window close logic is triggered.
Expand All @@ -195,7 +199,7 @@

// If removed nodes match the current modal, post a message to close the edit modal.
if (editModalRemovedNodes.length > 0) {
window.parent.postMessage({ method: 'close_edit_modal', msg: 'Message from edit iframe modal' }, '*');
iframeMessageHandler('close_edit_modal', 'Sends a message when the modal window is closed');
}
})

Expand All @@ -205,6 +209,20 @@
attributes: true,
attributeFilter: ['class']
});

const originalAjax = $.ajax;
// Notifies the parent element that a server error has occurred.
$.ajax = function(options) {
const errorHandler = options.error;
options.error = function(xhr, textStatus, errorThrown) {
iframeMessageHandler('edit_modal-error', 'Sends a message in case of server error');

if (errorHandler) {
errorHandler(xhr, textStatus, errorThrown);
}
};
return originalAjax.call(this, options);
};
});
</script>
</%block>
Expand Down

0 comments on commit 5f124d8

Please sign in to comment.