Edit wish in a modal instead of page

This commit is contained in:
grandeljay 2022-06-10 09:07:02 +02:00
parent c977ba0605
commit 363788a9db
4 changed files with 167 additions and 30 deletions

View file

@ -17,23 +17,25 @@ require '../../index.php';
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
if (isset($_GET['wish_id'], $_GET['wishlist_user'])) {
if (isset($_GET['wish_id'])) {
$columns = $database
->query('SELECT *
FROM `wishes`
WHERE `id` = ' . $_GET['wish_id'] . ';')
->query(
'SELECT *
FROM `wishes`
WHERE `id` = ' . $_GET['wish_id'] . ';'
)
->fetch();
$wish = new Wish($columns, true);
$response = array(
'info' => $wish,
'html' => $wish->getCard($_GET['wishlist_user'])
);
$response['info'] = $wish;
if (isset($_GET['wishlist_user'])) {
$response['html'] = $wish->getCard($_GET['wishlist_user']);
}
} elseif (isset($_GET['wish_url'])) {
$cache = new Cache\Embed($_GET['wish_url']);
$info = $cache->get(true);
$exists = $cache->exists() ? 'true' : 'false';
$cache = new Cache\Embed($_GET['wish_url']);
$info = $cache->get(true);
$response = array(
'info' => $info,
@ -42,9 +44,9 @@ switch ($_SERVER['REQUEST_METHOD']) {
break;
case 'POST':
if (isset($_POST['wishlist_id'])) {
if (isset($_POST['wishlist_id']) || isset($_POST['wish_id'])) {
/**
* Insert New Wish
* Save wish
*/
if (
empty($_POST['wish_title'])
@ -54,28 +56,62 @@ switch ($_SERVER['REQUEST_METHOD']) {
break;
}
$wishlist_id = $_POST['wishlist_id'];
$wish_title = trim($_POST['wish_title']);
$wish_description = trim($_POST['wish_description']);
$wish_url = trim($_POST['wish_url']);
$wish_priority = isset($_POST['wish_priority']) && $_POST['wish_priority'] ? $_POST['wish_priority'] : 'NULL';
$wish_title = trim($_POST['wish_title']);
$wish_description = trim($_POST['wish_description']);
$wish_url = trim($_POST['wish_url']);
$wish_priority = isset($_POST['wish_priority']) && $_POST['wish_priority'] ? $_POST['wish_priority'] : 'NULL';
$wish_is_purchasable = isset($_POST['wish_is_purchasable']);
$database
->query('INSERT INTO `wishes`
(
if (isset($_POST['wish_id'], $_POST['wishlist_id'])) {
/** Update wish */
$wish_id = $_POST['wish_id'];
$wishlist_id = $_POST['wishlist_id'];
$database
->query(
'REPLACE INTO `wishes`
(
`id`,
`wishlist`,
`title`,
`description`,
`url`,
`priority`
) VALUES (
' . $wishlist_id . ',
"' . $wish_title . '",
"' . $wish_description . '",
"' . $wish_url . '",
' . $wish_priority . '
)
;');
`priority`,
`is_purchasable`
) VALUES (
' . $wish_id . ',
' . $wishlist_id . ',
"' . $wish_title . '",
"' . $wish_description . '",
"' . $wish_url . '",
' . $wish_priority . ',
' . $wish_is_purchasable . '
);'
);
} elseif (isset($_POST['wishlist_id'])) {
/** Insert wish */
$wishlist_id = $_POST['wishlist_id'];
$database
->query(
'INSERT INTO `wishes`
(
`wishlist`,
`title`,
`description`,
`url`,
`priority`,
`is_purchasable`
) VALUES (
' . $wishlist_id . ',
"' . $wish_title . '",
"' . $wish_description . '",
"' . $wish_url . '",
' . $wish_priority . ',
' . $wish_is_purchasable . '
);'
);
}
$response['data'] = array(
'lastInsertId' => $database->lastInsertId(),

View file

@ -321,6 +321,78 @@ $(function () {
});
});
/**
* Edit Wish
*/
$(document).on('click', '.wish-edit', function (event) {
/** Form */
var form = $('.form.wishlist-wish-edit');
form.addClass('loading');
form.trigger('reset');
form.find('.dropdown').dropdown('restore defaults');
/** Get Wish */
var wishID = $(this).attr('data-id');
var wishFormData = new URLSearchParams({
'wish_id' : wishID
});
fetch('/src/api/wishes.php?' + wishFormData, {
method: 'GET'
})
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
var wish = response.info;
$('[name="wish_id"]').val(wish.id);
$('[name="wish_title"]').val(wish.title);
$('[name="wish_description"]').val(wish.description);
$('[name="wish_url"]').val(wish.url);
$('.ui.selection.dropdown.priority').dropdown('set selected', wish.priority);
$('[name="wish_is_purchasable"]').prop('checked', wish.is_purchasable);
})
.catch(handleFetchCatch)
.finally(function() {
form.removeClass('loading');
});
/** Modal */
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) {
buttonSave.addClass('loading');
var formData = new URLSearchParams(new FormData(form[0]));
fetch('/src/api/wishes.php', {
method : 'POST',
body : formData
})
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
$('body').toast({ message: text.toast_wish_update });
wishlistsRefresh();
modalWishlistWishEdit.modal('hide');
buttonSave.removeClass('loading');
})
.catch(handleFetchCatch);
return false;
}
});
event.preventDefault();
});
/**
* Delete Wish
*/

View file

@ -50,6 +50,8 @@ class Wish
public ?string $image;
public ?string $url;
public ?int $priority;
public bool $is_purchasable;
public ?string $status;
public \stdClass $info;

View file

@ -188,6 +188,33 @@ $page->navigation();
</div>
</div>
<!-- Wishlist: Edit a wish -->
<div class="ui modal wishlist-wish-edit">
<div class="header">
<?= __('Edit wish') ?>
</div>
<div class="content">
<div class="description">
<form class="ui form wishlist-wish-edit" method="POST">
<input type="hidden" name="wish_id" />
<input type="hidden" name="wishlist_id" />
<div class="ui two column grid">
<?php include 'parts/wish-add.php' ?>
</div>
</form>
</div>
</div>
<div class="actions">
<div class="ui primary approve button" title="<?= __('Save') ?>">
<?= __('Save') ?>
</div>
<div class="ui deny button" title="<?= __('Cancel') ?>">
<?= __('Cancel') ?>
</div>
</div>
</div>
<?php
$page->footer();
$page->bodyEnd();