Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #86 from hardingadonis/hoang_admin_dashboard
Browse files Browse the repository at this point in the history
Hoang admin dashboard
  • Loading branch information
GoldStarPro authored Dec 8, 2023
2 parents 46c0b43 + f4914d2 commit 7830da0
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.hardingadonis.miu.controller.admin;

import io.hardingadonis.miu.model.*;
import io.hardingadonis.miu.model.detail.OrderStatus;
import io.hardingadonis.miu.services.Singleton;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import org.json.simple.JSONObject;

@WebServlet(name = "OrderAdmin", urlPatterns = {"/admin/order"})
public class OrderAdmin extends HttpServlet {
Expand All @@ -14,7 +17,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

HttpSession session = request.getSession();

Admin admin = (Admin) session.getAttribute("admin");
Expand All @@ -30,6 +33,35 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

OrderStatus orderStatus = OrderStatus.create(request.getParameter("status"));

try {
int id = Integer.parseInt(request.getParameter("id"));

Order order = Singleton.orderDAO.get(id);

if (order != null) {
order.setStatus(orderStatus);

Singleton.orderDAO.update(order);
}
} catch (NumberFormatException ex) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);

return;
}

JSONObject jsonResponse = new JSONObject();
jsonResponse.put("status", "success");
jsonResponse.put("message", "Status updated successfully");

response.setContentType("application/json");
response.getWriter().write(jsonResponse.toString());

