From f7e695e0948363c2db96e0bd0d4aa413cd617024 Mon Sep 17 00:00:00 2001 From: grandeljay Date: Wed, 14 Dec 2022 16:45:55 +0100 Subject: [PATCH] Extend wishlists api response --- src/api/wishlists.php | 9 ++++++--- src/assets/js/parts/wishlists.js | 19 +++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/api/wishlists.php b/src/api/wishlists.php index 5900919f..5c44a1b3 100644 --- a/src/api/wishlists.php +++ b/src/api/wishlists.php @@ -135,19 +135,22 @@ switch ($_SERVER['REQUEST_METHOD']) { */ $user = isset($_GET['userid']) ? User::getFromID($_GET['userid']) : $_SESSION['user']; - $wishlists = array(); + $wishlists = array(); + $wishlists_items = array(); foreach ($user->getWishlists() as $wishlist_result) { $wishlist = new Wishlist($wishlist_result['id']); - $wishlists[$wishlist->id] = array( + $wishlists[$wishlist->id] = $wishlist; + $wishlists_items[$wishlist->id] = array( 'name' => $wishlist->name, 'value' => $wishlist->id, 'text' => $wishlist->name, ); } - $response['results'] = $wishlists; + $response['wishlists'] = $wishlists; + $response['wishlists_items'] = $wishlists_items; } break; diff --git a/src/assets/js/parts/wishlists.js b/src/assets/js/parts/wishlists.js index d1f69852..7fa844fc 100644 --- a/src/assets/js/parts/wishlists.js +++ b/src/assets/js/parts/wishlists.js @@ -1,18 +1,21 @@ +var wishlists = []; + $(function () { /** * Get Wishlists */ - var wishlists = []; - var wishlists_api = { + var wishlists_items = []; + var wishlists_api = { 'action' : 'get wishlists', 'onSuccess' : function(response, dropdown_wishlists, xhr) { /** Save response for later use */ - wishlists = response.results; + wishlists = response.wishlists; + wishlists_items = response.wishlists_items; /** Setup and populate dropdown */ var dropdown_values = { - 'values' : wishlists, + 'values' : wishlists_items, }; dropdown_wishlists.dropdown('setup menu', dropdown_values); @@ -704,17 +707,21 @@ $(function () { */ function setDropdownWishlistsSelection() { var dropdown_wishlists = $('.ui.dropdown.wishlists'); + var wishlist_id; if (!dropdown_wishlists.dropdown('get value')) { if (wishthis.$_GET.id) { - dropdown_wishlists.dropdown('set selected', wishthis.$_GET.id); + wishlist_id = wishthis.$_GET.id; } else { if (Object.keys(wishlists).length >= 1) { var first_wishlist_id = Object.keys(wishlists)[0]; - dropdown_wishlists.dropdown('set selected', first_wishlist_id); + wishlist_id = first_wishlist_id; } } + + wishlist = wishlists[wishlist_id]; + dropdown_wishlists.dropdown('set selected', wishlist.id); } }