Files
Romhack-Plaza---WordPress-P…/ts/manage-notifications/index.ts
2026-01-11 19:39:55 +01:00

135 lines
5.6 KiB
TypeScript

declare const _romhackplaza_manage_notifications: any;
declare const romhackplaza_modal_notifications: any;
declare function romhackplaza_manage_modal( a: object, b: string|undefined, c: string|undefined, d: string|undefined, e: string|undefined ): void;
document.addEventListener("DOMContentLoaded", () => {
function _change_event_to_reload(){
romhackplaza_modal_notifications.close_button.onclick = () => {
romhackplaza_manage_modal( romhackplaza_modal_notifications, "none", "", "", "" );
window.location.reload();
}
window.addEventListener( 'click', (e) => { if( e.target === romhackplaza_modal_notifications.modal ) window.location.reload(); } );
}
function _reply_to_message( e: Event ) {
const button = e.currentTarget as HTMLElement;
const html_content: HTMLFormElement = document.createElement( 'form' );
html_content.id = "reply_message_form"
const textarea: HTMLTextAreaElement = document.createElement( 'textarea' );
textarea.id = "reply_message_content";
const submit_button: HTMLElement = document.createElement( 'input' );
submit_button.setAttribute( 'type', 'submit' );
submit_button.setAttribute( 'value', 'Send' );
html_content.appendChild( textarea );
html_content.appendChild( submit_button );
romhackplaza_manage_modal( romhackplaza_modal_notifications, "block", "Reply to a message", "", html_content.outerHTML );
document.getElementById( 'reply_message_form' )!.onsubmit = function( e: SubmitEvent ) {
e.preventDefault();
// @ts-ignore
let txt = document.getElementById( 'reply_message_content' ).value;
romhackplaza_manage_modal( romhackplaza_modal_notifications, "none", "", "", " " );
if ( txt == "" || txt == "none" || txt == "undefined" ){
romhackplaza_manage_modal( romhackplaza_modal_notifications, "block", "Submit error", "A content is required", "" );
_change_event_to_reload();
return;
}
txt = "Reply to a notification " + button.getAttribute( 'data-id' ) + "\n\n" + txt;
let form_data: FormData = new FormData();
form_data.append( 'notification', button.getAttribute( 'data-id' ) as string )
form_data.append( 'content', txt );
form_data.append( '_wpnonce', _romhackplaza_manage_notifications.reply_nonce );
form_data.append( 'action', 'notifications_reply' );
const XML: XMLHttpRequest = new XMLHttpRequest();
XML.open( 'POST', _romhackplaza_manage_notifications.manage_url, true );
XML.onreadystatechange = function () {
if (XML.readyState === XMLHttpRequest.DONE && XML.status === 200) {
let real_response: any;
try {
real_response = JSON.parse(XML.responseText);
} catch( e ) {
console.error( e, XML.responseText );
return;
}
if( real_response != "-1" && real_response?.success == true ) {
romhackplaza_manage_modal( romhackplaza_modal_notifications, "block", "Submit successful", "Notification sent.", "" );
_change_event_to_reload();
return;
} else {
romhackplaza_manage_modal( romhackplaza_modal_notifications, "block", "Submit error", real_response?.message ?? real_response, "" );
_change_event_to_reload();
return;
}
}
}
XML.send( form_data );
}
}
function _mark_as_read( e: Event ) {
const button = e.currentTarget as HTMLElement;
let form_data: FormData = new FormData();
form_data.append( 'notification', button.getAttribute( 'data-id' ) as string )
form_data.append( '_wpnonce', _romhackplaza_manage_notifications.mark_as_read_nonce );
form_data.append( 'action', 'notifications_unread_to_read' );
const XML: XMLHttpRequest = new XMLHttpRequest();
XML.open( 'POST', _romhackplaza_manage_notifications.manage_url, true );
XML.onreadystatechange = function () {
if (XML.readyState === XMLHttpRequest.DONE && XML.status === 200) {
let real_response: any;
try {
real_response = JSON.parse(XML.responseText);
} catch( e ) {
console.error( e, XML.responseText );
return;
}
if( real_response != "-1" && real_response?.success == true ) {
document.getElementById( 'new-info-' + button.getAttribute( 'data-id' ) as string )?.remove();
button.parentElement?.remove();
return;
} else {
alert( "An error occurred : " + ( real_response?.message ?? real_response ) );
return;
}
}
}
XML.send( form_data );
}
// Replies.
const replies_buttons: NodeList = document.querySelectorAll('.reply-message');
for (const reply_button of replies_buttons) {
reply_button.addEventListener("click", _reply_to_message );
}
const mark_as_read_buttons: NodeList = document.querySelectorAll('.mark-as-read');
for (const mark_as_read_button of mark_as_read_buttons) {
mark_as_read_button.addEventListener("click", _mark_as_read );
}
})