fix: user wishlist view

This commit is contained in:
grandeljay 2023-09-05 14:49:40 +02:00
parent 4fc16105af
commit 05cd98812b
4 changed files with 44 additions and 38 deletions

View file

@ -117,7 +117,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
'placeholders' => array(), 'placeholders' => array(),
); );
$where = array( $where = array(
'wishlist' => '`wishlist` = ' . $wishlist->id, 'wishlist' => '`wishlist` = ' . $wishlist->getId(),
'priority' => '`priority` = ' . $_GET['priority'], 'priority' => '`priority` = ' . $_GET['priority'],
); );
@ -137,10 +137,10 @@ switch ($_SERVER['REQUEST_METHOD']) {
*/ */
$wishlist = Wishlist::getFromId($_GET['wishlist_id']); $wishlist = Wishlist::getFromId($_GET['wishlist_id']);
if ($wishlist->exists) {
/** Determine if user is allowed to access wishlist */ /** Determine if user is allowed to access wishlist */
if ($user->isLoggedIn() && $user->getId() === $wishlist->user) { if ($wishlist instanceof Wishlist) {
$response['results'] = $wishlist; if ($user->isLoggedIn() && $user->getId() === $wishlist->getUserId()) {
$response['results'] = $wishlist->getCards();
} else { } else {
http_response_code(403); http_response_code(403);
} }
@ -167,8 +167,11 @@ switch ($_SERVER['REQUEST_METHOD']) {
$wishlistId = $wishlist->getId(); $wishlistId = $wishlist->getId();
$wishlistName = $wishlist->getName(); $wishlistName = $wishlist->getName();
$wishlists[$wishlistId] = $wishlist; $wishlists[] = array(
$wishlistsItems[$wishlistId] = array( 'id' => $wishlistId,
'hash' => $wishlist->getHash(),
);
$wishlistsItems[] = array(
'name' => $wishlistName, 'name' => $wishlistName,
'value' => $wishlistId, 'value' => $wishlistId,
'text' => $wishlistName, 'text' => $wishlistName,
@ -176,7 +179,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
} }
$response['wishlists'] = $wishlists; $response['wishlists'] = $wishlists;
$response['wishlists_items'] = $wishlistsItems; $response['wishlistsItems'] = $wishlistsItems;
} }
break; break;

View file

@ -11,10 +11,10 @@ $(function () {
.api({ .api({
'action' : 'get wishlists by priority', 'action' : 'get wishlists by priority',
'beforeSend' : function (settings) { 'beforeSend' : function (settings) {
var wishlist_id = $('.wishlist-cards[data-wishlist]').attr('data-wishlist'); var wishlistId = $('.wishlist-cards[data-wishlist]').attr('data-wishlist');
if (undefined === wishthis.$_GET.id && wishlist_id.length > 0) { if (undefined === wishthis.$_GET.id && undefined !== wishlistId && wishlistId.length > 0) {
wishthis.$_GET.id = wishlist_id; wishthis.$_GET.id = wishlistId;
} }
settings.urlData.style = $('input[name="style"]').val(); settings.urlData.style = $('input[name="style"]').val();

View file

@ -9,20 +9,20 @@ $(function () {
/** /**
* Get Wishlists * Get Wishlists
*/ */
var wishlists_items = []; var wishlistsItems = [];
var wishlists_api = { var wishlistsApi = {
'action' : 'get wishlists', 'action' : 'get wishlists',
'onSuccess' : function(response, dropdown_wishlists, xhr) { 'onSuccess' : function(response, dropdownWishlists, xhr) {
/** Save response for later use */ /** Save response for later use */
wishlists = response.wishlists; wishlists = response.wishlists;
wishlists_items = response.wishlists_items; wishlistsItems = response.wishlistsItems;
/** Setup and populate dropdown */ /** Setup and populate dropdown */
var dropdown_values = { var dropdownValues = {
'values' : wishlists_items, 'values' : wishlistsItems,
}; };
dropdown_wishlists.dropdown('setup menu', dropdown_values); dropdownWishlists.dropdown('setup menu', dropdownValues);
/** Select a dropdown item */ /** Select a dropdown item */
setDropdownWishlistsSelection(); setDropdownWishlistsSelection();
@ -57,13 +57,15 @@ $(function () {
.then(handleFetchError) .then(handleFetchError)
.then(handleFetchResponse) .then(handleFetchResponse)
.then(function(response) { .then(function(response) {
var wishlist; /** Set currently selected wishlist */
wishlists.forEach(wishlistI => {
/** Set current wishlist */ if (wishlistI.id === parseInt(wishlist_id)) {
wishlist = response.results; wishlist = wishlistI;
}
});
/** Set share link */ /** Set share link */
$('.wishlist-share').attr('href', '/wishlist/' + wishlist.hash); $('.wishlist-share').attr('href', '/wishlist/' + $(wishlist).prop('hash'));
/** Enable wishlist options buttons */ /** Enable wishlist options buttons */
$('.button.wishlist-wish-add').removeClass('disabled'); $('.button.wishlist-wish-add').removeClass('disabled');
@ -160,7 +162,7 @@ $(function () {
} }
}, },
}) })
.api(wishlists_api) .api(wishlistsApi)
.api('query'); .api('query');
/** /**
@ -572,15 +574,16 @@ $(function () {
if (wishthis.$_GET.id) { if (wishthis.$_GET.id) {
wishlist_id = wishthis.$_GET.id; wishlist_id = wishthis.$_GET.id;
} else { } else {
if (Object.keys(wishlists).length >= 1) { if (wishlists.length >= 1) {
var first_wishlist_id = Object.keys(wishlists)[0]; wishlist = $(wishlists).first();
var first_wishlist_id = wishlist.prop('id');
wishlist_id = first_wishlist_id; wishlist_id = first_wishlist_id;
} }
} }
wishlist = wishlists[wishlist_id]; dropdown_wishlists.dropdown('set selected', wishlist_id);
dropdown_wishlists.dropdown('set selected', wishlist.id);
} }
} }

View file

@ -118,14 +118,14 @@ class Wish
/** /**
* A unix timestamp of when this wish was last edited. * A unix timestamp of when this wish was last edited.
* *
* @var int * @var int|null
*/ */
private int $edited; private ?int $edited;
public string $style = 'grid'; public string $style = 'grid';
/** Product */ /** Product */
public ?float $price; public ?float $price = null;
/** Other */ /** Other */
public \stdClass $info; public \stdClass $info;
@ -160,14 +160,14 @@ class Wish
$this->id = $columns['id']; $this->id = $columns['id'];
$this->wishlist = $columns['wishlist']; $this->wishlist = $columns['wishlist'];
$this->title = $columns['title']; $this->title = $columns['title'] ?? '';
$this->description = $columns['description']; $this->description = $columns['description'] ?? '';
$this->image = $columns['image']; $this->image = $columns['image'] ?? '';
$this->url = $columns['url']; $this->url = $columns['url'] ?? '';
$this->priority = $columns['priority']; $this->priority = $columns['priority'];
$this->status = $columns['status']; $this->status = $columns['status'];
$this->is_purchasable = $columns['is_purchasable']; $this->is_purchasable = $columns['is_purchasable'];
$this->edited = $columns['edited']; $this->edited = $columns['edited'] ? \strtotime($columns['edited']) : null;
$this->info = new \stdClass(); $this->info = new \stdClass();