Extend wishlists api response

This commit is contained in:
grandeljay 2022-12-14 16:45:55 +01:00
parent e374030f8b
commit f7e695e094
2 changed files with 19 additions and 9 deletions

View file

@ -135,19 +135,22 @@ switch ($_SERVER['REQUEST_METHOD']) {
*/ */
$user = isset($_GET['userid']) ? User::getFromID($_GET['userid']) : $_SESSION['user']; $user = isset($_GET['userid']) ? User::getFromID($_GET['userid']) : $_SESSION['user'];
$wishlists = array(); $wishlists = array();
$wishlists_items = array();
foreach ($user->getWishlists() as $wishlist_result) { foreach ($user->getWishlists() as $wishlist_result) {
$wishlist = new Wishlist($wishlist_result['id']); $wishlist = new Wishlist($wishlist_result['id']);
$wishlists[$wishlist->id] = array( $wishlists[$wishlist->id] = $wishlist;
$wishlists_items[$wishlist->id] = array(
'name' => $wishlist->name, 'name' => $wishlist->name,
'value' => $wishlist->id, 'value' => $wishlist->id,
'text' => $wishlist->name, 'text' => $wishlist->name,
); );
} }
$response['results'] = $wishlists; $response['wishlists'] = $wishlists;
$response['wishlists_items'] = $wishlists_items;
} }
break; break;

View file

@ -1,18 +1,21 @@
var wishlists = [];
$(function () { $(function () {
/** /**
* Get Wishlists * Get Wishlists
*/ */
var wishlists = []; var wishlists_items = [];
var wishlists_api = { var wishlists_api = {
'action' : 'get wishlists', 'action' : 'get wishlists',
'onSuccess' : function(response, dropdown_wishlists, xhr) { 'onSuccess' : function(response, dropdown_wishlists, xhr) {
/** Save response for later use */ /** Save response for later use */
wishlists = response.results; wishlists = response.wishlists;
wishlists_items = response.wishlists_items;
/** Setup and populate dropdown */ /** Setup and populate dropdown */
var dropdown_values = { var dropdown_values = {
'values' : wishlists, 'values' : wishlists_items,
}; };
dropdown_wishlists.dropdown('setup menu', dropdown_values); dropdown_wishlists.dropdown('setup menu', dropdown_values);
@ -704,17 +707,21 @@ $(function () {
*/ */
function setDropdownWishlistsSelection() { function setDropdownWishlistsSelection() {
var dropdown_wishlists = $('.ui.dropdown.wishlists'); var dropdown_wishlists = $('.ui.dropdown.wishlists');
var wishlist_id;
if (!dropdown_wishlists.dropdown('get value')) { if (!dropdown_wishlists.dropdown('get value')) {
if (wishthis.$_GET.id) { if (wishthis.$_GET.id) {
dropdown_wishlists.dropdown('set selected', wishthis.$_GET.id); wishlist_id = wishthis.$_GET.id;
} else { } else {
if (Object.keys(wishlists).length >= 1) { if (Object.keys(wishlists).length >= 1) {
var first_wishlist_id = Object.keys(wishlists)[0]; 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);
} }
} }