wishthis/includes/assets/js/default.js

188 lines
5.6 KiB
JavaScript
Raw Normal View History

$(function() {
/**
* Fomantic UI
*/
2022-01-17 11:40:53 +00:00
$.fn.api.settings.api = {
2022-01-20 12:45:09 +00:00
'get wishlists' : '/includes/api/wishlists.php',
'delete wishlist' : '/includes/api/wishlists.php',
'update product status': '/includes/api/products.php',
2022-01-20 13:33:06 +00:00
'delete product' : '/includes/api/products.php',
2022-01-17 11:40:53 +00:00
};
2022-01-17 11:40:53 +00:00
$('.ui.dropdown.wishlists').dropdown({
filterRemoteData: true
});
2022-01-20 13:33:06 +00:00
/**
* Refresh Wishlist
*/
2022-01-17 15:06:17 +00:00
wishlistRefresh();
2022-01-20 13:33:06 +00:00
/**
* Commit to Product
*/
$('.ui.button.commit').on('click', function() {
var button = $(this);
var card = button.closest('.ui.card');
var column = card.closest('.column');
$('body')
.modal({
title: 'Really commit?',
content: 'Would you really like to commit to this purchase? It will no longer appear in the wishlist anymore.',
class: 'tiny',
actions: [
{
text: 'Yes, commit',
class: 'approve primary'
},
{
text: 'Cancel',
class: ''
}
],
onApprove: function() {
/**
* Update product status
*/
button.api({
action: 'update product status',
method: 'PUT',
data: {
productID: card.data('id'),
productStatus: 'unavailable'
},
on: 'now',
onResponse: function(response) {
return response;
},
successTest: function(response) {
return response.success || false;
},
onComplete: function(response, element, xhr) {
},
onSuccess: function(response, element, xhr) {
column.fadeOut();
},
onFailure: function(response, element, xhr) {
},
onError: function(errorMessage, element, xhr) {
},
onAbort: function(errorMessage, element, xhr) {
}
});
}
})
.modal('show');
});
/**
* Delete Product
*/
$('.ui.button.delete').on('click', function() {
var button = $(this);
var card = button.closest('.ui.card');
var column = card.closest('.column');
$('body')
.modal({
title: 'Really delete?',
content: 'Would you really like to delete to this product? It will be gone forever.',
class: 'tiny',
actions: [
{
text: 'Yes, delete',
class: 'approve primary'
},
{
text: 'Cancel',
class: ''
}
],
onApprove: function() {
/**
* Delete product
*/
button.api({
action: 'delete product',
method: 'DELETE',
data: {
productID: card.data('id'),
},
on: 'now',
onResponse: function(response) {
return response;
},
successTest: function(response) {
return response.success || false;
},
onComplete: function(response, element, xhr) {
},
onSuccess: function(response, element, xhr) {
column.fadeOut();
},
onFailure: function(response, element, xhr) {
},
onError: function(errorMessage, element, xhr) {
},
onAbort: function(errorMessage, element, xhr) {
}
});
}
})
.modal('show');
});
2022-01-17 15:06:17 +00:00
});
function wishlistRefresh() {
2022-01-18 11:16:00 +00:00
/**
* URL Parameter
*/
const urlParams = new URLSearchParams(window.location.search);
2022-01-17 11:40:53 +00:00
$('.ui.dropdown.wishlists').api({
action: 'get wishlists',
method: 'GET',
on: 'now',
onResponse: function(response) {
return response;
},
successTest: function(response) {
return response.success || false;
},
onComplete: function(response, element, xhr) {
$('.ui.dropdown.wishlists').removeClass('loading');
},
onSuccess: function(response, element, xhr) {
2022-01-17 12:51:49 +00:00
$('.ui.dropdown.wishlists').dropdown({
2022-01-17 14:21:38 +00:00
values: response.results,
placeholder: 'No wishlist selected.'
2022-01-17 11:40:53 +00:00
})
if (urlParams.has('wishlist')) {
$('.ui.dropdown.wishlists').dropdown('set selected', urlParams.get('wishlist'));
}
2022-01-17 11:40:53 +00:00
},
onFailure: function(response, element, xhr) {
console.log('onFailure');
// request failed, or valid response but response.success = false
},
onError: function(errorMessage, element, xhr) {
console.log('onError');
// invalid response
},
onAbort: function(errorMessage, element, xhr) {
console.log('onAbort');
// navigated to a new page, CORS issue, or user canceled request
}
});
2022-01-17 15:06:17 +00:00
}