113 lines
4.0 KiB
TypeScript
113 lines
4.0 KiB
TypeScript
import { format_state_for_select2 } from "../../ajax_requests";
|
|
declare const _romhackplaza_send_notification: 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;
|
|
declare const tinyMCE: any;
|
|
|
|
jQuery(document).ready(function( $: any ) {
|
|
|
|
$("#recipients").select2({
|
|
placeholder: "Search an user",
|
|
ajax: {
|
|
url: _romhackplaza_send_notification.fetch_url,
|
|
dataType: "json",
|
|
delay: 250,
|
|
data: function (params: any) {
|
|
return {
|
|
q: params.term,
|
|
action: "search_user",
|
|
_wpnonce: _romhackplaza_send_notification.fetch_nonce
|
|
};
|
|
},
|
|
processResults: function (results: any) {
|
|
return {
|
|
results: results
|
|
}
|
|
},
|
|
cache: true
|
|
},
|
|
minimumInputLength: 1,
|
|
templateResult: format_state_for_select2,
|
|
templateSelection: format_state_for_select2
|
|
});
|
|
|
|
$( "#send-manual-notification" ).on( 'submit', function( e: Event ){
|
|
|
|
e.preventDefault();
|
|
let type: string = $("#type_select").val() || "none";
|
|
|
|
if( type === "none" ){
|
|
romhackplaza_manage_modal( romhackplaza_modal_notifications, "block", "Submit error", "A type is required.", "" );
|
|
return;
|
|
}
|
|
|
|
let anonymous_sender: string = $( "#anonymous_sender" ).is( ':checked' ) ? "yes" : "no";
|
|
let all_users: string = $( "#recipients_all" ).is( ':checked' ) ? "yes" : "no";
|
|
if( all_users !== "yes" ){
|
|
|
|
// Defined number of users.
|
|
let recipients_list: any = $( "#recipients" ).val();
|
|
if( recipients_list.length <= 0 ){
|
|
romhackplaza_manage_modal( romhackplaza_modal_notifications, "block", "Submit error", "A user is required.", "" );
|
|
return;
|
|
}
|
|
|
|
} else {
|
|
let recipients_list: any = [];
|
|
}
|
|
|
|
let editor_content: string;
|
|
if( tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden() ) {
|
|
// Visual mode.
|
|
editor_content = tinyMCE.get( 'postContent' ).getContent();
|
|
} else {
|
|
// @ts-ignore
|
|
// Text mode.
|
|
editor_content = document.getElementById( 'postContent' ).value;
|
|
}
|
|
|
|
if( editor_content === "" ){
|
|
romhackplaza_manage_modal( romhackplaza_modal_notifications, "block", "Submit error", "Content is required.", "" );
|
|
return;
|
|
}
|
|
|
|
// @ts-ignore
|
|
const form_data: FormData = new FormData( this );
|
|
form_data.append( "action", "submit_notification" );
|
|
form_data.append( "content", editor_content );
|
|
form_data.append( "_wpnonce", _romhackplaza_send_notification.submit_nonce );
|
|
|
|
$.ajax({
|
|
type: "POST",
|
|
url: _romhackplaza_send_notification.fetch_url,
|
|
data: form_data,
|
|
processData: false,
|
|
contentType: false,
|
|
success: function( response: any ) {
|
|
|
|
let real_response: any;
|
|
|
|
if( typeof response === "string" ) {
|
|
try {
|
|
real_response = JSON.parse(response);
|
|
} catch( e ) {
|
|
console.error( e, response );
|
|
return;
|
|
}
|
|
} else {
|
|
real_response = response;
|
|
}
|
|
|
|
if( real_response != "-1" && real_response?.success === true ) {
|
|
romhackplaza_manage_modal( romhackplaza_modal_notifications, "block", "Submit successful", "Notification sent.", "" );
|
|
return;
|
|
} else {
|
|
romhackplaza_manage_modal( romhackplaza_modal_notifications, "block", "Submit error", real_response?.message ?? real_response, "" );
|
|
return;
|
|
}
|
|
|
|
}
|
|
})
|
|
});
|
|
|
|
}); |