Modulized website and added delete-function

master
Eirik Th S 2021-04-14 10:16:27 +02:00
parent 1e396f1c51
commit 7e1a76c475
5 changed files with 72 additions and 42 deletions

View File

@ -6,11 +6,11 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grocery Assist</title>
<link rel="stylesheet" href="/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="/css/index.css" type="text/css" />
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="css/index.css" type="text/css" />
<script src="/js/jquery-3.5.1.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="js/jquery-3.5.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body id="home">
@ -20,10 +20,9 @@
<div class="container-sm" style="padding-top: 5px;">
<div class="card-group" style="text-align: center;">
<!--<div class="row row-cols-1 row-cols-sm-2" style="margin: auto; max-width: 900px;"> <!-- .row-cols-lg-4 -->
<div class="card h-100" style="">
<a href='/plan'>
<a href='./plan'>
<img src="#" class="card-img-top" alt="Plan">
<div class="card-body">
<h5 class="card-title">Plan shopping</h5>
@ -36,10 +35,10 @@
<img src="#" class="card-img-top" alt="Plan"></a>
<div class="card-body">
<h5 class="card-title">Review</h5>
<p class="card-text"></p>
<p class="card-text">Coming soon</p>
</div>
</div>
<!-- </div> -->
</div>

View File

@ -227,13 +227,23 @@ function renameStore($storeID, $newName){
function deleteStore($storeID, $storeName, $itemsLength){
global $db, $user_id;
$verifyOwnerSql = "SELECT plan_store_id FROM plan_store WHERE `user_id` = '$user_id' AND `plan_store_id` = '$storeID' AND `name` = '$storeName'";
$getItemsSql = "SELECT count(0) FROM plan_store_items WHERE plan_store_id = ($verifyOwnerSql)";
$verifyOwnerSql = "SELECT `plan_store_id` FROM plan_store WHERE `user_id` = '$user_id' AND `plan_store_id` = '$storeID' AND `name` = '$storeName'";
$getItemsSql = "SELECT `plan_store_id` FROM plan_store_item WHERE `plan_store_id` = ($verifyOwnerSql)";
if($getItemsSql = $db->query($getItemsSql) && $getItemsSql->fetch_row()[0] = $itemsLength){
if(($getItemsSql = $db->query($getItemsSql)) &&
$getItemsSql->num_rows == $itemsLength &&
($getItemsSql->num_rows == 0 || $getItemsSql->fetch_row()[0] == $storeID)){
// DELETE QUERIES HERE
$deleteSql = "";
if($getItemsSql->num_rows > 0){
$deleteSql .= "DELETE FROM plan_store_item WHERE `plan_store_id` = '$storeID';\n";
}
$deleteSql .= "DELETE FROM plan_store WHERE `user_id` = '$user_id' AND `plan_store_id` = '$storeID' AND `name` = '$storeName';";
return true;
if($db->multi_query($deleteSql)){
return true;
}
}
return false;

View File

@ -6,12 +6,12 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PLAN - Grocery Assist</title>
<link rel="stylesheet" href="/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="/css/index.css" type="text/css" />
<link rel="stylesheet" href="../css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="../css/index.css" type="text/css" />
<script src="/js/jquery-3.5.1.min.js"></script>
<script src="../js/jquery-3.5.1.min.js"></script>
<script src="//unpkg.com/@popperjs/core@2"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
</head>
<body id='plan'>
<?php include '../webdata/navbar.php'; ?>

View File

@ -13,8 +13,8 @@ class Store {
html += "<div class='card store' style='width: 20rem;'>";
html += " <div class='card-header'>"+this.title+"</div>";
html += " <div class='iconWrapper'>";
html += " <img src='/icon/pencil-square.svg' class='editStoreName' alt='edit name' data-toggle='tooltip' title='Edit store name' tabindex=0 role='button' />";
html += " <img src='/icon/x-circle.svg' class='removeStore' alt='remove store' data-toggle='tooltip' title='Remove store' tabindex=0 role='button' />";
html += " <img src='../icon/pencil-square.svg' class='editStoreName' alt='edit name' data-toggle='tooltip' title='Edit store name' tabindex=0 role='button' />";
html += " <img src='../icon/x-circle.svg' class='removeStore' alt='remove store' data-toggle='tooltip' title='Remove store' tabindex=0 role='button' />";
html += " </div>";
html += " <div class='card-body'>";
html += " <ul class='list-group list-group-flush'>";
@ -25,10 +25,10 @@ class Store {
html += " <input type='text' class='form-control newItemName' placeholder='New Item Name' data-toggle='tooltip' title='New Item Name' aria-label='New Item Name'>";
html += " <input type='number' class='form-control newItemPrice' value='0' min='0' step='.01' data-toggle='tooltip' title='Price' aria-label='Price'>";
html += " <div class='input-group-append'>";
html += " <input type='image' class='form-control addItem' src='/icon/plus.svg' alt='+'>";
html += " <input type='image' class='form-control addItem' src='../icon/plus.svg' alt='+'>";
html += " </div>";
html += " </form>";
html += " <button class='save'>Save</button>";
// html += " <button class='save'>Save</button>";
html += " </div>";
html += " <div class='card-footer subtotal'>Subtotal: <span class='price'>0.00 kr</span></div>";
html += "</div>";
@ -65,7 +65,7 @@ class Store {
/** DEV **/
this.selector.find('.save').click(ev => { this.save(); });
// this.selector.find('.save').click(ev => { this.save(); });
}
addItem(text, price){
@ -103,7 +103,7 @@ class Store {
try {
price = Number(price);
this.items.push({ text: text, price: price });
this.selector.find("ul").append("<li class='list-group-item'><span>"+text+"</span><span class='price'>"+price.toFixed(2)+" kr <button class='remItem' data-price='"+price+"' data-toggle='tooltip' title='Remove item'><img class='iconBtn' src='/icon/dash.svg' alt='-' style='height: 16px; width: 16px;' /></button></span></li>");
this.selector.find("ul").append("<li class='list-group-item'><span>"+text+"</span><span class='price'>"+price.toFixed(2)+" kr <button class='remItem' data-price='"+price+"' data-toggle='tooltip' title='Remove item' style='background: url(../icon/dash.svg); height: 20px; width: 20px;'></button></span></li>");
this.selector.find(".emptyList").hide();
this.verify();
return true;
@ -173,27 +173,43 @@ class Store {
});
}
save(){
$.ajax({
method: "POST",
url: "do.php",
data: { plan: 'saveStore', storeName: this.title, items: this.items },
dataType: 'JSON'
})
.done(json => {
// json = JSON.parse(body);
// save(){
// $.ajax({
// method: "POST",
// url: "do.php",
// data: { plan: 'saveStore', storeName: this.title, items: this.items },
// dataType: 'JSON'
// })
// .done(json => {
// // json = JSON.parse(body);
if(handleJsonErrors(json)){
return;
}
// if(handleJsonErrors(json)){
// return;
// }
console.log(json);
})
.fail(handleAjaxErrors);
}
// // console.log(json);
// })
// .fail(handleAjaxErrors);
// }
removeStore(){
this.selector.remove();
if(this.storeID != null){
$.ajax({
method: "POST",
url: "do.php",
data: { plan: 'deleteStore', storeID: this.storeID, storeName: this.title, itemsLength: this.items.length },
dataType: 'JSON'
})
.done(json => {
if(handleJsonErrors(json)){
return;
}
console.log("Delete store response:",json);
})
.fail(handleAjaxErrors);
}
}
getStoreID(){

View File

@ -1,4 +1,9 @@
<?php $activePage = $_SERVER['REQUEST_URI']; if($activePage == "/index.php"){ $activePage = "/"; } ?><nav class="navbar navbar-expand-sm navbar-light bg-light" style="z-index: 2;">
<?php
$projectRoot = isset($projectRoot)?$projectRoot:"";
$activePage = $_SERVER['REQUEST_URI'];
if($activePage == "/index.php"){ $activePage = "/"; }
?><nav class="navbar navbar-expand-sm navbar-light bg-light" style="z-index: 2;">
<div class="container-fluid" style="max-width: 900px;">
<a class="navbar-brand" href="/"><img src='#' style="height: 24px;" alt='Groceries' /></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
@ -9,9 +14,9 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-sm-0">
<?php
$links["Home"] = "/";
$links["Plan"] = "/plan/";
$links["Review"] = "/review/";
$links["Home"] = $projectRoot."/";
$links["Plan"] = $projectRoot."/plan/";
$links["Review"] = "#";
foreach($links as $title => $href){
echo '<li class="nav-item">';
echo '<a class="nav-link'.($activePage == $href?" active\" aria-current=\"page":"").'" href="'.$href.'">'.$title.'</a>';