N°3430 - fix preference page's warning and add missing token generation

- fix the warning (ajax call interrupted) if preference form ajax call is way faster than the one of the 2 other by adding a new timeout_duration option before the redirect.
This commit is contained in:
bruno-ds
2021-02-18 12:18:38 +01:00
parent 83434b5506
commit e9e18513be
7 changed files with 32 additions and 17 deletions

View File

@@ -33,6 +33,7 @@ $(function()
category: 'redirect',
url: null,
modal: false,
timeout_duration: 400,
},
cancel_rule: {
category: 'close',
@@ -56,7 +57,7 @@ $(function()
this.options.submit_rule.url = this.options.submit_url;
if((this.options.cancel_url !== null) && (this.options.cancel_url !== ''))
this.options.cancel_rule.url = this.options.cancel_url;
this._super();
},
@@ -143,12 +144,14 @@ $(function()
// Determine where we go in case validation is successful
var sRuleType = me.options.submit_rule.category;
var bRedirectInModal = me.options.submit_rule.modal;
var iRedirectTimeout = me.options.submit_rule.timeout_duration;
var sRedirectUrl = me.options.submit_rule.url;
// - The validation might want us to be redirect elsewhere
if(oValidation.valid)
{
// Checking if we have to redirect to another page
// Typically this happens when applying a stimulus, we redirect to the transition form
// This code let the ajax response override the initial parameters
if(oValidation.redirection !== undefined)
{
var oRedirection = oValidation.redirection;
@@ -160,6 +163,10 @@ $(function()
{
sRedirectUrl = oRedirection.url;
}
if(oRedirection.timeout_duration !== undefined)
{
iRedirectTimeout = oRedirection.timeout_duration;
}
sRuleType = 'redirect';
}
}
@@ -241,7 +248,7 @@ $(function()
// Checking if we have to redirect to another page
if(sRuleType === 'redirect')
{
me._applyRedirectRule(sRedirectUrl, bRedirectInModal);
me._applyRedirectRule(sRedirectUrl, bRedirectInModal, iRedirectTimeout);
}
// Close rule only needs to be applied to non modal forms (modal is always closed on submit)
else if(sRuleType === 'close')
@@ -360,10 +367,13 @@ $(function()
{
$('#page_overlay').fadeOut(200);
},
_applyRedirectRule: function(sRedirectUrl, bRedirectInModal)
_applyRedirectRule: function(sRedirectUrl, bRedirectInModal, iRedirectTimeout)
{
var me = this;
//optional argument
iRedirectTimeout = (typeof iRedirectTimeout !== 'undefined' && iRedirectTimeout != null ) ? iRedirectTimeout : 400;
// Always close current modal
if(this.options.is_modal)
{
@@ -391,8 +401,8 @@ $(function()
// Showing loader while redirecting, otherwise user tend to click somewhere in the page.
// Note: We use a timeout because .always() is called right after here and will hide the loader
setTimeout(function(){ me._disableFormBeforeLoading(); }, 50);
// Redirecting after a few ms so the user can see what happend
setTimeout(function() { location.href = sRedirectUrl; }, 400);
// Redirecting after a few ms so the user can see what happened
setTimeout(function() { location.href = sRedirectUrl; }, iRedirectTimeout);
}
}
},

View File

@@ -211,6 +211,7 @@ class UserProfileBrickController extends BrickController
{
$aFormData['validation']['redirection'] = array(
'url' => $oUrlGenerator->generate('p_user_profile_brick'),
'timeout_duration' => 1000, //since there are several ajax request, we use a longer timeout in hope that they will all be finished in time. A promise would have been more reliable, but since this change is made in a minor version, this approach is less error prone.
);
}
}

View File

@@ -1050,7 +1050,8 @@ class ObjectFormManager extends FormManager
public function CheckTransaction($aData)
{
if (! utils::IsTransactionValid($this->oForm->GetTransactionId())) {
$isTransactionValid = \utils::IsTransactionValid($this->oForm->GetTransactionId(), false); //The transaction token is kept in order to preserve BC with ajax forms (the second call would fail if the token is deleted). (The GC will take care of cleaning the token for us later on)
if (!$isTransactionValid) {
if ($this->oObject->IsNew()) {
$sError = Dict::S('UI:Error:ObjectAlreadyCreated');
} else {

View File

@@ -48,6 +48,8 @@ class PasswordFormManager extends FormManager
// Building the form
$oForm = new Form('change_password');
$oForm->SetTransactionId(\utils::GetNewTransactionId());
// Adding hidden field with form type
$oField = new HiddenField('form_type');
$oField->SetCurrentValue('change_password');

View File

@@ -49,6 +49,8 @@ class PreferencesFormManager extends FormManager
// Building the form
$oForm = new Form('preferences');
$oForm->SetTransactionId(\utils::GetNewTransactionId());
// Adding hidden field with form type
$oField = new HiddenField('form_type');
$oField->SetCurrentValue('preferences');

View File

@@ -227,16 +227,10 @@
$('#user-profile-wrapper .form_field .help-block > p').remove();
// Submiting contact form through AJAX
//if($('#{{ oContactForm.id }} .field_set').field_set('hasTouchedFields'))
//{
$('#{{ oContactForm.id }}').portal_form_handler('submit', oEvent);
//}
$('#{{ oContactForm.id }}').portal_form_handler('submit', oEvent);
// Submiting preferences form through AJAX
//if($('#{{ oPreferencesForm.id }} .field_set').field_set('hasTouchedFields'))
//{
$('#{{ oPreferencesForm.id }}').portal_form_handler('submit', oEvent);
//}
$('#{{ oPreferencesForm.id }}').portal_form_handler('submit', oEvent);
{% if oPasswordForm is not null %}
// Submiting password form through AJAX

View File

@@ -160,7 +160,9 @@ abstract class FormManager
/**
* @param array|null $aArgs
*
* @return mixed
* @return array
*
* @since 2.7.4 3.0.0 N°3430
*/
public function OnSubmit($aArgs = null)
{
@@ -179,13 +181,16 @@ abstract class FormManager
}
/**
* @param $aData
* @param array $aData
*
* @return array
*
* @since 2.7.4 3.0.0 N°3430
*/
public function CheckTransaction($aData)
{
if (! \utils::IsTransactionValid($this->oForm->GetTransactionId())) {
$isTransactionValid = \utils::IsTransactionValid($this->oForm->GetTransactionId(), false); //The transaction token is kept in order to preserve BC with ajax forms (the second call would fail if the token is deleted). (The GC will take care of cleaning the token for us later on)
if (!$isTransactionValid) {
$aData['messages']['error'] += [
'_main' => [\Dict::S('UI:Error:InvalidToken')] //This message is generic, if you override this method you should use a more precise message. @see \Combodo\iTop\Portal\Form\ObjectFormManager::CheckTransaction
];