wishthis/src/assets/js/wish.js

148 lines
4.9 KiB
JavaScript
Raw Normal View History

2022-02-27 11:23:38 +00:00
$(function () {
/**
2022-03-04 10:03:12 +00:00
* Auto fill
2022-02-27 11:23:38 +00:00
*/
2022-03-04 10:03:12 +00:00
if ($('[name="wish_url"]').val()) {
$('.button.auto-fill').removeClass('disabled');
}
$(document).on('click', '.button.auto-fill', function () {
2022-02-27 11:23:38 +00:00
var modalAutoFill = $('.modal.auto-fill');
var modalValidate = $('.modal.validate');
var formWish = $('.form.wish');
var inputTitle = $('[name="wish_title"]');
var inputDescription = $('[name="wish_description"]');
var inputURL = $('[name="wish_url"]');
modalAutoFill
.modal({
onApprove: function(buttonApprove) {
formWish.addClass('loading');
fetch('/src/api/wishes.php?wish_url=' + inputURL.val(), {
method: 'GET'
})
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
var info = response.info;
2022-02-27 11:33:02 +00:00
/**
* Prodiver name
*/
if (info.providerName) {
modalValidate.find('.providerName').text(info.providerName);
} else {
modalValidate.find('.provider').remove();
}
2022-02-27 11:23:38 +00:00
/**
* Title
*/
if (info.title) {
inputTitle.val(info.title);
}
/**
* Description
*/
if (info.description) {
inputDescription.val(info.description);
}
/**
* URL
*/
if (info.url && info.url !== inputURL.val()) {
var elementModalFetch = $('.modal.validate');
elementModalFetch.find('input.current').val(inputURL.val());
elementModalFetch.find('input.proposed').val(info.url);
elementModalFetch
.modal({
onApprove: function (buttonFetch) {
var formData = new URLSearchParams();
formData.append('wish_url_current', inputURL.val());
formData.append('wish_url_proposed', info.url);
buttonFetch.addClass('loading');
fetch('/src/api/wishes.php', {
method: 'PUT',
body: formData
})
.then(response => response.json())
.then(response => {
if (response.success) {
inputURL.val(info.url);
elementModalFetch.modal('hide');
2022-02-27 14:07:07 +00:00
$('body').toast({ message: 'Wish information updated.' });
2022-02-27 11:23:38 +00:00
}
buttonFetch.removeClass('loading');
});
return false;
},
onHide: function() {
formWish.removeClass('loading');
}
})
.modal('show');
} else {
2022-02-27 14:07:07 +00:00
$('body').toast({ message: 'Wish information updated.' });
2022-02-27 11:33:02 +00:00
2022-02-27 11:23:38 +00:00
formWish.removeClass('loading');
}
})
2022-03-05 09:50:21 +00:00
.catch(handleFetchCatch);
2022-02-27 11:23:38 +00:00
}
})
.modal('show');
});
2022-02-27 14:07:07 +00:00
/**
* Image
*/
$(document).on('click', '.image.preview', function() {
2022-03-04 08:32:56 +00:00
var modalImage = $('.modal.preview');
2022-02-27 14:07:07 +00:00
modalImage
.modal({
onApprove: function(buttonApprove) {
2022-03-04 08:32:56 +00:00
var formImage = modalImage.find('form.preview');
2022-02-27 14:07:07 +00:00
var formData = new URLSearchParams(new FormData(formImage[0]));
formImage.addClass('loading');
fetch('/src/api/wishes.php', {
method: 'PUT',
body: formData
})
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
var elementImage = $('.form.wish img.preview');
elementImage.attr('src', response.wish_url);
formImage.removeClass('loading');
modalImage.modal('hide');
$('body').toast({ message: 'Wish image successfully updated.' });
});
return false;
}
})
.modal('show');
});
2022-02-27 11:23:38 +00:00
});