diff --git a/src/api/wishlists.php b/src/api/wishlists.php index d174b9f9..a6e2d94b 100644 --- a/src/api/wishlists.php +++ b/src/api/wishlists.php @@ -21,13 +21,13 @@ switch ($_SERVER['REQUEST_METHOD']) { $user = isset($_GET['userid']) ? new User($_GET['userid']) : new User(); $wishlists = $user->getWishlists(); $wishlists = array_map(function ($wishlist) { - return array( - 'name' => $wishlist['name'], - 'value' => $wishlist['id'], - 'text' => $wishlist['name'], - ); - }, $wishlists); + $newFormat = $wishlist; + // $newFormat['name'] = $wishlist['name']; + $newFormat['value'] = $wishlist['id']; + $newFormat['text'] = $wishlist['name']; + return $newFormat; + }, $wishlists); $response['results'] = $wishlists; $response['success'] = true; } diff --git a/src/assets/js/default.js b/src/assets/js/default.js index 4ba2b327..0dadd5d9 100644 --- a/src/assets/js/default.js +++ b/src/assets/js/default.js @@ -9,6 +9,90 @@ $(function() { 'delete product' : '/src/api/products.php', }; + /** Default callbacks */ + $.fn.api.settings.onResponse = function(response) { + return response; + } + $.fn.api.settings.successTest = function(response) { + return response.status == 'OK' || response.success || false; + } + $.fn.api.settings.onComplete = function(response, element, xhr) { + element.removeClass('loading'); + } + $.fn.api.settings.onSuccess = function(response, element, xhr) { + element.dropdown({ + values: response.results, + placeholder: 'No wishlist selected.' + }) + + const urlParams = new URLSearchParams(window.location.search); + + if (urlParams.has('wishlist')) { + element.dropdown('set selected', urlParams.get('wishlist')); + } else { + if (response.results[0]) { + element.dropdown('set selected', response.results[0].value); + } + } + } + $.fn.api.settings.onFailure = function(response, element, xhr) { + console.log(response); + console.log(element); + console.log(xhr); + + $('body') + .modal({ + title: 'Failure', + content: 'The process has failed.', + class: '', + actions: [ + { + text: 'Oh well', + class: 'primary' + } + ] + }) + .modal('show'); + } + $.fn.api.settings.onError = function(response, element, xhr) { + console.log(response); + console.log(element); + console.log(xhr); + + $('body') + .modal({ + title: 'Error', + content: 'There has been an error.', + class: '', + actions: [ + { + text: 'Oh well', + class: 'primary' + } + ] + }) + .modal('show'); + } + $.fn.api.settings.onAbort = function(response, element, xhr) { + console.log(response); + console.log(element); + console.log(xhr); + + $('body') + .modal({ + title: 'Interrupted', + content: 'The process was interrupted.', + class: '', + actions: [ + { + text: 'Oh well', + class: 'primary' + } + ] + }) + .modal('show'); + } + $('.ui.dropdown.wishlists').dropdown({ filterRemoteData: true }); @@ -16,7 +100,7 @@ $(function() { /** * Refresh Wishlist */ - wishlistRefresh(); + // wishlistRefresh(); /** * Commit to Product @@ -151,41 +235,6 @@ function wishlistRefresh() { $('.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) { - $('.ui.dropdown.wishlists').dropdown({ - values: response.results, - placeholder: 'No wishlist selected.' - }) - - if (urlParams.has('wishlist')) { - $('.ui.dropdown.wishlists').dropdown('set selected', urlParams.get('wishlist')); - } else { - if (response.results[0]) { - $('.ui.dropdown.wishlists').dropdown('set selected', response.results[0].value); - } - } - }, - 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 - } + on: 'now' }); } diff --git a/src/assets/js/wishlist-product-add.js b/src/assets/js/wishlist-product-add.js new file mode 100644 index 00000000..32e287d0 --- /dev/null +++ b/src/assets/js/wishlist-product-add.js @@ -0,0 +1,7 @@ +$(function() { + $('.ui.dropdown.wishlists').api({ + action: 'get wishlists', + method: 'GET', + on: 'now' + }); +}); diff --git a/src/assets/js/wishlist-view.js b/src/assets/js/wishlist-view.js index 8417ef8e..bacfe664 100644 --- a/src/assets/js/wishlist-view.js +++ b/src/assets/js/wishlist-view.js @@ -1,9 +1,39 @@ $(function() { + /** + * Get Wishlists + */ + var wishlists = []; + + $('.ui.dropdown.wishlists').api({ + action: 'get wishlists', + method: 'GET', + on: 'now', + onSuccess: function(response, element, xhr) { + wishlists = response.results; + + element.dropdown({ + values: wishlists, + placeholder: 'No wishlist selected.' + }) + + const urlParams = new URLSearchParams(window.location.search); + + if (urlParams.has('wishlist')) { + element.dropdown('set selected', urlParams.get('wishlist')); + } else { + if (wishlists[0]) { + element.dropdown('set selected', wishlists[0].value); + } + } + } + }); + /** * Selection */ $('.ui.dropdown.wishlists').on('change', function() { var wishlistValue = $('.ui.dropdown.wishlists').dropdown('get value'); + var wishlistIndex = $('.ui.dropdown.wishlists select').prop('selectedIndex') - 1; $('[name="wishlist_delete_id"]').val(wishlistValue); @@ -14,6 +44,8 @@ $(function() { urlParams.set('wishlist', wishlistValue); window.history.pushState({}, '', '/?' + urlParams.toString()); + $('.wishlist-share').attr('href', '/?wishlist=' + wishlists[wishlistIndex].hash); + $('.wishlist-share').removeClass('disabled'); $('.wishlist-delete button').removeClass('disabled'); } else {