From 8e3d06b084e3f127febf4d54234a6def5a0b4e3f Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sat, 26 Feb 2022 23:14:10 +0100 Subject: [PATCH 01/12] Improve fetch error handling --- src/assets/js/default.js | 57 ++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/src/assets/js/default.js b/src/assets/js/default.js index 437218e6..94ae1c2c 100644 --- a/src/assets/js/default.js +++ b/src/assets/js/default.js @@ -49,10 +49,6 @@ $(function() { } } $.fn.api.settings.onFailure = function(response, element, xhr) { - console.log(response); - console.log(element); - console.log(xhr); - if ('string' === typeof response) { response = response.replace('
', ''); } @@ -72,10 +68,6 @@ $(function() { .modal('show'); } $.fn.api.settings.onError = function(response, element, xhr) { - console.log(response); - console.log(element); - console.log(xhr); - if ('string' === typeof response) { response = response.replace('
', ''); } @@ -101,3 +93,52 @@ $(function() { $.fn.toast.settings.minDisplayTime = 3000; $.fn.toast.settings.showProgress = true; }); + +/** + * Functions + */ +function handleFetchError(response) { + if (!response.ok) { + console.log('handleFetchError'); + console.log(response); + + showError(response.statusText); + throw Error(response.statusText); + } + + return response; +} + +function handleFetchResponse(response) { + var isText = response.headers.get('content-type')?.includes('text/html'); + var isJSON = response.headers.get('content-type')?.includes('application/json'); + + if (isText) { + return response.text() + .then(function(text) { + if (text.toLowerCase().includes('error') || text.toLowerCase().includes('exception')) { + showError(text); + } + }) + } else if (isJSON) { + return response.json(); + } +} + +function showError(error) { + error = error.replace('
', ''); + + $('body') + .modal({ + title: 'Error', + content: error, + class: '', + actions: [ + { + text: 'Thanks for nothing', + class: 'primary' + } + ] + }) + .modal('show'); +} From 14ee9d09fda7f08c6f44bde807e210396c27672a Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sat, 26 Feb 2022 23:15:03 +0100 Subject: [PATCH 02/12] Add wish type --- src/api/wishes.php | 2 ++ src/assets/js/wishlists.js | 18 +++++++++++------- src/pages/wishlists.php | 8 ++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/api/wishes.php b/src/api/wishes.php index acc24a84..80c65581 100644 --- a/src/api/wishes.php +++ b/src/api/wishes.php @@ -24,9 +24,11 @@ switch ($_SERVER['REQUEST_METHOD']) { $database->query('INSERT INTO `wishes` ( `wishlist`, + `type`, `url` ) VALUES (' . $_POST['wishlist_id'] . ', + "' . $_POST['wish_type'] . '", "' . $_POST['wish_url'] . '" ) ;'); diff --git a/src/assets/js/wishlists.js b/src/assets/js/wishlists.js index 3e22891f..a556650f 100644 --- a/src/assets/js/wishlists.js +++ b/src/assets/js/wishlists.js @@ -376,6 +376,8 @@ $(function () { /** * Add wish */ + $('.ui.dropdown.types').dropdown(); + $(document).on('click', '.button.wishlist-wish-add', function () { var modalWishlistWishAdd = $('.ui.modal.wishlist-wish-add'); @@ -387,17 +389,16 @@ $(function () { onApprove: function (button) { button.addClass('loading'); - var form = $('.ui.form.wishlist-wish-fetch'); - var formData = new URLSearchParams(); - formData.append('wishlist_id', form.find('input[name="wishlist_id"]').val()); - formData.append('wish_url', form.find('input[name="wish_url"]').val()); + var form = $('.ui.form.wishlist-wish-fetch'); + var formData = new URLSearchParams(new FormData(form[0])); fetch('/src/api/wishes.php', { method: 'POST', - body: formData + body: formData }) - .then(response => response.json()) - .then(response => { + .then(handleFetchError) + .then(handleFetchResponse) + .then(function(response) { if (response.success) { $('body').toast({ class: 'success', @@ -411,6 +412,9 @@ $(function () { } button.removeClass('loading'); + }) + .catch(function(error) { + console.log(error); }); return false; diff --git a/src/pages/wishlists.php b/src/pages/wishlists.php index ff4112c8..71b58680 100644 --- a/src/pages/wishlists.php +++ b/src/pages/wishlists.php @@ -138,6 +138,14 @@ $page->navigation();
+
+ + +
+
From 7292caecf49945266fadaf689934d5ad497be62a Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sat, 26 Feb 2022 23:31:21 +0100 Subject: [PATCH 03/12] Refactor --- src/pages/install.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/pages/install.php b/src/pages/install.php index 472b0f13..513784ed 100644 --- a/src/pages/install.php +++ b/src/pages/install.php @@ -112,11 +112,11 @@ switch ($step) { * Users */ $database->query('CREATE TABLE `users` ( - `id` int PRIMARY KEY AUTO_INCREMENT, - `email` varchar(64) NOT NULL UNIQUE, - `password` varchar(128) NOT NULL, + `id` INT PRIMARY KEY AUTO_INCREMENT, + `email` VARCHAR(64) NOT NULL UNIQUE, + `password` VARCHAR(128) NOT NULL, `last_login` datetime NOT NULL DEFAULT NOW(), - `power` int NOT NULL DEFAULT 0 + `power` INT NOT NULL DEFAULT 0 );'); $database->query('CREATE INDEX `idx_password` ON `users` (`password`);'); @@ -124,10 +124,10 @@ switch ($step) { * Wishlists */ $database->query('CREATE TABLE `wishlists` ( - `id` int PRIMARY KEY AUTO_INCREMENT, - `user` int NOT NULL, - `name` varchar(128) NOT NULL, - `hash` varchar(128) NOT NULL, + `id` INT PRIMARY KEY AUTO_INCREMENT, + `user` INT NOT NULL, + `name` VARCHAR(128) NOT NULL, + `hash` VARCHAR(128) NOT NULL, FOREIGN KEY (`user`) REFERENCES `users` (`id`) ON DELETE CASCADE @@ -138,8 +138,8 @@ switch ($step) { * Wishes */ $database->query('CREATE TABLE `wishes` ( - `id` int NOT NULL PRIMARY KEY AUTO_INCREMENT, - `wishlist` int NOT NULL, + `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT, + `wishlist` INT NOT NULL, `type` VARCHAR(32) NULL DEFAULT NULL, `url` VARCHAR(255) NULL DEFAULT NULL, `status` VARCHAR(32) NULL DEFAULT NULL, @@ -152,9 +152,9 @@ switch ($step) { * Options */ $database->query('CREATE TABLE `options` ( - `id` int PRIMARY KEY AUTO_INCREMENT, - `key` varchar(64) NOT NULL UNIQUE, - `value` varchar(128) NOT NULL + `id` INT PRIMARY KEY AUTO_INCREMENT, + `key` VARCHAR(64) NOT NULL UNIQUE, + `value` VARCHAR(128) NOT NULL );'); $database->query('INSERT INTO `options` From 0b2e8889d3f5b97dc123081ec655cb8970c95af1 Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sat, 26 Feb 2022 23:37:54 +0100 Subject: [PATCH 04/12] Remove wish types --- src/api/wishes.php | 2 -- src/assets/js/wishlists.js | 2 -- src/pages/wishlists.php | 8 -------- 3 files changed, 12 deletions(-) diff --git a/src/api/wishes.php b/src/api/wishes.php index 80c65581..acc24a84 100644 --- a/src/api/wishes.php +++ b/src/api/wishes.php @@ -24,11 +24,9 @@ switch ($_SERVER['REQUEST_METHOD']) { $database->query('INSERT INTO `wishes` ( `wishlist`, - `type`, `url` ) VALUES (' . $_POST['wishlist_id'] . ', - "' . $_POST['wish_type'] . '", "' . $_POST['wish_url'] . '" ) ;'); diff --git a/src/assets/js/wishlists.js b/src/assets/js/wishlists.js index a556650f..d280e7b4 100644 --- a/src/assets/js/wishlists.js +++ b/src/assets/js/wishlists.js @@ -376,8 +376,6 @@ $(function () { /** * Add wish */ - $('.ui.dropdown.types').dropdown(); - $(document).on('click', '.button.wishlist-wish-add', function () { var modalWishlistWishAdd = $('.ui.modal.wishlist-wish-add'); diff --git a/src/pages/wishlists.php b/src/pages/wishlists.php index 71b58680..ff4112c8 100644 --- a/src/pages/wishlists.php +++ b/src/pages/wishlists.php @@ -138,14 +138,6 @@ $page->navigation(); -
- - -
-
From 95fce926c51873b84731b5e41dd6db67c0b348c6 Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sun, 27 Feb 2022 00:39:08 +0100 Subject: [PATCH 05/12] Add wish title --- src/api/wishes.php | 2 ++ src/assets/js/wishlists.js | 45 +++++++++++++++++++------------------- src/classes/wishlist.php | 11 +++++----- src/pages/install.php | 2 +- src/pages/wishlists.php | 30 ++++++++++++++++--------- 5 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/api/wishes.php b/src/api/wishes.php index acc24a84..a35cbdfd 100644 --- a/src/api/wishes.php +++ b/src/api/wishes.php @@ -24,9 +24,11 @@ switch ($_SERVER['REQUEST_METHOD']) { $database->query('INSERT INTO `wishes` ( `wishlist`, + `title`, `url` ) VALUES (' . $_POST['wishlist_id'] . ', + "' . $_POST['wish_title'] . '", "' . $_POST['wish_url'] . '" ) ;'); diff --git a/src/assets/js/wishlists.js b/src/assets/js/wishlists.js index d280e7b4..96e922e7 100644 --- a/src/assets/js/wishlists.js +++ b/src/assets/js/wishlists.js @@ -380,14 +380,12 @@ $(function () { var modalWishlistWishAdd = $('.ui.modal.wishlist-wish-add'); modalWishlistWishAdd.find('[name="wishlist_id"]').val($('.ui.dropdown.wishlists').dropdown('get value')); - modalWishlistWishAdd.find('.primary.approve.button').addClass('disabled'); - modalWishlistWishAdd .modal({ - onApprove: function (button) { - button.addClass('loading'); + onApprove: function (buttonAdd) { + buttonAdd.addClass('loading'); - var form = $('.ui.form.wishlist-wish-fetch'); + var form = $('.form.wishlist-wish-add'); var formData = new URLSearchParams(new FormData(form[0])); fetch('/src/api/wishes.php', { @@ -409,7 +407,7 @@ $(function () { modalWishlistWishAdd.modal('hide'); } - button.removeClass('loading'); + buttonAdd.removeClass('loading'); }) .catch(function(error) { console.log(error); @@ -422,20 +420,19 @@ $(function () { }); /** Fetch */ - $(document).on('submit', '.wishlist-wish-fetch', function (event) { - event.preventDefault(); - - var form = $(event.currentTarget); - var href = form.find('[name="wish_url"]').val(); + $(document).on('click', '#wishlist-wish-add-url-validate', function () { + var buttonValidate = $(this); + var inputWishURL = buttonValidate.prev(); + var inputURLContainer = buttonValidate.parent(); var elementModalAdd = $('.ui.modal.wishlist-wish-add'); var elementButtons = elementModalAdd.find('.actions .button'); - var elementImage = elementModalAdd.find('.image img'); + var elementName = elementModalAdd.find('input[name="wish_name"]'); - form.addClass('loading'); + buttonValidate.addClass('disabled loading'); elementButtons.addClass('disabled'); - fetch('/src/api/cache.php?wish_url=' + href, { + fetch('/src/api/cache.php?wish_url=' + inputWishURL.val(), { method: 'GET' }) .then(response => response.json()) @@ -444,19 +441,19 @@ $(function () { var info = response.data; /** - * Image + * Name */ - if (info.image && elementImage.length) { - elementImage.attr('src', info.image); + if (info.title && elementName.length) { + elementName.val(info.title); } /** * URL */ - if (info.url && info.url !== href) { + if (info.url && info.url !== inputWishURL.val()) { var elementModalFetch = $('.ui.modal.wishlist-wish-fetch'); - elementModalFetch.find('input.current').val(href); + elementModalFetch.find('input.current').val(inputWishURL.val()); elementModalFetch.find('input.proposed').val(info.url); elementButtons.addClass('disabled'); @@ -467,7 +464,7 @@ $(function () { closable: false, onApprove: function (buttonFetch) { var formData = new URLSearchParams(); - formData.append('wish_url_current', href); + formData.append('wish_url_current', inputWishURL.val()); formData.append('wish_url_proposed', info.url); buttonFetch.addClass('loading'); @@ -479,7 +476,7 @@ $(function () { .then(response => response.json()) .then(response => { if (response.success) { - form.find('input[type="url"]').val(info.url); + inputWishURL.val(info.url); elementModalFetch.modal('hide'); } @@ -490,15 +487,17 @@ $(function () { return false; }, onHide: function() { - form.removeClass('loading'); + buttonValidate.removeClass('disabled loading'); elementButtons.removeClass('disabled'); } }) .modal('show'); } else { - form.removeClass('loading'); + buttonValidate.removeClass('disabled loading'); elementButtons.removeClass('disabled'); } + + inputURLContainer.attr('data-validated', 'true'); } }); }); diff --git a/src/classes/wishlist.php b/src/classes/wishlist.php index a2027e5d..035d4510 100644 --- a/src/classes/wishlist.php +++ b/src/classes/wishlist.php @@ -89,6 +89,8 @@ class Wishlist $cache = new EmbedCache($wish['url']); $info = $cache->get(false); $exists = $cache->exists() ? 'true' : 'false'; + + $title = $wish['title'] ?? $info->title; ?>
@@ -114,14 +116,13 @@ class Wishlist
-
- title) { ?> +
url) { ?> - title ?> + - title ?> +
@@ -151,7 +152,7 @@ class Wishlist url) { ?> - View + Visit diff --git a/src/pages/install.php b/src/pages/install.php index 513784ed..595ee632 100644 --- a/src/pages/install.php +++ b/src/pages/install.php @@ -140,7 +140,7 @@ switch ($step) { $database->query('CREATE TABLE `wishes` ( `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT, `wishlist` INT NOT NULL, - `type` VARCHAR(32) NULL DEFAULT NULL, + `title` VARCHAR(128) NULL DEFAULT NULL, `url` VARCHAR(255) NULL DEFAULT NULL, `status` VARCHAR(32) NULL DEFAULT NULL, FOREIGN KEY (`wishlist`) diff --git a/src/pages/wishlists.php b/src/pages/wishlists.php index ff4112c8..db0cd1e1 100644 --- a/src/pages/wishlists.php +++ b/src/pages/wishlists.php @@ -127,28 +127,38 @@ $page->navigation();
Add a wish
-
-
- -
+
Wish
-

Fill out the below fields to add your new wish.

+

Fill out any or all of the below fields to add your new wish.

- +
- - + +
- +
+ + +
+ + + +
+
-
+
Add
From fa7d0d72255006dba00d66358a1022dc78886ce4 Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sun, 27 Feb 2022 00:42:04 +0100 Subject: [PATCH 06/12] Refactor --- src/assets/js/wishlists.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/assets/js/wishlists.js b/src/assets/js/wishlists.js index 96e922e7..f2e9886c 100644 --- a/src/assets/js/wishlists.js +++ b/src/assets/js/wishlists.js @@ -427,7 +427,7 @@ $(function () { var elementModalAdd = $('.ui.modal.wishlist-wish-add'); var elementButtons = elementModalAdd.find('.actions .button'); - var elementName = elementModalAdd.find('input[name="wish_name"]'); + var elementTitle = elementModalAdd.find('input[name="wish_name"]'); buttonValidate.addClass('disabled loading'); elementButtons.addClass('disabled'); @@ -443,8 +443,8 @@ $(function () { /** * Name */ - if (info.title && elementName.length) { - elementName.val(info.title); + if (info.title && elementTitle.length) { + elementTitle.val(info.title); } /** From 083f3a4cc36b502cad94d47fd52e24804934f89c Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sun, 27 Feb 2022 00:42:27 +0100 Subject: [PATCH 07/12] Refactor --- src/assets/js/wishlists.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/assets/js/wishlists.js b/src/assets/js/wishlists.js index f2e9886c..6aa848f1 100644 --- a/src/assets/js/wishlists.js +++ b/src/assets/js/wishlists.js @@ -441,7 +441,7 @@ $(function () { var info = response.data; /** - * Name + * Title */ if (info.title && elementTitle.length) { elementTitle.val(info.title); From 965fbf3af61f1927287af7e121411cdcd0e6680b Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sun, 27 Feb 2022 01:05:50 +0100 Subject: [PATCH 08/12] Improve spelling --- src/pages/register.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/register.php b/src/pages/register.php index 46b54cc9..edf9f1f8 100644 --- a/src/pages/register.php +++ b/src/pages/register.php @@ -111,7 +111,7 @@ $page->navigation();

Authentication

- Prove you are a Human, Lizard-person or Zuck-Like creature. + Prove you are a Human, Lizard-person or Zuck-like creature. Please name a planet from our solar system.

From 2a33f2c32084ad16579682147a21237da10b3e83 Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sun, 27 Feb 2022 01:07:50 +0100 Subject: [PATCH 09/12] Fix cache for wishes without an url --- src/assets/js/wishlists.js | 4 ++++ src/classes/wishlist.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/assets/js/wishlists.js b/src/assets/js/wishlists.js index 6aa848f1..4cba2ef0 100644 --- a/src/assets/js/wishlists.js +++ b/src/assets/js/wishlists.js @@ -123,6 +123,10 @@ $(function () { var wish_id = card.data('id'); var refresh = card.find('button.refresh'); + if (!href) { + return; + } + card.addClass('loading'); card.attr('data-cache', true); diff --git a/src/classes/wishlist.php b/src/classes/wishlist.php index 035d4510..bb1f3196 100644 --- a/src/classes/wishlist.php +++ b/src/classes/wishlist.php @@ -88,7 +88,7 @@ class Wishlist get(false); - $exists = $cache->exists() ? 'true' : 'false'; + $exists = $cache->exists() || !$info->url ? 'true' : 'false'; $title = $wish['title'] ?? $info->title; ?> From 952e5715162addd193da6989904fe88429b3b706 Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sun, 27 Feb 2022 01:08:48 +0100 Subject: [PATCH 10/12] Hide refresh button for wishes without url --- src/classes/wishlist.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/classes/wishlist.php b/src/classes/wishlist.php index bb1f3196..d1f7d5f2 100644 --- a/src/classes/wishlist.php +++ b/src/classes/wishlist.php @@ -109,7 +109,7 @@ class Wishlist providerName ?> - + url) { ?> From a2956c47eca2f786a4997c9d324634c039f9c59a Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sun, 27 Feb 2022 01:09:19 +0100 Subject: [PATCH 11/12] Add description --- src/api/wishes.php | 2 ++ src/assets/css/default.css | 6 ++++-- src/assets/js/wishlists.js | 16 ++++++++++++---- src/classes/wishlist.php | 7 ++++--- src/pages/install.php | 11 ++++++----- src/pages/wishlists.php | 5 +++++ 6 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/api/wishes.php b/src/api/wishes.php index a35cbdfd..2bffc4bd 100644 --- a/src/api/wishes.php +++ b/src/api/wishes.php @@ -25,10 +25,12 @@ switch ($_SERVER['REQUEST_METHOD']) { ( `wishlist`, `title`, + `description`, `url` ) VALUES (' . $_POST['wishlist_id'] . ', "' . $_POST['wish_title'] . '", + "' . $_POST['wish_description'] . '", "' . $_POST['wish_url'] . '" ) ;'); diff --git a/src/assets/css/default.css b/src/assets/css/default.css index 6071d12c..d7fe35dc 100644 --- a/src/assets/css/default.css +++ b/src/assets/css/default.css @@ -144,17 +144,19 @@ img { max-height: calc(2 * 1.28571429em); overflow: hidden; } - .ui.card > .content > .description { max-height: calc(3em * var(--lineHeight)); overflow: hidden; } @media (hover: hover) { + .ui.card > .content:not(.extra) { + margin-bottom: calc(3em * var(--lineHeight)); + } + .ui.card > .content > .description { position: absolute; left: 1em; right: 1em; - max-height: calc(3em * var(--lineHeight)); } } diff --git a/src/assets/js/wishlists.js b/src/assets/js/wishlists.js index 4cba2ef0..cb03044a 100644 --- a/src/assets/js/wishlists.js +++ b/src/assets/js/wishlists.js @@ -239,7 +239,7 @@ $(function () { */ $(document).on('click', '.ui.button.refresh', function (event) { var button = $(event.currentTarget); - var card = button.closest('.ui.card'); + var card = button.closest('.ui.card'); button.addClass('working'); @@ -429,9 +429,10 @@ $(function () { var inputWishURL = buttonValidate.prev(); var inputURLContainer = buttonValidate.parent(); - var elementModalAdd = $('.ui.modal.wishlist-wish-add'); - var elementButtons = elementModalAdd.find('.actions .button'); - var elementTitle = elementModalAdd.find('input[name="wish_name"]'); + var elementModalAdd = $('.ui.modal.wishlist-wish-add'); + var elementButtons = elementModalAdd.find('.actions .button'); + var elementTitle = elementModalAdd.find('[name="wish_name"]'); + var elementDescription = elementModalAdd.find('[name="wish_description"]'); buttonValidate.addClass('disabled loading'); elementButtons.addClass('disabled'); @@ -451,6 +452,13 @@ $(function () { elementTitle.val(info.title); } + /** + * Description + */ + if (info.description && elementDescription.length) { + elementDescription.text(info.description); + } + /** * URL */ diff --git a/src/classes/wishlist.php b/src/classes/wishlist.php index d1f7d5f2..fe7cf277 100644 --- a/src/classes/wishlist.php +++ b/src/classes/wishlist.php @@ -90,7 +90,8 @@ class Wishlist $info = $cache->get(false); $exists = $cache->exists() || !$info->url ? 'true' : 'false'; - $title = $wish['title'] ?? $info->title; + $title = $wish['title'] ?? $info->title; + $description = $wish['description'] ?? $info->description; ?>
@@ -133,9 +134,9 @@ class Wishlist
- description) { ?> +
- description ?> +
diff --git a/src/pages/install.php b/src/pages/install.php index 595ee632..1f54114a 100644 --- a/src/pages/install.php +++ b/src/pages/install.php @@ -138,11 +138,12 @@ switch ($step) { * Wishes */ $database->query('CREATE TABLE `wishes` ( - `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT, - `wishlist` INT NOT NULL, - `title` VARCHAR(128) NULL DEFAULT NULL, - `url` VARCHAR(255) NULL DEFAULT NULL, - `status` VARCHAR(32) NULL DEFAULT NULL, + `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT, + `wishlist` INT NOT NULL, + `title` VARCHAR(128) NULL DEFAULT NULL, + `description` TEXT NULL DEFAULT NULL, + `url` VARCHAR(255) NULL DEFAULT NULL, + `status` VARCHAR(32) NULL DEFAULT NULL, FOREIGN KEY (`wishlist`) REFERENCES `wishlists` (`id`) ON DELETE CASCADE diff --git a/src/pages/wishlists.php b/src/pages/wishlists.php index db0cd1e1..103b9436 100644 --- a/src/pages/wishlists.php +++ b/src/pages/wishlists.php @@ -140,6 +140,11 @@ $page->navigation();
+
+ + +
+
From 360975fc97a29ce8a0447e63ae155f7cf6a336ac Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sun, 27 Feb 2022 02:55:07 +0100 Subject: [PATCH 12/12] Improve adding wishes without url --- src/api/cache.php | 30 ----- src/api/wishes.php | 27 +++- src/assets/js/default.js | 4 +- src/assets/js/home.js | 3 - src/assets/js/wishlists.js | 261 ++++++++++++------------------------ src/classes/embed-cache.php | 14 +- src/classes/wish.php | 149 ++++++++++++++++++++ src/classes/wishlist.php | 101 ++------------ src/pages/install.php | 1 + 9 files changed, 285 insertions(+), 305 deletions(-) delete mode 100644 src/api/cache.php create mode 100644 src/classes/wish.php diff --git a/src/api/cache.php b/src/api/cache.php deleted file mode 100644 index 0ed936f1..00000000 --- a/src/api/cache.php +++ /dev/null @@ -1,30 +0,0 @@ - - */ - -use wishthis\EmbedCache; - -$api = true; -$response = array( - 'success' => false, -); - -require '../../index.php'; - -switch ($_SERVER['REQUEST_METHOD']) { - case 'GET': - $cache = new EmbedCache($_GET['wish_url']); - $info = $cache->get(true); - - $response['success'] = true; - $response['data'] = $info; - break; -} - -header('Content-type: application/json; charset=utf-8'); -echo json_encode($response); -die(); diff --git a/src/api/wishes.php b/src/api/wishes.php index 2bffc4bd..dcdcdaba 100644 --- a/src/api/wishes.php +++ b/src/api/wishes.php @@ -6,7 +6,7 @@ * @author Jay Trees */ -use wishthis\User; +use wishthis\{User, Wish, EmbedCache}; $api = true; $response = array( @@ -16,6 +16,31 @@ $response = array( require '../../index.php'; switch ($_SERVER['REQUEST_METHOD']) { + case 'GET': + if (isset($_GET['wish_id'], $_GET['wishlist_user'])) { + $columns = $database + ->query('SELECT * + FROM `wishes` + WHERE `id` = ' . $_GET['wish_id'] . ';') + ->fetch(); + + $wish = new wish($columns); + + $response = array( + 'info' => $wish, + 'html' => $wish->getCard($_GET['wishlist_user'], true) + ); + } elseif (isset($_GET['wish_url'])) { + $cache = new EmbedCache($_GET['wish_url']); + $info = $cache->get(true); + $exists = $cache->exists() ? 'true' : 'false'; + + $response = array( + 'info' => $info + ); + } + break; + case 'POST': if (isset($_POST['wishlist_id'], $_POST['wish_url'])) { /** diff --git a/src/assets/js/default.js b/src/assets/js/default.js index 94ae1c2c..f8f3ff29 100644 --- a/src/assets/js/default.js +++ b/src/assets/js/default.js @@ -20,8 +20,8 @@ $(function() { $.fn.api.settings.api = { 'get wishlists' : '/src/api/wishlists.php', 'delete wishlist' : '/src/api/wishlists.php', - 'update wish status': '/src/api/wishes.php', - 'delete wish' : '/src/api/wishes.php', + 'update wish status' : '/src/api/wishes.php', + 'delete wish' : '/src/api/wishes.php', }; /** Default callbacks */ diff --git a/src/assets/js/home.js b/src/assets/js/home.js index ba74e3aa..ed584106 100644 --- a/src/assets/js/home.js +++ b/src/assets/js/home.js @@ -7,8 +7,6 @@ $(function() { }) .then(response => response.json()) .then(response => { - console.log(response); - if (response.success) { showStatistic( $('#wishes .value'), @@ -51,7 +49,6 @@ function showStatistic(elementStatistic, amount, timeout) { interval += interval * 0.1; } - console.log(interval); setTimeout(count, interval); } }, diff --git a/src/assets/js/wishlists.js b/src/assets/js/wishlists.js index cb03044a..5e511ee1 100644 --- a/src/assets/js/wishlists.js +++ b/src/assets/js/wishlists.js @@ -120,7 +120,6 @@ $(function () { card = $(card); var href = card.find('.content [href]').prop('href'); - var wish_id = card.data('id'); var refresh = card.find('button.refresh'); if (!href) { @@ -130,108 +129,26 @@ $(function () { card.addClass('loading'); card.attr('data-cache', true); - fetch('/src/api/cache.php?wish_id=' + wish_id + '&wish_url=' + href, { + var wishlistIndex = $('.ui.dropdown.wishlists select').prop('selectedIndex') - 1; + var wishlist_user = wishlists[wishlistIndex].user; + + fetch('/src/api/wishes.php?wish_id=' + card.data('id') + '&wishlist_user=' + wishlist_user, { method: 'GET' }) - .then(response => response.json()) - .then(response => { - if (response.success) { - var info = response.data; - - /** - * Elements - */ - var elementImage = card.children('.image'); - var elementContent = card.children('.content').first(); - var elementDetails = card.children('.extra.content.details'); - var elementButtons = card.children('.extra.content.buttons'); - - /** - * Image - */ - if (info.image) { - if (!elementImage.length) { - card.prepend( - '
' + - '' + - '
' - ); - } else { - elementImage.children('img').attr('src', info.image); - } - } - - /** Favicon */ - if (info.favicon) { - var elementFavicon = elementImage.children('img.favicon'); - - if (!elementFavicon.length) { - elementImage.children().first().after( - '' - ); - } else { - elementFavicon.attr('src', info.favicon); - } - } - - /** Provider name */ - if (info.providerName) { - var elementProviderName = elementImage.children('span.provider'); - - if (!elementProviderName.length) { - $('' + info.providerName + '').insertBefore(elementImage.children().last()); - } else { - elementProviderName.text(info.providerName); - } - } - - /** - * Header - */ - var elementContentHeader = elementContent.children('.header'); - var elementContentHeaderTitle = elementContentHeader.children('a'); - - /** Title */ - if (info.title) { - elementContentHeaderTitle.text(info.title); - } - - /** - * Meta - */ - var elementContentMeta = elementContent.children('.meta'); - - if (info.keywords.length) { - if (!elementContentMeta.length) { - elementContent.append( - '
' + info.keywords.join(', ') + '
' - ); - } - } - - /** - * Description - */ - var elementContentDescription = elementContent.children('.description'); - - if (info.description) { - if (!elementContentDescription.length) { - elementContent.append( - '
' + info.description + '
' + - '
' - ); - } - } - - /** - * Finish - */ - card.removeClass('loading'); - progress.progress('increment'); - } - - refresh.removeClass('working'); + .then(handleFetchError) + .then(handleFetchResponse) + .then(function(response) { + card.html(response.html); + }) + .catch(function(error) { + console.log(error); + }) + .finally(function() { + card.attr('data-cache', 'true'); + card.removeClass('loading'); }); + + refresh.removeClass('working'); } /** @@ -427,91 +344,91 @@ $(function () { $(document).on('click', '#wishlist-wish-add-url-validate', function () { var buttonValidate = $(this); var inputWishURL = buttonValidate.prev(); - var inputURLContainer = buttonValidate.parent(); var elementModalAdd = $('.ui.modal.wishlist-wish-add'); var elementButtons = elementModalAdd.find('.actions .button'); - var elementTitle = elementModalAdd.find('[name="wish_name"]'); + var elementTitle = elementModalAdd.find('[name="wish_title"]'); var elementDescription = elementModalAdd.find('[name="wish_description"]'); buttonValidate.addClass('disabled loading'); elementButtons.addClass('disabled'); - fetch('/src/api/cache.php?wish_url=' + inputWishURL.val(), { + fetch('/src/api/wishes.php?wish_url=' + inputWishURL.val(), { method: 'GET' }) - .then(response => response.json()) - .then(response => { - if (response.success) { - var info = response.data; + .then(handleFetchError) + .then(handleFetchResponse) + .then(function(response) { + var info = response.info; - /** - * Title - */ - if (info.title && elementTitle.length) { - elementTitle.val(info.title); - } - - /** - * Description - */ - if (info.description && elementDescription.length) { - elementDescription.text(info.description); - } - - /** - * URL - */ - if (info.url && info.url !== inputWishURL.val()) { - var elementModalFetch = $('.ui.modal.wishlist-wish-fetch'); - - elementModalFetch.find('input.current').val(inputWishURL.val()); - elementModalFetch.find('input.proposed').val(info.url); - - elementButtons.addClass('disabled'); - - elementModalFetch - .modal({ - allowMultiple: true, - closable: false, - onApprove: function (buttonFetch) { - var formData = new URLSearchParams(); - formData.append('wish_url_current', inputWishURL.val()); - formData.append('wish_url_proposed', info.url); - - buttonFetch.addClass('loading'); - - fetch('/src/api/wishes.php', { - method: 'PUT', - body: formData - }) - .then(response => response.json()) - .then(response => { - if (response.success) { - inputWishURL.val(info.url); - - elementModalFetch.modal('hide'); - } - - buttonFetch.removeClass('loading'); - }); - - return false; - }, - onHide: function() { - buttonValidate.removeClass('disabled loading'); - elementButtons.removeClass('disabled'); - } - }) - .modal('show'); - } else { - buttonValidate.removeClass('disabled loading'); - elementButtons.removeClass('disabled'); - } - - inputURLContainer.attr('data-validated', 'true'); + /** + * Title + */ + if (info.title && elementTitle.length) { + elementTitle.val(info.title); } + + /** + * Description + */ + if (info.description && elementDescription.length) { + elementDescription.val(info.description); + } + + /** + * URL + */ + if (info.url && info.url !== inputWishURL.val()) { + var elementModalFetch = $('.ui.modal.wishlist-wish-fetch'); + + elementModalFetch.find('input.current').val(inputWishURL.val()); + elementModalFetch.find('input.proposed').val(info.url); + + elementButtons.addClass('disabled'); + + elementModalFetch + .modal({ + allowMultiple: true, + closable: false, + onApprove: function (buttonFetch) { + var formData = new URLSearchParams(); + formData.append('wish_url_current', inputWishURL.val()); + formData.append('wish_url_proposed', info.url); + + buttonFetch.addClass('loading'); + + fetch('/src/api/wishes.php', { + method: 'PUT', + body: formData + }) + .then(response => response.json()) + .then(response => { + if (response.success) { + inputWishURL.val(info.url); + + elementModalFetch.modal('hide'); + } + + buttonFetch.removeClass('loading'); + }); + + return false; + }, + onHide: function() { + buttonValidate.removeClass('disabled loading'); + elementButtons.removeClass('disabled'); + } + }) + .modal('show'); + } else { + buttonValidate.removeClass('disabled loading'); + elementButtons.removeClass('disabled'); + } + }) + .catch(function(error) { + console.log(error); }); + }); /** diff --git a/src/classes/embed-cache.php b/src/classes/embed-cache.php index 8be6f64e..05391e5b 100644 --- a/src/classes/embed-cache.php +++ b/src/classes/embed-cache.php @@ -16,8 +16,6 @@ class EmbedCache * Private */ private string $directory = ROOT . '/src/cache'; - private string $noImage = '/src/assets/img/no-image.svg'; - private string $filepath; private function getIdentifier(): string @@ -39,9 +37,9 @@ class EmbedCache public function get(bool $generateCache = false): mixed { - $info = null; - $maxAge = 2592000; // 30 days - $age = file_exists($this->getFilepath()) ? time() - filemtime($this->getFilepath()) : $maxAge; + $info = null; + $maxAge = 2592000; // 30 days + $age = file_exists($this->getFilepath()) ? time() - filemtime($this->getFilepath()) : $maxAge; if ($this->exists() && $age <= $maxAge && false === $generateCache) { $info = json_decode(file_get_contents($this->getFilepath())); @@ -60,7 +58,7 @@ class EmbedCache $info_simplified->favicon = ''; $info_simplified->feeds = array(); $info_simplified->icon = ''; - $info_simplified->image = $this->noImage; + $info_simplified->image = ''; $info_simplified->keywords = array(); $info_simplified->language = ''; $info_simplified->languages = array(); @@ -84,7 +82,7 @@ class EmbedCache $info_simplified->favicon = (string) $info->favicon; $info_simplified->feeds = (array) $info->feeds; $info_simplified->icon = (string) $info->icon; - $info_simplified->image = $info->image ? (string) $info->image : $this->noImage; + $info_simplified->image = (string) $info->image; $info_simplified->keywords = (array) $info->keywords; $info_simplified->language = (string) $info->language; $info_simplified->languages = (array) $info->languages; @@ -98,7 +96,7 @@ class EmbedCache } catch (\Throwable $ex) { $generateCache = false; - $info_simplified->title = $ex->getMessage(); + $info_simplified->description = $ex->getMessage(); } } diff --git a/src/classes/wish.php b/src/classes/wish.php new file mode 100644 index 00000000..aa921a57 --- /dev/null +++ b/src/classes/wish.php @@ -0,0 +1,149 @@ + + */ + +namespace wishthis; + +class Wish +{ + public int $id; + public int $wishlist; + public ?string $title; + public ?string $description; + public ?string $url; + public ?string $status; + + public function __construct(int|array $wish) + { + global $database; + + $columns = array(); + + if (is_int($wish)) { + $wish = $database + ->query('SELECT * + FROM `wishes` + WHERE `id` = ' . $wish . ';') + ->fetch(); + + $columns = $wish; + } elseif (is_array($wish)) { + $columns = $wish; + } + + if ($columns) { + foreach ($columns as $key => $value) { + $this->$key = $value; + } + } + } + + public function getCard(int $ofUser, bool $generateCache = false): string + { + ob_start(); + + /** + * Card + */ + $userIsCurrent = isset($_SESSION['user']['id']) && intval($_SESSION['user']['id']) === $ofUser; + + if ($this->url) { + $cache = new EmbedCache($this->url); + $info = $cache->get($generateCache); + $exists = $cache->exists() || !$this->url ? 'true' : 'false'; + } else { + $exists = 'true'; + } + + $title = $this->title ?? $info->title ?? null; + $description = $this->description ?? $info->description ?? null; + $url = $this->url ?? $info->url ?? null; + + $image = $info->image ?? '/src/assets/img/no-image.svg'; + $favicon = $info->favicon ?? null; + $providerName = $info->providerName ?? null; + $keywords = $info->keywords ?? null; + ?> + +
+
+ +
+ + + + + + + + + + + + + + + +
+ +
+ +
+ + + + + +
+ + + +
+ +
+ + + +
+ +
+
+ +
+ + +
+ wishes = $database->query('SELECT * - FROM `wishes` - WHERE `wishlist` = ' . $this->id . ';') - ->fetchAll(); + $this->wishes = $database + ->query('SELECT * + FROM `wishes` + WHERE `wishlist` = ' . $this->id . ';') + ->fetchAll(); + + foreach ($this->wishes as &$wish) { + $wish = new Wish($wish); + } } public function getCards($options = array()): string @@ -71,7 +76,7 @@ class Wishlist if ($exclude) { $wishes = array_filter($this->wishes, function ($wish) use ($exclude) { - return !in_array($wish['status'], $exclude); + return !in_array($wish->status, $exclude); }); } else { $wishes = $this->wishes; @@ -80,96 +85,14 @@ class Wishlist /** * Cards */ - $userIsCurrent = isset($_SESSION['user']) && $this->data['user'] === $_SESSION['user']['id']; - $cardIndex = 0; - if (!empty($wishes)) { ?>
- get(false); - $exists = $cache->exists() || !$info->url ? 'true' : 'false'; - - $title = $wish['title'] ?? $info->title; - $description = $wish['description'] ?? $info->description; - ?> +
-
-
- -
- image) { ?> - - - - favicon) { ?> - - - - providerName) { ?> - providerName ?> - - - url) { ?> - - -
- -
- -
- url) { ?> - - - - -
- - - keywords) { ?> -
- keywords) ?> -
- - - -
- -
-
- -
- -
- - - - Commit - - - - url) { ?> - - - Visit - - - - - - - Delete - - -
-
+ getCard($this->data['user'], false) ?>
- -
diff --git a/src/pages/install.php b/src/pages/install.php index 1f54114a..b04954df 100644 --- a/src/pages/install.php +++ b/src/pages/install.php @@ -148,6 +148,7 @@ switch ($step) { REFERENCES `wishlists` (`id`) ON DELETE CASCADE );'); + $database->query('CREATE INDEX `idx_url` ON `wishes` (`url`);'); /** * Options