Allow toggling wishlist save
This commit is contained in:
parent
1cc48ca4be
commit
ff4c5bed2d
5 changed files with 140 additions and 8 deletions
70
src/api/wishlists-saved.php
Normal file
70
src/api/wishlists-saved.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* wishlists-saved.php
|
||||
*
|
||||
* @author Jay Trees <github.jay@grandel.anonaddy.me>
|
||||
*/
|
||||
|
||||
$api = true;
|
||||
$response = array();
|
||||
|
||||
ob_start();
|
||||
|
||||
require '../../index.php';
|
||||
|
||||
switch ($_SERVER['REQUEST_METHOD']) {
|
||||
case 'GET':
|
||||
/**
|
||||
* Get
|
||||
*/
|
||||
$wishlists = $database
|
||||
->query('SELECT *
|
||||
FROM `wishlists_saved` `ws`
|
||||
JOIN `wishlists` `w` ON `w`.`id` = `ws`.`wishlist`
|
||||
WHERE `ws`.`user` = ' . $user->id . ';')
|
||||
->fetchAll();
|
||||
|
||||
$response['data'] = $wishlists;
|
||||
break;
|
||||
|
||||
case 'POST':
|
||||
if (isset($_POST['wishlist'])) {
|
||||
$wishlist = $database
|
||||
->query('SELECT *
|
||||
FROM `wishlists_saved`
|
||||
WHERE `wishlist` = ' . $_POST['wishlist'] . '
|
||||
;')
|
||||
->fetch();
|
||||
|
||||
if ($wishlist) {
|
||||
/** Delete */
|
||||
$database
|
||||
->query('DELETE FROM `wishlists_saved`
|
||||
WHERE `wishlist` = ' . $_POST['wishlist'] . '
|
||||
;');
|
||||
|
||||
$response['action'] = 'deleted';
|
||||
} else {
|
||||
/** Create */
|
||||
$database
|
||||
->query('INSERT INTO `wishlists_saved` (
|
||||
`user`,
|
||||
`wishlist`
|
||||
) VALUES (
|
||||
' . $user->id . ',
|
||||
' . $_POST['wishlist'] . '
|
||||
)
|
||||
;');
|
||||
|
||||
$response['action'] = 'created';
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$response['warning'] = ob_get_clean();
|
||||
|
||||
header('Content-type: application/json; charset=utf-8');
|
||||
echo json_encode($response);
|
||||
die();
|
|
@ -80,13 +80,65 @@ $(function() {
|
|||
* Save wishlist
|
||||
*/
|
||||
$(document).on('click', '.button.save', function() {
|
||||
$(this).addClass('disabled loading');
|
||||
var buttonSave = $(this);
|
||||
|
||||
setTimeout(() => {
|
||||
$(this).find('.icon').addClass('red');
|
||||
$(this).find('span').text(text.button_wishlist_saved);
|
||||
$(this).removeClass('disabled loading');
|
||||
}, 400);
|
||||
buttonSave.addClass('disabled loading');
|
||||
|
||||
var formData = new URLSearchParams();
|
||||
formData.append('wishlist', $('[data-wishlist]').attr('data-wishlist'));
|
||||
|
||||
fetch('/src/api/wishlists-saved.php', {
|
||||
method : 'POST',
|
||||
body : formData
|
||||
})
|
||||
.then(handleFetchError)
|
||||
.then(handleFetchResponse)
|
||||
.then(function(response) {
|
||||
switch (response.action) {
|
||||
case 'created':
|
||||
button_set_saved_state(buttonSave);
|
||||
break;
|
||||
|
||||
case 'deleted':
|
||||
button_set_default_state(buttonSave);
|
||||
break;
|
||||
}
|
||||
|
||||
buttonSave.removeClass('disabled loading');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
/** Determine if list is saved */
|
||||
fetch('/src/api/wishlists-saved.php', {
|
||||
method : 'GET',
|
||||
})
|
||||
.then(handleFetchError)
|
||||
.then(handleFetchResponse)
|
||||
.then(function(response) {
|
||||
var wishlists = response.data;
|
||||
var buttonSave = $('.button.save');
|
||||
|
||||
wishlists.forEach(wishlist => {
|
||||
if (wishlist.hash == $_GET.wishlist) {
|
||||
button_set_saved_state(buttonSave);
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
buttonSave.removeClass('disabled loading');
|
||||
});
|
||||
|
||||
/** Set default state */
|
||||
function button_set_default_state(buttonSave) {
|
||||
buttonSave.find('.icon').removeClass('red');
|
||||
buttonSave.find('span').text(text.button_wishlist_save);
|
||||
}
|
||||
|
||||
/** Set saved state */
|
||||
function button_set_saved_state(buttonSave) {
|
||||
buttonSave.find('.icon').addClass('red');
|
||||
buttonSave.find('span').text(text.button_wishlist_saved);
|
||||
}
|
||||
|
||||
});
|
||||
|
|
4
src/assets/js/wishlists-saved.js
Normal file
4
src/assets/js/wishlists-saved.js
Normal file
|
@ -0,0 +1,4 @@
|
|||
$(function() {
|
||||
|
||||
|
||||
});
|
|
@ -380,6 +380,7 @@ class Page
|
|||
calendar_pm : '<?= _x('PM', 'Calendar') ?>',
|
||||
calendar_week_no : '<?= _x('Week', 'Calendar') ?>',
|
||||
|
||||
button_wishlist_save : '<?= __('Save') ?>',
|
||||
button_wishlist_saved : '<?= __('Saved') ?>',
|
||||
};
|
||||
</script>
|
||||
|
@ -470,7 +471,12 @@ class Page
|
|||
'text' => __('My lists'),
|
||||
'url' => '/?page=wishlists',
|
||||
'icon' => 'list',
|
||||
)
|
||||
),
|
||||
array(
|
||||
'text' => __('Saved lists'),
|
||||
'url' => '/?page=wishlists-saved',
|
||||
'icon' => 'heart',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ $page->navigation();
|
|||
<div class="column">
|
||||
|
||||
<?php if ($user->isLoggedIn() && $user->id !== intval($wishlist->user)) { ?>
|
||||
<button class="ui white small basic labeled icon button save">
|
||||
<button class="ui white small basic labeled icon button save disabled loading">
|
||||
<i class="heart icon"></i>
|
||||
<span><?= __('Save list') ?></span>
|
||||
</button>
|
||||
|
|
Loading…
Reference in a new issue