Enable "add contributors" functionality

This commit is contained in:
Kelly Dwan 2019-11-13 16:05:18 -05:00
parent b0672a6f6e
commit 5a72ff8b53
No known key found for this signature in database
GPG key ID: 8BA5575F3D11575D
4 changed files with 63 additions and 6 deletions

View file

@ -66,6 +66,7 @@ jQuery( document ).ready( function( $ ) {
url: ajaxurl,
data: Object.assign( {
action: 'manage_contributors',
pledge_id: FiveForTheFuture.pledgeId,
_ajax_nonce: FiveForTheFuture.manageNonce,
}, data ),
success: callback,
@ -73,6 +74,33 @@ jQuery( document ).ready( function( $ ) {
} );
}
/**
* Send off the AJAX request with contributors pulled from the contributor text field.
*/
function _addContributors() {
const contribs = $( '#5ftf-pledge-contributors' ).val();
if ( ! contribs.length ) {
return;
}
sendAjaxRequest( {
contributors: contribs,
manage_action: 'add-contributor',
}, function( response ) {
if ( ! response.success ) {
const $message = $( '<div>' )
.attr( 'id', 'add-contrib-message' )
.addClass( 'notice notice-error notice-alt' )
.html( '<p>' + response.message + '</p>' );
$( '#add-contrib-message' ).replaceWith( $message );
} else if ( response.contributors ) {
render( response.contributors, container );
$( '#5ftf-pledge-contributors' ).val( '' );
}
} );
}
// Initialize.
const container = document.getElementById( '5ftf-contributors' );
render( fftfContributors, container );
@ -86,7 +114,6 @@ jQuery( document ).ready( function( $ ) {
const data = event.currentTarget.dataset;
sendAjaxRequest( {
pledge_id: data.pledgePost || 0,
contributor_id: data.contributorPost || 0,
manage_action: data.action || '',
} );
@ -99,9 +126,22 @@ jQuery( document ).ready( function( $ ) {
const data = event.currentTarget.dataset;
sendAjaxRequest( {
pledge_id: data.pledgePost || 0,
contributor_id: data.contributorPost || 0,
manage_action: data.action || '',
} );
} );
// Add Contributor button action.
$( container ).on( 'click', '[data-action="add-contributor"]', function( event ) {
event.preventDefault();
_addContributors();
} );
// Prevent "enter" in the contributor field from submitting the whole post form.
$( container ).on( 'keydown', '#5ftf-pledge-contributors', function( event ) {
if ( 13 === event.which ) {
event.preventDefault();
_addContributors();
}
} );
} );