From 8913337de7afbac5a26c4f987eeee69a5b750ed7 Mon Sep 17 00:00:00 2001 From: Jay Trees Date: Mon, 17 Jan 2022 16:06:17 +0100 Subject: [PATCH] Add wishlist-delete --- includes/api/wishlists.php | 10 ++++ includes/assets/js/default.js | 12 +++-- includes/assets/js/wishlist-view.js | 74 +++++++++++++++++++++++++++-- includes/pages/wishlist-view.php | 6 +-- 4 files changed, 91 insertions(+), 11 deletions(-) diff --git a/includes/api/wishlists.php b/includes/api/wishlists.php index 63b1387c..0bf4be8a 100644 --- a/includes/api/wishlists.php +++ b/includes/api/wishlists.php @@ -32,6 +32,16 @@ switch ($_SERVER['REQUEST_METHOD']) { $response['success'] = true; } break; + + case 'DELETE': + parse_str(file_get_contents("php://input"), $_DELETE); + + $database->query('DELETE FROM `wishlists` + WHERE `id` = ' . $_DELETE['wishlistID'] . ' + ;'); + + $response['success'] = true; + break; } echo json_encode($response); diff --git a/includes/assets/js/default.js b/includes/assets/js/default.js index f027b1c7..f5733374 100644 --- a/includes/assets/js/default.js +++ b/includes/assets/js/default.js @@ -8,24 +8,26 @@ $(function() { * Fomantic UI */ $.fn.api.settings.api = { - 'get wishlists' : '/includes/api/wishlists.php' + 'get wishlists' : '/includes/api/wishlists.php', + 'delete wishlist' : '/includes/api/wishlists.php' }; $('.ui.dropdown.wishlists').dropdown({ filterRemoteData: true }); + wishlistRefresh(); +}); + +function wishlistRefresh() { $('.ui.dropdown.wishlists').api({ action: 'get wishlists', method: 'GET', on: 'now', onResponse: function(response) { - console.log('onResponse'); - // make some adjustments to response return response; }, successTest: function(response) { - console.log('successTest'); return response.success || false; }, onComplete: function(response, element, xhr) { @@ -54,4 +56,4 @@ $(function() { // navigated to a new page, CORS issue, or user canceled request } }); -}); +} diff --git a/includes/assets/js/wishlist-view.js b/includes/assets/js/wishlist-view.js index 4231b028..df0d476a 100644 --- a/includes/assets/js/wishlist-view.js +++ b/includes/assets/js/wishlist-view.js @@ -1,7 +1,75 @@ $(function() { + /** + * Delete Wishlist + */ $('.ui.dropdown.wishlists').on('change', function() { - $('[name="wishlist_delete_id"]').val( - $('.ui.dropdown.wishlists').dropdown('get value') - ); + var wishlistValue = $('.ui.dropdown.wishlists').dropdown('get value'); + + $('[name="wishlist_delete_id"]').val(wishlistValue); + + if (wishlistValue) { + $('.wishlist-view').removeClass('disabled'); + $('.wishlist-delete button').removeClass('disabled'); + } else { + $('.wishlist-view').addClass('disabled'); + $('.wishlist-delete button').addClass('disabled'); + } + }); + + $('.wishlist-delete').on('submit', function(event) { + var wishlistValue = $('.ui.dropdown.wishlists').dropdown('get value'); + + if (wishlistValue) { + $('body') + .modal({ + title: 'Really delete?', + class: 'tiny', + content: 'Do you really want to delete the wishlist ' + $('.ui.dropdown.wishlists').dropdown('get text') + '?', + actions: [ + { + text: 'Yes, delete', + class: 'approve red' + }, + { + text: 'No, keep', + class: 'deny' + }, + ], + onApprove: function() { + $('.ui.dropdown.wishlists').api({ + action: 'delete wishlist', + method: 'DELETE', + data: { + wishlistID: wishlistValue + }, + on: 'now', + onResponse: function(response) { + return response; + }, + successTest: function(response) { + return response.success || false; + }, + onComplete: function(response, element, xhr) { + + }, + onSuccess: function(response, element, xhr) { + wishlistRefresh(); + }, + onFailure: function(response, element, xhr) { + + }, + onError: function(errorMessage, element, xhr) { + + }, + onAbort: function(errorMessage, element, xhr) { + + } + }); + } + }) + .modal('show'); + } + + event.preventDefault(); }); }); diff --git a/includes/pages/wishlist-view.php b/includes/pages/wishlist-view.php index 5bfa9c48..9d48ec02 100644 --- a/includes/pages/wishlist-view.php +++ b/includes/pages/wishlist-view.php @@ -51,7 +51,7 @@ if (isset($_POST['wishlist_delete_id'])) { - + @@ -64,10 +64,10 @@ if (isset($_POST['wishlist_delete_id'])) { Share -
+ -