Skip to content

Instantly share code, notes, and snippets.

@wolfcoder
Created June 8, 2022 05:39
Show Gist options
  • Save wolfcoder/c403af824686c1a14c9be55afd76eaf2 to your computer and use it in GitHub Desktop.
Save wolfcoder/c403af824686c1a14c9be55afd76eaf2 to your computer and use it in GitHub Desktop.
VIN article js
var vapSyncCategories = [];
var vapAllSuccess = true;
var vapRunning = false;
function vapGetCategoryArticles(categoryIndex, pageIndex) {
var post_data = {
action: 'vap_sync_start',
on_sync: true,
category_id: vapSyncCategories[categoryIndex].id,
category_wpid: vapSyncCategories[categoryIndex].wpid,
page_index: pageIndex,
timestamp: jQuery('#vap_syn_tstamp').val(),
};
if (pageIndex == 1) {
jQuery('#vapProcessMessage').append('<p>Importing articles from category: ' + vapSyncCategories[categoryIndex].name + '... [ Importing ]</p>');
}
jQuery.ajax({
type: 'POST',
url: vap_ajax_object.ajax_url,
data: post_data,
dataType : 'json',
success: function(response) {
if (response.success) {
if (response.result.processed == 0) {
jQuery('#vapProcessMessage p').last().html('<p>Importing articles from category: ' + vapSyncCategories[categoryIndex].name + '... [ Done ]</p>');
vapSyncNextCategory(categoryIndex, pageIndex, false);
} else {
if (vapRunning) {
vapGetCategoryArticles(categoryIndex, (pageIndex + 1));
} else {
jQuery('#vapProcessMessage p').last().html('<p>Importing articles from category: ' + vapSyncCategories[categoryIndex].name + '... [ Done ]</p>');
vapSetViewOnStop('Sync / import process is ended.');
}
}
} else {
vapOnError(categoryIndex, pageIndex);
}
},
error: function(response) {
vapOnError(categoryIndex, pageIndex);
}
});
}
function vapSendFinalSignal() {
var post_data = {
action: 'vap_sync_final_stop'
};
jQuery.ajax({
type: 'POST',
url: vap_ajax_object.ajax_url,
data: post_data,
dataType : 'json',
success: function(response) {
jQuery('#vapProcessMessage').append('<p>Saving last sync configuration... [ ' + (response.success ? 'Done' : 'Error') + ' ]</p>');
vapSetViewOnStop('Sync / import process is ended.');
},
error: function(response) {
jQuery('#vapProcessMessage').append('<p>Saving last sync configuration... [ Error ]</p>');
vapSetViewOnStop('Sync / import process is ended.');
}
});
}
function vapOnError(categoryIndex, pageIndex) {
jQuery('#vapProcessMessage p').last().html('<p>Importing articles from category: ' + vapSyncCategories[categoryIndex].name + '... [ Error ]</p>');
vapSyncNextCategory(categoryIndex, pageIndex, true);
}
function vapSyncNextCategory(categoryIndex, pageIndex, error) {
if (vapRunning) {
var isLast = vapSyncCategories.length <= (categoryIndex + 1);
vapAllSuccess = vapAllSuccess && !error;
if (isLast) {
if (!vapAllSuccess) {
vapSetViewOnStop('Sync / import process is ended.');
} else {
vapSendFinalSignal();
}
} else {
vapGetCategoryArticles((categoryIndex + 1), 1);
}
} else {
vapSetViewOnStop('Sync / import process is ended.');
}
}
function vapStartProcess() {
jQuery('#vapProcessHeading').html('Starting...');
jQuery('#lnk_vap_sync').data('mode', 1);
jQuery('#lnk_vap_sync').html('Stop Process');
jQuery('#loader_vap_sync').show();
vapRunning = true;
var lsCategory = parseInt(jQuery('#vap_syn_cat').val());
var lsPage = parseInt(jQuery('#vap_syn_pg').val());
var post_data = {
action: 'vap_sync_categories',
};
jQuery.ajax({
type: 'POST',
url: vap_ajax_object.ajax_url,
data: post_data,
dataType : 'json',
success: function(response) {
if (response.success) {
vapSyncCategories = response.result;
if (lsCategory == 0) {
vapGetCategoryArticles(0, 1);
} else {
var catIndex = vapSyncCategories.map(function(item) { return item.id; }).indexOf(lsCategory);
vapGetCategoryArticles(catIndex, lsPage);
}
} else {
vapSetViewOnStop('Something went wrong during sync / import process.');
}
},
error: function(response) {
vapSetViewOnStop('Something went wrong during sync / import process.');
}
});
}
function vapStopProcess() {
jQuery('#vapProcessHeading').html('Stopping...');
jQuery('#lnk_vap_sync').prop('disabled', true);
vapRunning = false;
}
function vapSetViewOnStop(message) {
jQuery('#vap_message_content').html(message);
jQuery('#vap_message').show();
jQuery('#loader_vap_sync').hide();
jQuery('#lnk_vap_sync').html('Start Sync / Import Article');
jQuery('#lnk_vap_sync').prop('disabled', false);
jQuery('#lnk_vap_sync').data('mode', 0);
jQuery('#vapProcessHeading').html('Stopped...');
}
jQuery(function($){
$('#lnk_vap_sync').on('click', function(){
if ($(this).data('mode') != 1) {
vapStartProcess();
} else {
if (confirm("Are you sure you want to stop the process!") == true) {
vapStopProcess();
}
}
return false;
});
$('#lnk_vap_cfg_reset').on('click', function() {
if (confirm("Are you sure you want to delete the last sync configuration!") == true) {
var post_data = {
action: 'vap_setting_dellastsync',
};
jQuery.ajax({
type: 'POST',
url: vap_ajax_object.ajax_url,
data: post_data,
dataType : 'json',
success: function(response) {
if (response.success) {
jQuery('#vap_message_content').html('Last sync configuration data has been reset.');
} else {
jQuery('#vap_message_content').html('Something went wrong during the process.');
}
jQuery('#vap_message').show();
},
error: function(response) {
jQuery('#vap_message_content').html('Something went wrong during the process.');
jQuery('#vap_message').show();
}
});
}
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment