| 1 |
/** |
| 2 |
* Become a Teacher form handler js |
| 3 |
* |
| 4 |
* @author ThimPress |
| 5 |
* @version 3.0.0 |
| 6 |
*/ |
| 7 |
|
| 8 |
import { lpAjaxParseJsonOld } from '../utils.js'; |
| 9 |
jQuery( function( $ ) { |
| 10 |
$( 'form[name="become-teacher-form"]' ).each( function() { |
| 11 |
const $form = $( this ), |
| 12 |
$submit = $form.find( 'button[type="submit"]' ), |
| 13 |
hideMessages = function() { |
| 14 |
$( '.learn-press-error, .learn-press-message' ).fadeOut( 'fast', function() { |
| 15 |
$( this ).remove(); |
| 16 |
} ); |
| 17 |
}, |
| 18 |
showMessages = function( messages ) { |
| 19 |
let m = []; |
| 20 |
if ( $.isPlainObject( messages ) ) { |
| 21 |
for ( const i in messages ) { |
| 22 |
m.push( $( messages[ i ] ) ); |
| 23 |
} |
| 24 |
} else if ( $.isArray( messages ) ) { |
| 25 |
m = messages.reverse(); |
| 26 |
} else { |
| 27 |
m = [ messages ]; |
| 28 |
} |
| 29 |
for ( let i = 0; i < m.length; i++ ) { |
| 30 |
$( m[ i ] ).insertBefore( $form ); |
| 31 |
} |
| 32 |
}, |
| 33 |
blockForm = function( block ) { |
| 34 |
return $form.find( 'input, select, button, textarea' ) |
| 35 |
.prop( 'disabled', !! block ); |
| 36 |
}, |
| 37 |
beforeSend = function() { |
| 38 |
hideMessages(); |
| 39 |
|
| 40 |
blockForm( true ) |
| 41 |
.filter( $submit ) |
| 42 |
.data( 'origin-text', $submit.text() ) |
| 43 |
.html( $submit.data( 'text' ) ); |
| 44 |
}, |
| 45 |
ajaxSuccess = function( response ) { |
| 46 |
response = lpAjaxParseJsonOld( response ); |
| 47 |
if ( response.message ) { |
| 48 |
showMessages( response.message ); |
| 49 |
} |
| 50 |
|
| 51 |
blockForm().filter( $submit ).html( $submit.data( 'origin-text' ) ); |
| 52 |
|
| 53 |
if ( response.result === 'success' ) { |
| 54 |
$form.remove(); |
| 55 |
} else { |
| 56 |
$submit.prop( 'disabled', false ); |
| 57 |
$submit.html( $submit.data( 'text' ) ); |
| 58 |
} |
| 59 |
}, |
| 60 |
ajaxError = function( response ) { |
| 61 |
response = LP.parseJSON( response ); |
| 62 |
|
| 63 |
if ( response.message ) { |
| 64 |
showMessages( response.message ); |
| 65 |
} |
| 66 |
|
| 67 |
blockForm().filter( $submit ).html( $submit.data( 'origin-text' ) ); |
| 68 |
}; |
| 69 |
|
| 70 |
$form.on( 'submit', function( e ) { |
| 71 |
e.preventDefault(); |
| 72 |
if ( $form.triggerHandler( 'become_teacher_send' ) !== false ) { |
| 73 |
const url = new URL( window.location.href ); |
| 74 |
url.searchParams.set( 'lp-ajax', 'request-become-a-teacher' ); |
| 75 |
|
| 76 |
$.ajax( { |
| 77 |
url, |
| 78 |
data: $form.serialize(), |
| 79 |
dataType: 'text', |
| 80 |
type: 'POST', |
| 81 |
beforeSend, |
| 82 |
success: ajaxSuccess, |
| 83 |
error: ajaxError, |
| 84 |
} ); |
| 85 |
} |
| 86 |
return false; |
| 87 |
} ); |
| 88 |
} ); |
| 89 |
} ); |
| 90 |
|
| 91 |
|