Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/el-courses'
Browse files Browse the repository at this point in the history
  • Loading branch information
tungnxt89 committed Jan 30, 2024
2 parents c4c9cbc + 7a49d92 commit 88b73c4
Show file tree
Hide file tree
Showing 52 changed files with 1,022 additions and 1,200 deletions.
2 changes: 1 addition & 1 deletion assets/src/apps/js/admin/admin-notices.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const callAdminNotices = ( set = '' ) => {
params += set ? ( tab ? '&' : '?' ) + `${ set }` : '';

fetch( adminAPI.apiAdminNotice + params, {
method: 'GET',
method: 'POST',
headers: {
'X-WP-Nonce': lpGlobalSettings.nonce,
},
Expand Down
2 changes: 1 addition & 1 deletion assets/src/apps/js/admin/course-material.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ document.addEventListener( 'DOMContentLoaded', function() {
parent.insertAdjacentHTML(
'beforeend',
`<tr data-id="${ data.file_id }" data-sort="${ data.orders }" >
<td class="sort">${ data.file_name }</td>
<td class="sort"><span class="dashicons dashicons-menu"></span> ${ data.file_name }</td>
<td>${ capitalizeFirstChar( data.method ) }</td>
<td><a href="javascript:void(0)" class="delete-material-row" data-id="${ data.file_id }">${ delete_btn_text }</a></td>
</tr>`
Expand Down
109 changes: 29 additions & 80 deletions assets/src/apps/js/frontend/checkout.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
( function( $, settings ) {
'use strict';

if ( window.LP === undefined ) {
window.LP = {};
}

/**
* Checkout
*
Expand All @@ -31,33 +27,6 @@
};
}

const needPayment = function() {
return $payments.length > 0;
};

const selectedPayment = function() {
return $payments.find( 'input[name="payment_method"]:checked' ).val();
};

const isLoggedIn = function() {
return $formCheckout.find( 'input[name="checkout-account-switch-form"]:checked' ).length = 0;
};

const getActiveFormData = function() {
const formName = $formCheckout.find( 'input[name="checkout-account-switch-form"]:checked' ).val();
const $form = $( '#checkout-account-' + formName );

return $form.serializeJSON();
};

const getPaymentData = function() {
return $( '#checkout-payment' ).serializeJSON();
};

const getPaymentNote = function() {
return $( '.learn-press-checkout-comment' ).serializeJSON();
};

const showErrors = function( errors ) {
showMessage( errors );
const firstId = Object.keys( errors )[ 0 ];
Expand All @@ -68,19 +37,6 @@
const _formSubmit = function( e ) {
e.preventDefault();

if ( needPayment() && ! selectedPayment() ) {
showMessage( 'Please select payment method', true );
return false;
}

let formData = {};

if ( ! isLoggedIn() ) {
formData = $.extend( formData, getActiveFormData(), getPaymentNote() );
}

formData = $.extend( formData, getPaymentData() );

removeMessage();

const btnText = $buttonCheckout.text();
Expand All @@ -89,17 +45,28 @@
urlHandle.searchParams.set( 'lp-ajax', 'checkout' );

const elCheckoutForm = document.querySelector( '#learn-press-checkout-form' );
const elInputNonce = document.querySelector( 'input[name="learn-press-checkout-nonce"]' );
const elElCheckoutAccountType = elCheckoutForm.querySelector( 'input[name="checkout-account-switch-form"]:checked' );
if ( elElCheckoutAccountType ) {
formData[ 'checkout-account-switch-form' ] = elElCheckoutAccountType.value;

// Test
const formDatax = new FormData( elCheckoutForm );

// get values from FormData
const obj = Object.fromEntries( Array.from( formDatax.keys(), ( key ) => {
const val = formDatax.getAll( key );

return [ key, val.length > 1 ? val : val.pop() ];
} ) );

// Check validates form
if ( 'undefined' === typeof obj.payment_method || obj.payment_method.length === 0 ) {
showMessage( 'Please select payment method', 'error' );
return false;
}
formData[ 'learn-press-checkout-nonce' ] = elInputNonce.value;
// End check validates form

$.ajax( {
url: urlHandle,
dataType: 'html',
data: formData,
data: obj,
type: 'POST',
beforeSend() {
$( '#learn-press-checkout-place-order' ).addClass( 'loading' );
Expand Down Expand Up @@ -128,7 +95,7 @@
error( jqXHR, textStatus, errorThrown ) {
$( '#learn-press-checkout-place-order' ).removeClass( 'loading' );

showMessage( '<div class="learn-press-message error">' + errorThrown + '</div>' );
showMessage( errorThrown );

$buttonCheckout.html( btnText );

Expand Down Expand Up @@ -170,38 +137,16 @@
* Append messages into document.
*
* @param message
* @param wrap
* @param status
*/
const showMessage = function( message, wrap = false ) {
const showMessage = function( message, status = 'error' ) {
removeMessage();
const $formCheckout = document.querySelector( '#learn-press-checkout-form' );

if ( $.isPlainObject( message ) ) {
Object.keys( message ).reverse().forEach( ( id ) => {
const m = message[ id ];
let msg = Array.isArray( m ) ? m[ 0 ] : m;
const type = Array.isArray( m ) ? m[ 1 ] : '';
msg = '<div class="learn-press-message ' + ( typeof ( type ) === 'string' ? type : '' ) + '">' + msg + '</div>';
$formCheckout.prepend( msg );
} );

return;
}
message = `<div class="learn-press-message ${ status }">${ message }</div>`;
$formCheckout.insertAdjacentHTML( 'afterbegin', message );

if ( wrap ) {
message = '<div class="learn-press-message ' + ( typeof ( wrap ) === 'string' ? wrap : '' ) + '">' + message + '</div>';
}

if ( Array.isArray( message ) ) {
message.map( ( msg ) => $formCheckout.prepend( '<div class="learn-press-message error">' + msg + '</div>' ) );
} else {
$formCheckout.prepend( '<div class="learn-press-message error">' + message + '</div>' );
}

$( 'html, body' ).animate( {
scrollTop: ( $formCheckout.offset().top - 100 ),
}, 1000 );

$( document ).trigger( 'learn-press/checkout-error' );
$formCheckout.scrollIntoView();
};

/**
Expand Down Expand Up @@ -244,7 +189,11 @@
* Remove all messages
*/
const removeMessage = function() {
$( '.learn-press-error, .learn-press-notice, .learn-press-message' ).remove();
const lpMessage = document.querySelector( '.learn-press-message' );
if ( ! lpMessage ) {
return;
}
lpMessage.remove();
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class QuestionFillInBlanks extends QuestionBase {
const parent = target.closest( '.lp-fib-input' );

if ( parent ) {
const question_id = e.target.closest( '.question-fill_in_blanks' ).dataset.id;
const elQuestionFIB = target.closest( '.question-fill_in_blanks' );
const question_id = elQuestionFIB.dataset.id;
this.setAnswered( question_id, target.dataset.id, target.value );
}
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ class ButtonCheck extends Component {
checkAnswer = () => {
const { checkAnswer, question, answered } = this.props;

// Fix temporary for FIB.
if ( question.type === 'fill_in_blanks' ) {
const elFIB = document.querySelector( `.question-fill_in_blanks[data-id="${ question.id }"]` );
const elInputs = elFIB.querySelectorAll( '.lp-fib-input > input' );
elInputs.forEach( ( elInput ) => {
if ( elInput.value.length > 0 ) {
this.setState( {
loading: true,
} );
checkAnswer( question.id );
return false;
}
} );
}

if ( answered ) {
checkAnswer( question.id );

Expand All @@ -33,7 +48,6 @@ class ButtonCheck extends Component {
<>
<button className={ classNames( 'lp-button', 'instant-check', {
loading: this.state.loading,
disable: ! answered,
} ) } onClick={ this.checkAnswer }
>
<span className="instant-check__icon" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const Status = () => {
indexHtml = sprintf( __( 'Question <span>%d of %d</span>', 'learnpress' ), start, questionsCount );
}
} else {
indexHtml = sprintf( __( 'Question <span>%d to %d</span>', 'learnpress' ), start, end );
indexHtml = sprintf( __( 'Question <span>%d of %d</span>', 'learnpress' ), start, end );
}

return (
Expand Down
Loading

0 comments on commit 88b73c4

Please sign in to comment.