response.setStatus(HttpServletResponse.SC_OK);
}

}
4 changes: 4 additions & 0 deletions src/main/java/io/hardingadonis/miu/model/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public class Order {
public Order() {
}

public Order(OrderStatus status) {
this.status = status;
}

public Order(int userID, String address, long totalPrice, Payment payment, OrderStatus status) {
this.userID = userID;
this.address = address;
Expand Down
67 changes: 25 additions & 42 deletions src/main/webapp/assets/js/admin/edit-category.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

var categoryId;
var selectedRowIndex;

function openEditModal() {
$('#editCategoryModal').modal('show');
// Lưu index của hàng được chọn
selectedRowIndex = $(this).closest('tr').index();
}

// Function to populate fields in the edit modal with existing data
Expand All @@ -12,53 +13,35 @@ function populateEditModalFields(row) {
document.getElementById('editCategoryName').value = categoryName;
}

function openEditModel(id) {
categoryId = id;

openEditModal();
}
function saveChangesEditCategory() {
// Lấy giá trị từ modal
var editedCategoryName = document.getElementById('editCategoryName').value;

function saveCategoryChanges() {
let editedCategoryName = document.getElementById('editCategoryName').value;
// Kiểm tra giá trị có đọc được không
console.log("Edited Category Name:", editedCategoryName);

// Get the category ID from the row or another source
// Cập nhật hàng trong bảng
var rowToUpdate = $('#datatablesSimple tbody tr').eq(selectedRowIndex);
console.log("Row to update:", rowToUpdate);

const url = contextPath + '/admin/category?id=' + categoryId + '&name=' + editedCategoryName;
// Kiểm tra có lấy được hàng cần update không
if (rowToUpdate.length > 0) {
rowToUpdate.find('td:eq(1)').text(editedCategoryName);

$.ajax({
url: url,
type: "POST",
dataType: "json",
success: function (data) {
if (data.status === "success") {
Swal.fire({
title: "Success!",
text: "Category information updated successfully!",
icon: "success"
}).then((result) => {
if (result.isConfirmed) {
window.location.reload();
}
});
} else {
Swal.fire({
title: "Oops...",
text: "Something went wrong!",
icon: "error"
});
}
},
});
// Đóng modal
closeEditCategoryModal();
} else {
console.error("Row to update not found!");
}
}

function closeEditModal() {
// Function to close the edit modal
function closeEditCategoryModal() {
$('#editCategoryModal').modal('hide');
}

$(document).on('click', '#cancelEditBtn', function () {
closeEditModal();
});

$(document).on('click', '#saveCategoryChangesBtn', function () {
saveCategoryChanges();
// Event delegation to handle dynamically added elements
$(document).on('click', '.btn-tiny', function () {
openEditModal.call(this); // Phải gọi hàm openEditModal với this là element được click
populateEditModalFields($(this).closest('tr')[0]);
});
64 changes: 64 additions & 0 deletions src/main/webapp/assets/js/admin/order-admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var orderId;

function openEditStatusModal() {
$('#editOrderModal').modal('show');
}

// Function to populate fields in the edit modal with existing data
function populateEditModalFields(row) {
var orderStatus = row.cells[4].textContent.trim();

document.getElementById('editStatus').value = orderStatus;
}

function openEditOrderStatusModal(id) {
orderId = id;

openEditStatusModal();
}

function saveChangesEditOrder() {
let editedStatus = document.getElementById('editStatus').value;

// Get the order ID from the row or another source

const url = contextPath + '/admin/order?id=' + orderId + '&status=' + editedStatus;

$.ajax({
url: url,
type: "POST",
dataType: "json",
success: function (data) {
if (data.status === "success") {
Swal.fire({
title: "Success!",
text: "Order Status information updated successfully!",
icon: "success"
}).then((result) => {
if (result.isConfirmed) {
window.location.reload();
}
});
} else {
Swal.fire({
title: "Oops...",
text: "Something went wrong!",
icon: "error"
});
}
},
});
}


// Function to close the edit modal
function closeEditModal() {
$('#editOrderModal').modal('hide');
}

//// Event delegation to handle dynamically added elements
//$(document).on('click', '.btn-tiny', function () {
// openEditModal.call(this); // Phải gọi hàm openEditModal với this là element được click
// populateEditModalFields($(this).closest('tr')[0]);
//});

2 changes: 1 addition & 1 deletion src/main/webapp/view/admin/category-admin.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,4 @@
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/simple-datatables.min.js"></script>
<script src="<%=request.getContextPath()%>/assets/js/admin/datatables-simple-demo.js"></script>
</body>
</html>
</html>
13 changes: 7 additions & 6 deletions src/main/webapp/view/admin/dashboard-admin.jsp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="io.hardingadonis.miu.services.Singleton"%>
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down Expand Up @@ -83,8 +84,8 @@
<i class="material-icons">face</i>
</div>
<div class="content">
<div class="text">TỔNG KHÁCH HÀNG</div>
<span class="number count-to" data-from="0" data-to="10000" data-speed="1000" data-fresh-interval="20">15</span>
<div class="text">TOTAL CUSTOMERS</div>
<span class="number count-to" data-from="0" data-to="10000" data-speed="1000" data-fresh-interval="20">${Singleton.userDAO.count()}</span>
</div>
</div>
</a>
Expand All @@ -96,8 +97,8 @@
<i class="material-icons">bookmark</i>
</div>
<div class="content">
<div class="text">TỔNG SẢN PHẨM</div>
<span class="number count-to" data-from="0" data-to="1000" data-speed="1000" data-fresh-interval="20">50</span>
<div class="text">TOTAL PRODUCTS</div>
<span class="number count-to" data-from="0" data-to="1000" data-speed="1000" data-fresh-interval="20">${Singleton.productDAO.count()}</span>
</div>
</div>
</a>
Expand All @@ -109,8 +110,8 @@
<i class="material-icons">shopping_cart</i>
</div>
<div class="content">
<div class="text">TỔNG ĐƠN HÀNG</div>
<span class="number count-to" data-from="0" data-to="10000" data-speed="1000" data-fresh-interval="20">30</span>
<div class="text">TOTAL ORDERS</div>
<span class="number count-to" data-from="0" data-to="10000" data-speed="1000" data-fresh-interval="20">${Singleton.orderDAO.count()}</span>
</div>
</div>
</a>
Expand Down
24 changes: 13 additions & 11 deletions src/main/webapp/view/admin/order-admin.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,29 @@
Order Table
</div>
<div class="card-body">
<table id="datatablesSimple" class="datatable-table">
<table id="datatablesSimple">
<thead>
<tr>
<th>ID</th>
<th>User ID</th>
<th>User Full Name</th>
<th>Total Price</th>
<th>Payment</th>
<th>Status</th>
<th>Actions</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<c:forEach var="c" items="${Singleton.orderDAO.getAll()}">
<tr>
<td>${c.ID}</td>
<td>${c.userID}</td>
<td>${Singleton.userDAO.get(c.userID).getFullName()}</td>
<td>${c.totalPrice}</td>
<td>${c.payment}</td>
<td>${c.status}</td>
<td>
<a href="#" class="btn btn-info btn-tiny" title="Edit">
<button href="#" class="btn btn-info btn-tiny" title="Edit" onclick="openEditOrderStatusModal(${c.ID})">
<i class="fa fa-pencil"></i>
</a>
</button>
</td>
</tr>
</c:forEach>
Expand Down Expand Up @@ -152,18 +152,18 @@
<div class="form-group">
<label for="editStatus">Status:</label>
<select class="form-select" id="editStatus">
<option value="Processing">Processing</option>
<option value="Shipping">Shipping</option>
<option value="Done">Done</option>
<option value="Cancel">Canceled</option>
<option value="processing">PROCESSING</option>
<option value="shipping">SHIPPING</option>
<option value="done">DONE</option>
<option value="canceled">CANCELED</option>
</select>
</div>
</form>
</div>

<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclick="saveChanges()">Save changes</button>
<button type="button" class="btn btn-primary" onclick="saveChangesEditOrder()">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal" onclick="closeEditModal()">Cancel</button>
</div>
</div>
Expand All @@ -183,6 +183,8 @@
<script src="<%=request.getContextPath()%>/assets/js/admin/chart-pie-demo.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/simple-datatables.min.js"></script>
<script src="<%=request.getContextPath()%>/assets/js/admin/datatables-simple-demo.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

</body>
</html>

2 changes: 1 addition & 1 deletion src/main/webapp/view/admin/user-admin.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
<th>Email</th>
<th>Address</th>
<th>Status</th>
<th>Actions</th>
<th>Action</th>
</tr>
</thead>
<tbody>
Expand Down

0 comments on commit 7830da0

Please sign in to comment.