From ac57daee5e5a7b08bf37d44675c0ce4b0c780761 Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sat, 31 Dec 2022 13:41:15 +0100 Subject: [PATCH] Add wish edit modal --- src/assets/js/parts/wish.js | 78 ++++++++++++++++++++++++++-- src/assets/js/parts/wishlists.js | 89 ++------------------------------ src/pages/parts/wish-add.php | 41 +++++---------- src/pages/parts/wishlist.php | 44 ++++++++-------- 4 files changed, 116 insertions(+), 136 deletions(-) diff --git a/src/assets/js/parts/wish.js b/src/assets/js/parts/wish.js index cc22cf6b..d0b3d804 100644 --- a/src/assets/js/parts/wish.js +++ b/src/assets/js/parts/wish.js @@ -1,5 +1,7 @@ const wish_button_mark_as_fulfilled = '.ui.button.wish-fulfilled'; const wish_button_visit = '.ui.button.wish-visit'; +const wish_button_options = '.ui.button.wish-options'; +const wish_button_options_edit = wish_button_options + ' .item.wish-edit'; var wish; @@ -13,6 +15,16 @@ function wish_set_to(wish_data) { .attr('href', wish.url) .removeClass('disabled'); } + + /** + * Options + */ + $(wish_button_options) + .dropdown() + .removeClass('disabled'); + + /** Edit */ + $(wish_button_options_edit).removeClass('disabled'); } function wish_unset() { @@ -37,9 +49,7 @@ $(function () { * Dirty hack to change the default `display: block;` to * `display: flex;` (using CSS). */ - setTimeout(() => { - $(this).css('display', ''); - }, 0); + setTimeout(() => { $(this).css('display', ''); }, 0); }, 'onHide' : function(modal) { wish_unset(); @@ -130,4 +140,66 @@ $(function () { }); /** */ + /** + * Options: Edit + */ + $(document).on('click', wish_button_options_edit, function() { + var wish_edit_template = $('template#wish-edit').clone(true, true); + var wish_edit = wish_edit_template.contents().filter(function() { return this.nodeType !== 3; }); + + /** + * Initialise + */ + /** Checkbox */ + const checkbox_is_purchasable = wish_edit.find('.ui.checkbox.wish-is-purchasable'); + + checkbox_is_purchasable + .checkbox({ + onChecked : function() { + wish_edit.find('.item[data-tab="product"]').removeClass('disabled'); + }, + onUnchecked : function() { + wish_edit.find('.item[data-tab="product"]').addClass('disabled'); + }, + }); + + /** + * Set values + */ + /** + * Save global-scope wish since it will be unset when the wish-details + * modal is closed. + */ + var wish_local = wish; + + wish_edit.modal('show'); + + /** Initialise Tabs */ + wish_edit.find('.item[data-tab]') + .tab(); + + /** General */ + $('[name="wish_id"]').val(wish_local.id); + $('[name="wish_title"]').val(wish_local.title); + $('[name="wish_description"]').val(wish_local.description); + $('[name="wish_image"]').val(wish_local.image); + $('[name="wish_url"]').val(wish_local.url); + $('.ui.selection.dropdown.priority').dropdown('set selected', wish_local.priority); + + if (wish_local.is_purchasable) { + checkbox_is_purchasable.checkbox('check'); + } else { + checkbox_is_purchasable.checkbox('uncheck'); + } + + /** Product */ + $('[name="wish_price"]').val(wish_local.price); + }); + /** */ + + /** + * Options: Delete + */ + /** */ + }); diff --git a/src/assets/js/parts/wishlists.js b/src/assets/js/parts/wishlists.js index 7fa844fc..403f6d5f 100644 --- a/src/assets/js/parts/wishlists.js +++ b/src/assets/js/parts/wishlists.js @@ -1,3 +1,7 @@ +/** + * The currently selected wishlist. + */ +var wishlist; var wishlists = []; $(function () { @@ -307,91 +311,6 @@ $(function () { } }); - /** - * Edit Wish - */ - $(document).on('click', '.wish-edit', function (event) { - validateURL = true; - - /** Form */ - var formEdit = $('.form.wishlist-wish-edit'); - formEdit.addClass('loading'); - formEdit.trigger('reset'); - formEdit.find('.dropdown').dropdown('restore defaults'); - formEdit.find('.item').tab('change tab', 'general'); - - /** Checkbox */ - formEdit - .find('.checkbox') - .checkbox({ - onChecked : function() { - formEdit.find('.item[data-tab="product"]').removeClass('disabled'); - }, - onUnchecked : function() { - formEdit.find('.item[data-tab="product"]').addClass('disabled'); - }, - }) - .checkbox('uncheck'); - - /** Get Wish */ - var wishID = $(this).attr('data-id'); - - var wishFormData = new URLSearchParams( - { - 'module' : 'wishes', - 'page' : 'api', - - 'wish_id' : wishID - } - ); - - fetch('/?' + wishFormData, { - method: 'GET' - }) - .then(handleFetchError) - .then(handleFetchResponse) - .then(function(response) { - var wish = response.info; - - /** General */ - $('[name="wish_id"]').val(wish.id); - $('[name="wish_title"]').val(wish.title); - $('[name="wish_description"]').val(wish.description); - $('[name="wish_image"]').val(wish.image); - $('[name="wish_url"]').val(wish.url); - $('.ui.selection.dropdown.priority').dropdown('set selected', wish.priority); - - if (wish.is_purchasable) { - formEdit.find('.checkbox').checkbox('check'); - } else { - formEdit.find('.checkbox').checkbox('uncheck'); - } - - /** Product */ - $('[name="wish_price"]').val(wish.price); - }) - .catch(handleFetchCatch) - .finally(function() { - formEdit.removeClass('loading'); - }); - - /** Save wish */ - var modalWishlistWishEdit = $('.ui.modal.wishlist-wish-edit'); - - modalWishlistWishEdit.find('[name="wishlist_id"]').val($('.ui.dropdown.wishlists').dropdown('get value')); - modalWishlistWishEdit - .modal({ - autoShow : true, - onApprove : function (buttonSave) { - validateWishURL(formEdit, buttonSave, modalWishlistWishEdit, validateURL); - - return false; - } - }); - - event.preventDefault(); - }); - /** * Delete Wish */ diff --git a/src/pages/parts/wish-add.php b/src/pages/parts/wish-add.php index 9b115356..16a4260c 100644 --- a/src/pages/parts/wish-add.php +++ b/src/pages/parts/wish-add.php @@ -13,7 +13,7 @@ namespace wishthis; -
+
@@ -22,10 +22,8 @@ namespace wishthis;
-
@@ -35,7 +33,7 @@ namespace wishthis;
- +
@@ -46,10 +44,8 @@ namespace wishthis;
-
@@ -58,16 +54,12 @@ namespace wishthis;
@@ -75,10 +67,8 @@ namespace wishthis;
-
@@ -87,11 +77,10 @@ namespace wishthis;
-
+
is_purchasable) && $wish->is_purchasable ? 'checked' : '' ?> + name="wish_is_purchasable" />
@@ -111,10 +100,8 @@ namespace wishthis;
-
diff --git a/src/pages/parts/wishlist.php b/src/pages/parts/wishlist.php index ece31cb1..e8171c31 100644 --- a/src/pages/parts/wishlist.php +++ b/src/pages/parts/wishlist.php @@ -156,32 +156,34 @@ namespace wishthis;
-