WIP : coupon refactor + fix
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
$(function($){
|
||||
|
||||
// Manage how coupon and rules are saved
|
||||
// Manage how coupon and conditions are saved
|
||||
$.couponManager = {};
|
||||
// Rule to be saved
|
||||
$.couponManager.ruleToSave = {};
|
||||
$.couponManager.ruleToSave.serviceId = false;
|
||||
$.couponManager.ruleToSave.operators = {};
|
||||
$.couponManager.ruleToSave.values = {};
|
||||
// Rules payload to save
|
||||
$.couponManager.rulesToSave = [];
|
||||
// Rule being updated id
|
||||
$.couponManager.ruleToUpdateId = false;
|
||||
// Condition to be saved
|
||||
$.couponManager.conditionToSave = {};
|
||||
$.couponManager.conditionToSave.serviceId = false;
|
||||
$.couponManager.conditionToSave.operators = {};
|
||||
$.couponManager.conditionToSave.values = {};
|
||||
// Conditions payload to save
|
||||
$.couponManager.conditionsToSave = [];
|
||||
// Condition being updated id
|
||||
$.couponManager.conditionToUpdateId = false;
|
||||
|
||||
// Clean array from deleteValue (undefined) keys
|
||||
Array.prototype.clean = function(deleteValue) {
|
||||
@@ -23,110 +23,109 @@ $(function($){
|
||||
return this;
|
||||
};
|
||||
|
||||
// Remove 1 Rule then Save Rules AJAX
|
||||
$.couponManager.removeRuleAjax = function(id) {
|
||||
// Delete rule in temporary array
|
||||
delete $.couponManager.rulesToSave[id];
|
||||
$.couponManager.rulesToSave.clean(undefined);
|
||||
// Remove 1 Condition then Save Conditions AJAX
|
||||
$.couponManager.removeConditionAjax = function(id) {
|
||||
// Delete condition in temporary array
|
||||
delete $.couponManager.conditionsToSave[id];
|
||||
$.couponManager.conditionsToSave.clean(undefined);
|
||||
|
||||
// Save
|
||||
$.couponManager.saveRuleAjax();
|
||||
$.couponManager.saveConditionAjax();
|
||||
};
|
||||
|
||||
// Add 1 Rule / or update the temporary Rules array then Save Rules via AJAX
|
||||
$.couponManager.createOrUpdateRuleAjax = function() {
|
||||
var id = $.couponManager.ruleToUpdateId;
|
||||
// Add 1 Condition / or update the temporary Conditions array then Save Conditions via AJAX
|
||||
$.couponManager.createOrUpdateConditionAjax = function() {
|
||||
var id = $.couponManager.conditionToUpdateId;
|
||||
// If create
|
||||
if(!id) {
|
||||
$.couponManager.rulesToSave.push($.couponManager.ruleToSave);
|
||||
$.couponManager.conditionsToSave.push($.couponManager.conditionToSave);
|
||||
} else { // else update
|
||||
$.couponManager.rulesToSave[id] = $.couponManager.ruleToSave;
|
||||
$.couponManager.conditionsToSave[id] = $.couponManager.conditionToSave;
|
||||
// reset edit mode to off
|
||||
$.couponManager.ruleToUpdateId = false;
|
||||
$.couponManager.conditionToUpdateId = false;
|
||||
}
|
||||
|
||||
// Save
|
||||
$.couponManager.saveRuleAjax();
|
||||
$.couponManager.saveConditionAjax();
|
||||
};
|
||||
|
||||
// Set rule inputs to allow editing
|
||||
$.couponManager.updateRuleSelectAjax = function(id) {
|
||||
$.couponManager.ruleToUpdateId = id;
|
||||
$.couponManager.ruleToSave = $.couponManager.rulesToSave[id];
|
||||
// Set condition inputs to allow editing
|
||||
$.couponManager.updateConditionSelectAjax = function(id) {
|
||||
$.couponManager.conditionToUpdateId = id;
|
||||
$.couponManager.conditionToSave = $.couponManager.conditionsToSave[id];
|
||||
|
||||
// Set the rule selector
|
||||
// Set the condition selector
|
||||
$("#category-rule option").filter(function() {
|
||||
return $(this).val() == $.couponManager.ruleToSave.serviceId;
|
||||
return $(this).val() == $.couponManager.conditionToSave.serviceId;
|
||||
}).prop('selected', true);
|
||||
|
||||
// Force rule input refresh
|
||||
$.couponManager.loadRuleInputs($.couponManager.ruleToSave.serviceId, function() {
|
||||
$.couponManager.fillInRuleInputs();
|
||||
// Force condition input refresh
|
||||
$.couponManager.loadConditionInputs($.couponManager.conditionToSave.serviceId, function() {
|
||||
$.couponManager.fillInConditionInputs();
|
||||
});
|
||||
};
|
||||
|
||||
// Fill in rule inputs
|
||||
$.couponManager.fillInRuleInputs = function() {
|
||||
// Fill in condition inputs
|
||||
$.couponManager.fillInConditionInputs = function() {
|
||||
var operatorId = null;
|
||||
var valueId = null;
|
||||
var idName = null;
|
||||
|
||||
var id = $.couponManager.ruleToUpdateId;
|
||||
var id = $.couponManager.conditionToUpdateId;
|
||||
if(id) {
|
||||
$.couponManager.ruleToSave = $.couponManager.rulesToSave[id];
|
||||
$.couponManager.conditionToSave = $.couponManager.conditionsToSave[id];
|
||||
}
|
||||
|
||||
for (idName in $.couponManager.ruleToSave.operators) {
|
||||
for (idName in $.couponManager.conditionToSave.operators) {
|
||||
// Setting idName operator select
|
||||
operatorId = idName + '-operator';
|
||||
$('#' + operatorId).val($.couponManager.ruleToSave.operators[idName]);
|
||||
$('#' + operatorId).val($.couponManager.conditionToSave.operators[idName]);
|
||||
|
||||
// Setting idName value input
|
||||
valueId = idName + '-value';
|
||||
$('#' + valueId).val($.couponManager.ruleToSave.values[idName]);
|
||||
$('#' + valueId).val($.couponManager.conditionToSave.values[idName]);
|
||||
}
|
||||
};
|
||||
|
||||
// Save rules on click
|
||||
$.couponManager.onClickSaveRule = function() {
|
||||
// Save conditions on click
|
||||
$.couponManager.onClickSaveCondition = function() {
|
||||
$('#constraint-save-btn').on('click', function () {
|
||||
console.log('constraint-save-btn');
|
||||
if($('#category-rule').val() == 'thelia.condition.match_for_everyone') {
|
||||
// // @todo translate + modal
|
||||
// // @todo translate message + put it in modal
|
||||
var r = confirm("Do you really want to set this coupon available to everyone ?");
|
||||
if (r == true) {
|
||||
$.couponManager.createOrUpdateRuleAjax();
|
||||
$.couponManager.createOrUpdateConditionAjax();
|
||||
}
|
||||
} else {
|
||||
$.couponManager.createOrUpdateRuleAjax();
|
||||
$.couponManager.createOrUpdateConditionAjax();
|
||||
}
|
||||
|
||||
});
|
||||
};
|
||||
$.couponManager.onClickSaveRule();
|
||||
$.couponManager.onClickSaveCondition();
|
||||
|
||||
// Remove rule on click
|
||||
$.couponManager.onClickDeleteRule = function() {
|
||||
// Remove condition on click
|
||||
$.couponManager.onClickDeleteCondition = function() {
|
||||
$('.constraint-delete-btn').on('click', function (e) {
|
||||
e.preventDefault();
|
||||
var $this = $(this);
|
||||
$.couponManager.removeRuleAjax($this.attr('data-int'));
|
||||
$.couponManager.removeConditionAjax($this.attr('data-int'));
|
||||
});
|
||||
};
|
||||
$.couponManager.onClickDeleteRule();
|
||||
$.couponManager.onClickDeleteCondition();
|
||||
|
||||
// Update rule on click
|
||||
$.couponManager.onClickUpdateRule = function() {
|
||||
$('.constraint-update-btn').on('click', function (e) {
|
||||
// Update condition on click
|
||||
$.couponManager.onClickUpdateCondition = function() {
|
||||
$('.condition-update-btn').on('click', function (e) {
|
||||
e.preventDefault();
|
||||
var $this = $(this);
|
||||
$.couponManager.updateRuleSelectAjax($this.attr('data-int'));
|
||||
$.couponManager.updateConditionSelectAjax($this.attr('data-int'));
|
||||
|
||||
// Hide row being updated
|
||||
$this.parent().parent().remove();
|
||||
});
|
||||
};
|
||||
$.couponManager.onClickUpdateRule();
|
||||
$.couponManager.onClickUpdateCondition();
|
||||
|
||||
// Reload effect inputs when changing effect
|
||||
$.couponManager.onEffectChange = function() {
|
||||
@@ -139,15 +138,15 @@ $(function($){
|
||||
};
|
||||
$.couponManager.onEffectChange();
|
||||
|
||||
// Reload rule inputs when changing effect
|
||||
$.couponManager.onRuleChange = function() {
|
||||
// Reload condition inputs when changing effect
|
||||
$.couponManager.onConditionChange = function() {
|
||||
$('#category-rule').on('change', function () {
|
||||
$.couponManager.loadRuleInputs($(this).val(), function() {});
|
||||
$.couponManager.loadConditionInputs($(this).val(), function() {});
|
||||
});
|
||||
};
|
||||
$.couponManager.onRuleChange();
|
||||
$.couponManager.onConditionChange();
|
||||
|
||||
// Fill in ready to be saved rule array
|
||||
// Fill in ready to be saved condition array
|
||||
// var onInputsChange = function()
|
||||
// In AJAX response
|
||||
|
||||
|
||||
Reference in New Issue
Block a user