Improve wishlist view

This commit is contained in:
Jay Trees 2022-02-22 13:02:32 +01:00
parent 066315fea4
commit 05cc9ee0ce
6 changed files with 107 additions and 109 deletions

View file

@ -6,7 +6,7 @@
* @author Jay Trees <github.jay@grandel.anonaddy.me> * @author Jay Trees <github.jay@grandel.anonaddy.me>
*/ */
use wishthis\User; use wishthis\{User, Wishlist};
$api = true; $api = true;
$response = array( $response = array(
@ -20,13 +20,16 @@ switch ($_SERVER['REQUEST_METHOD']) {
if (isset($_GET['userid']) || isset($_SESSION['user']['id'])) { if (isset($_GET['userid']) || isset($_SESSION['user']['id'])) {
$user = isset($_GET['userid']) ? new User($_GET['userid']) : new User(); $user = isset($_GET['userid']) ? new User($_GET['userid']) : new User();
$wishlists = $user->getWishlists(); $wishlists = $user->getWishlists();
$wishlists = array_map(function ($wishlist) { $wishlists = array_map(function ($dataWishlist) {
$newFormat = $wishlist; $data = $dataWishlist;
// $newFormat['name'] = $wishlist['name']; // $newFormat['name'] = $wishlist['name'];
$newFormat['value'] = $wishlist['id']; $data['value'] = $dataWishlist['id'];
$newFormat['text'] = $wishlist['name']; $data['text'] = $dataWishlist['name'];
return $newFormat; $wishlist = new Wishlist(intval($dataWishlist['id']));
$data['cards'] = $wishlist->getCards();
return $data;
}, $wishlists); }, $wishlists);
$response['results'] = $wishlists; $response['results'] = $wishlists;
$response['success'] = true; $response['success'] = true;

View file

@ -40,14 +40,18 @@ $(function() {
console.log(element); console.log(element);
console.log(xhr); console.log(xhr);
if (response.startsWith('<br />')) {
response = response.replace('<br />', '');
}
$('body') $('body')
.modal({ .modal({
title: 'Failure', title: 'Failure',
content: 'The process has failed.', content: response,
class: '', class: '',
actions: [ actions: [
{ {
text: 'Oh well', text: 'Thanks for nothing',
class: 'primary' class: 'primary'
} }
] ]
@ -59,14 +63,18 @@ $(function() {
console.log(element); console.log(element);
console.log(xhr); console.log(xhr);
if (response.startsWith('<br />')) {
response = response.replace('<br />', '');
}
$('body') $('body')
.modal({ .modal({
title: 'Error', title: 'Error',
content: 'There has been an error.', content: response,
class: '', class: '',
actions: [ actions: [
{ {
text: 'Oh well', text: 'Thanks for nothing',
class: 'primary' class: 'primary'
} }
] ]
@ -85,7 +93,7 @@ $(function() {
class: '', class: '',
actions: [ actions: [
{ {
text: 'Oh well', text: 'Thanks for nothing',
class: 'primary' class: 'primary'
} }
] ]

View file

@ -10,6 +10,7 @@ $(function() {
on: 'now', on: 'now',
onSuccess: function(response, element, xhr) { onSuccess: function(response, element, xhr) {
wishlists = response.results; wishlists = response.results;
console.log(wishlists);
element.dropdown({ element.dropdown({
values: wishlists, values: wishlists,
@ -38,8 +39,6 @@ $(function() {
$('[name="wishlist_delete_id"]').val(wishlistValue); $('[name="wishlist_delete_id"]').val(wishlistValue);
if (wishlistValue) { if (wishlistValue) {
$('.wishlist-view').removeClass('disabled');
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
urlParams.set('wishlist', wishlistValue); urlParams.set('wishlist', wishlistValue);
window.history.pushState({}, '', '/?' + urlParams.toString()); window.history.pushState({}, '', '/?' + urlParams.toString());
@ -49,11 +48,14 @@ $(function() {
$('.wishlist-share').removeClass('disabled'); $('.wishlist-share').removeClass('disabled');
$('.wishlist-delete button').removeClass('disabled'); $('.wishlist-delete button').removeClass('disabled');
} else { } else {
$('.wishlist-view').addClass('disabled');
$('.wishlist-share').addClass('disabled'); $('.wishlist-share').addClass('disabled');
$('.wishlist-delete button').addClass('disabled'); $('.wishlist-delete button').addClass('disabled');
} }
/**
* Cards
*/
$('.wishlist-cards').html(wishlists[wishlistIndex].cards);
}); });
/** /**

View file

@ -57,8 +57,10 @@ class Wishlist
->fetchAll(); ->fetchAll();
} }
public function getCards($options = array()): void public function getCards($options = array()): string
{ {
ob_start();
/** /**
* Exclude * Exclude
*/ */
@ -76,74 +78,70 @@ class Wishlist
* Cards * Cards
*/ */
if (!empty($products)) { ?> if (!empty($products)) { ?>
<div class="ui three column stackable grid wishlist-cards"> <?php foreach ($products as $product) {
$cache = new EmbedCache();
$info = $cache->get($product['url']);
?>
<div class="column">
<div class="ui fluid card" data-id="<?= $product['id'] ?>">
<?php foreach ($products as $product) { <?php if ($info->image) { ?>
$cache = new EmbedCache(); <div class="image">
$info = $cache->get($product['url']); <img src="<?= $info->image ?>" />
?> </div>
<div class="column"> <?php } ?>
<div class="ui fluid card" data-id="<?= $product['id'] ?>">
<?php if ($info->image) { ?> <div class="content">
<div class="image"> <?php if ($info->title) { ?>
<img src="<?= $info->image ?>" /> <div class="header">
<?php if ($info->favicon) { ?>
<img src="<?= $info->favicon ?>" />
<?php } ?>
<?php if ($info->url) { ?>
<a href="<?= $info->url ?>" target="_blank"><?= $info->title ?></a>
<?php } else { ?>
<?= $info->title ?>
<?php } ?>
</div> </div>
<?php } ?> <?php } ?>
<div class="content"> <?php if ($info->keywords) { ?>
<?php if ($info->title) { ?> <div class="meta">
<div class="header"> <?= $info->keywords ?>
<?php if ($info->favicon) { ?> </div>
<img src="<?= $info->favicon ?>" /> <?php } ?>
<?php } ?>
<?php if ($info->url) { ?>
<a href="<?= $info->url ?>" target="_blank"><?= $info->title ?></a>
<?php } else { ?>
<?= $info->title ?>
<?php } ?>
</div>
<?php } ?>
<?php if ($info->keywords) { ?>
<div class="meta">
<?= $info->keywords ?>
</div>
<?php } ?>
<?php if ($info->description) { ?>
<div class="description">
<?= $info->description ?>
</div>
<?php } ?>
</div>
<div class="extra content">
<?php if ($info->publishedTime) { ?>
<span class="right floated">
<?= $info->publishedTime ?>
</span>
<?php } ?>
<?php if ($info->providerName) { ?>
<?= $info->providerName ?>
<?php } ?>
</div>
<div class="extra content">
<?php if ($info->url) { ?>
<a class="ui tiny button" href="<?= $info->url ?>" target="_blank">View</a>
<?php } ?>
<?php if ($this->data['user'] === $_SESSION['user']['id']) { ?>
<a class="ui tiny red button delete">Delete</a>
<?php } else { ?>
<a class="ui tiny button commit">Commit</a>
<?php } ?>
</div>
<?php if ($info->description) { ?>
<div class="description">
<?= $info->description ?>
</div>
<?php } ?>
</div>
<div class="extra content">
<?php if ($info->publishedTime) { ?>
<span class="right floated">
<?= $info->publishedTime ?>
</span>
<?php } ?>
<?php if ($info->providerName) { ?>
<?= $info->providerName ?>
<?php } ?>
</div>
<div class="extra content">
<?php if ($info->url) { ?>
<a class="ui tiny button" href="<?= $info->url ?>" target="_blank">View</a>
<?php } ?>
<?php if ($this->data['user'] === $_SESSION['user']['id']) { ?>
<a class="ui tiny red button delete">Delete</a>
<?php } else { ?>
<a class="ui tiny button commit">Commit</a>
<?php } ?>
</div> </div>
</div>
<?php } ?>
</div> </div>
</div>
<?php } ?>
<?php } else { ?> <?php } else { ?>
<?php if (isset($_GET['wishlist'])) { ?> <?php if (isset($_GET['wishlist'])) { ?>
<div class="ui icon message"> <div class="ui icon message">
@ -157,5 +155,7 @@ class Wishlist
</div><?php </div><?php
} }
} }
return ob_get_clean();
} }
} }

View file

@ -1,7 +1,7 @@
<?php <?php
/** /**
* wishlist.php * Template for viewing wishlists.
* *
* @author Jay Trees <github.jay@grandel.anonaddy.me> * @author Jay Trees <github.jay@grandel.anonaddy.me>
*/ */
@ -12,13 +12,6 @@ $page = new page(__FILE__, 'View wishlist');
$page->header(); $page->header();
$page->navigation(); $page->navigation();
/**
* Get wishlist products
*/
if (isset($_GET['wishlist'])) {
$wishlist = new Wishlist(intval($_GET['wishlist']));
}
/** /**
* Delete wishlist * Delete wishlist
*/ */
@ -37,24 +30,18 @@ if (isset($_POST['wishlist_delete_id'])) {
<h2 class="ui header">Wishlists</h2> <h2 class="ui header">Wishlists</h2>
<p>Please select a wishlist to view.</p> <p>Please select a wishlist to view.</p>
<form class="ui form" method="get"> <div class="field">
<input type="hidden" name="page" value="wishlist-view" /> <select class="ui search selection dropdown loading wishlists" name="wishlist">
<option value="">Loading your wishlists...</option>
<div class="field"> </select>
<select class="ui search selection dropdown loading wishlists" name="wishlist"> </div>
<option value="">Loading your wishlists...</option>
</select>
</div>
<input class="ui primary button wishlist-view disabled" type="submit" value="View" />
</form>
</div> </div>
<div class="ui segment"> <div class="ui segment">
<h2 class="ui header">Options</h1> <h2 class="ui header">Options</h1>
<p>Wishlist related options.</p> <p>Wishlist related options.</p>
<a class="ui small labeled icon button wishlist-share <?= !isset($_GET['wishlist']) ? 'disabled' : '' ?>" href="/?wishlist=<?= $wishlist->data['hash'] ?? '' ?>" target="_blank"> <a class="ui small labeled icon button wishlist-share disabled" target="_blank">
<i class="share icon"></i> <i class="share icon"></i>
Share Share
</a> </a>
@ -62,7 +49,7 @@ if (isset($_POST['wishlist_delete_id'])) {
<form class="ui form wishlist-delete" method="post" style="display: inline-block;"> <form class="ui form wishlist-delete" method="post" style="display: inline-block;">
<input type="hidden" name="wishlist_delete_id" /> <input type="hidden" name="wishlist_delete_id" />
<button class="ui small labeled red icon button <?= !isset($_GET['wishlist']) ? 'disabled' : '' ?>" type="submit"> <button class="ui small labeled red icon button disabled" type="submit">
<i class="trash icon"></i> <i class="trash icon"></i>
Delete Delete
</button> </button>
@ -70,11 +57,7 @@ if (isset($_POST['wishlist_delete_id'])) {
</div> </div>
</div> </div>
<?php <div class="ui three column stackable grid wishlist-cards"></div>
if (isset($_GET['wishlist'])) {
$wishlist->getCards();
}
?>
</div> </div>
</main> </main>

View file

@ -62,13 +62,15 @@ $page->navigation();
</p> </p>
</div> </div>
<?php <div class="ui three column stackable grid wishlist-cards">
$wishlist->getCards( <?php
array( echo $wishlist->getCards(
'exclude' => array('unavailable'), array(
) 'exclude' => array('unavailable'),
); )
?> );
?>
</div>
</div> </div>
</main> </main>