Replace number input with dropdown on "Add a product"

This commit is contained in:
Jay Trees 2022-01-13 16:43:07 +01:00
parent bbadf90874
commit d20fcfaadb
6 changed files with 173 additions and 14 deletions

View file

@ -0,0 +1,39 @@
<?php
/**
* wishlists.php
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
use wishthis\User;
$api = true;
$response = array(
'success' => false,
);
require '../../index.php';
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
if (isset($_GET['userid'])) {
$user = new User($_GET['userid']);
$wishlists = $user->getWishlists();
$wishlists = array_map(function ($wishlist) {
return array(
'name' => $wishlist['name'],
'value' => $wishlist['id'],
'text' => $wishlist['name'],
);
}, $wishlists);
$response['results'] = $wishlists;
$response['success'] = true;
}
break;
}
echo json_encode($response);
header('Content-type: application/json; charset=utf-8');
die();

View file

@ -0,0 +1,50 @@
$(function() {
$.fn.api.settings.api = {
'get wishlists' : '/includes/api/wishlists.php'
};
$('.ui.dropdown.wishlists').dropdown({
filterRemoteData: true
});
$('.ui.dropdown.wishlists').api({
action: 'get wishlists',
method: 'GET',
data : {
userid: 1
},
on: 'now',
onResponse: function(response) {
console.log('onResponse');
// make some adjustments to response
return response;
},
successTest: function(response) {
console.log('successTest');
// test whether a JSON response is valid
return response.success || false;
},
onComplete: function(response, element, xhr) {
$('.ui.dropdown.wishlists').removeClass('loading');
},
onSuccess: function(response, element, xhr) {
$('.ui.dropdown.wishlists')
.dropdown({
values: response.results
})
.dropdown('set selected', response.results[0].value);
},
onFailure: function(response, element, xhr) {
console.log('onFailure');
// request failed, or valid response but response.success = false
},
onError: function(errorMessage, element, xhr) {
console.log('onError');
// invalid response
},
onAbort: function(errorMessage, element, xhr) {
console.log('onAbort');
// navigated to a new page, CORS issue, or user canceled request
}
});
});

View file

@ -54,10 +54,26 @@ class Page
echo '<link rel="stylesheet" href="' . $stylesheetPage . '?m=' . $stylesheetPageModified . '" />';
}
?>
<script defer src="/node_modules/jquery/dist/jquery.min.js"></script>
<script defer src="/semantic/dist/semantic.min.js"></script>
/**
* Scripts
*/
/** jQuery */
$scriptjQuery = 'node_modules/jquery/dist/jquery.min.js';
$scriptjQueryModified = filemtime($scriptjQuery);
echo '<script defer src="' . $scriptjQuery . '?m=' . $scriptjQueryModified . '"></script>';
/** Fomantic */
$scriptFomantic = 'semantic/dist/semantic.min.js';
$scriptFomanticModified = filemtime($scriptFomantic);
echo '<script defer src="' . $scriptFomantic . '?m=' . $scriptFomanticModified . '"></script>';
/** Default */
$scriptDefault = 'includes/assets/js/default.js';
$scriptDefaultModified = filemtime($scriptDefault);
echo '<script defer src="' . $scriptDefault . '?m=' . $scriptDefaultModified . '"></script>';
?>
<title><?= $this->title ?> - wishthis</title>
</head>
@ -65,7 +81,8 @@ class Page
<?php
}
public function navigation(): void {
public function navigation(): void
{
?>
<div class="ui attached stackable menu">
<div class="ui container">

44
includes/classes/user.php Normal file
View file

@ -0,0 +1,44 @@
<?php
/**
* user.php
*
* A wishthis user.
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
namespace wishthis;
class User
{
public int $id;
public function __construct(int $id = -1)
{
if (-1 === $id) {
$this->id = $_SESSION['user']['id'];
} else {
$this->id = $id;
}
}
/**
* Returns a list of the users wishlists.
* Defaults to the currently logged in user.
*
* @return array
*/
public function getWishlists(): array
{
global $database;
$wishlists = $database->query(
'SELECT *
FROM wishlists
WHERE user = ' . $_SESSION['user']['id'] . ';'
)->fetchAll();
return $wishlists;
}
}

View file

@ -6,7 +6,7 @@
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
use wishthis\Page;
use wishthis\{Page, User};
use Embed\Embed;
if (isset($_POST['url'], $_POST['wishlist'])) {
@ -28,6 +28,7 @@ $info = $embed->get($url);
$page = new page(__FILE__, 'Add a product');
$page->header();
$page->navigation();
$user = new User();
?>
<main>
<div class="ui container">
@ -41,8 +42,9 @@ $page->navigation();
</div>
<div class="field">
<label>Wishlist</label>
<input type="number" name="wishlist" value="1" />
<select class="ui search selection dropdown loading wishlists" name="wishlist">
<option value="">Loading your wishlists...</option>
</select>
</div>
<input class="ui primary button" type="submit" value="Add" />

View file

@ -6,7 +6,7 @@
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
/**
/**
* Include
*/
require 'vendor/autoload.php';
@ -17,7 +17,7 @@ $include = new Grandel\IncludeDirectory(__DIR__ . '/includes/functions');
/**
* Config
*/
$configPath = 'includes/config/config.php';
$configPath = __DIR__ . '/' . 'includes/config/config.php';
if (file_exists($configPath)) {
require $configPath;
@ -42,6 +42,18 @@ if (
);
}
/**
* Session
*/
session_start();
/**
* API
*/
if (isset($api)) {
return;
}
/**
* Install
*/
@ -53,11 +65,6 @@ if ($database) {
}
}
/**
* Session
*/
session_start();
/**
* Page
*/