refactor: replace inline banner with dynamic component grid and update CSS layouts

This commit is contained in:
2026-07-17 15:30:44 +07:00
parent fd4ad87a32
commit 49b8d2b2ea
9 changed files with 668 additions and 185 deletions
File diff suppressed because one or more lines are too long
@@ -0,0 +1,37 @@
<div class="cc--component-container cc--news-events">
<div class="c--component c--news-events">
<!-- Phần 2 cột: Tin mới (Trái) và Sự kiện (Phải) -->
<div class="news-events-row">
<div class="news-column">
<h3 class="section-title--news">
TIN TỨC MỚI
</h3>
<div class="news-list">
<!-- Shortcode Tin Tức hiển thị dạng list (tuỳ chỉnh lại limit cho phù hợp) -->
[component:news-grid data-source="posts:tag=news,limit=2,collection=news_posts"]
</div>
</div>
<div class="events-column">
<h3 class="events-title">
SỰ KIỆN
</h3>
<div class="events-widget">
<!-- Shortcode Sự kiện -->
[component:events-list data-source="posts:tag=event,limit=4,collection=event_posts"]
</div>
</div>
</div>
<!-- Header chung (Mobile) -->
<div class="news-events-section__footer">
<a href="/news/news-events" class="news-events-section__cta news-events-section__cta--mobile">
Xem Thêm
</a>
</div>
</div>
</div>
File diff suppressed because one or more lines are too long
@@ -53,13 +53,16 @@ public class ManagePostController {
}
/**
* GET /manage/posts — List all posts with optional filters.
* GET /manage/posts — List all posts with optional filters, search, and pagination.
*/
@GetMapping
public String listPosts(@RequestParam(value = "categoryId", required = false) Long categoryId,
@RequestParam(value = "status", required = false) String statusStr,
@RequestParam(value = "search", required = false) String search,
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "10") int size,
Model model) {
LOG.debug("Request to list all posts (categoryId={}, status={})", categoryId, statusStr);
LOG.debug("Request to list all posts (categoryId={}, status={}, search={}, page={}, size={})", categoryId, statusStr, search, page, size);
PageStatus status = null;
if (statusStr != null && !statusStr.isBlank()) {
@@ -70,11 +73,17 @@ public class ManagePostController {
}
}
model.addAttribute("posts", postService.findFiltered(categoryId, status));
org.springframework.data.domain.Pageable pageable = org.springframework.data.domain.PageRequest.of(page, size, org.springframework.data.domain.Sort.by("createdDate").descending());
org.springframework.data.domain.Page<com.sisvietnamvn.web.domain.Post> postPage = postService.findFilteredAndSearch(categoryId, status, search, pageable);
model.addAttribute("postPage", postPage);
model.addAttribute("posts", postPage.getContent());
model.addAttribute("categories", categoryService.findAll());
model.addAttribute("statuses", PageStatus.values());
model.addAttribute("selectedCategoryId", categoryId);
model.addAttribute("selectedStatus", statusStr);
model.addAttribute("selectedSearch", search);
model.addAttribute("pageSize", size);
return "manage/posts/list";
}
@@ -82,4 +82,16 @@ public interface PostRepository extends JpaRepository<Post, Long> {
*/
@Query("SELECT p FROM Post p JOIN p.tags t WHERE t.id = :tagId ORDER BY p.createdDate DESC")
List<Post> findByTagId(@Param("tagId") Long tagId, org.springframework.data.domain.Pageable pageable);
/**
* Find posts with filters, search text, and pagination.
*/
@org.springframework.data.jpa.repository.EntityGraph(attributePaths = {"category"})
@Query("SELECT p FROM Post p WHERE (:categoryId IS NULL OR p.category.id = :categoryId) " +
"AND (:status IS NULL OR p.status = :status) " +
"AND (:search IS NULL OR LOWER(p.title) LIKE LOWER(:search) OR LOWER(p.excerpt) LIKE LOWER(:search))")
org.springframework.data.domain.Page<Post> findFilteredAndSearch(@Param("categoryId") Long categoryId,
@Param("status") PageStatus status,
@Param("search") String search,
org.springframework.data.domain.Pageable pageable);
}
@@ -217,7 +217,18 @@ public class ComponentTemplateService {
for (com.sisvietnamvn.web.domain.Post post : posts) {
String itemHtml = loopBody;
String safeTitle = post.getTitle() != null ? org.springframework.web.util.HtmlUtils.htmlEscape(post.getTitle()) : "";
String safeExcerpt = post.getExcerpt() != null ? org.springframework.web.util.HtmlUtils.htmlEscape(post.getExcerpt()) : "";
String rawExcerpt = post.getExcerpt();
if (rawExcerpt == null || rawExcerpt.trim().isEmpty()) {
rawExcerpt = post.getMetaDescription();
}
if (rawExcerpt == null || rawExcerpt.trim().isEmpty()) {
String rawContent = post.getContent();
if (rawContent != null) {
rawContent = rawContent.replaceAll("<[^>]*>", " ").replaceAll("\\s+", " ").trim();
rawExcerpt = rawContent.length() > 150 ? rawContent.substring(0, 150) + "..." : rawContent;
}
}
String safeExcerpt = rawExcerpt != null ? org.springframework.web.util.HtmlUtils.htmlEscape(rawExcerpt) : "";
itemHtml = itemHtml.replace("{{title}}", safeTitle);
itemHtml = itemHtml.replace("{{slug}}", post.getSlug() != null ? post.getSlug() : "");
@@ -275,13 +286,16 @@ public class ComponentTemplateService {
Optional<ComponentTemplate> existing = templateRepository.findBySlug("news-grid");
if (existing.isPresent()) {
ComponentTemplate t = existing.get();
if (t.getHtmlTemplate() == null || !t.getHtmlTemplate().contains("<style>")) {
return; // Already clean
}
// Update the existing one
if (t.getHtmlTemplate() == null || (!t.getHtmlTemplate().contains("<style>") && !t.getHtmlTemplate().contains("{{excerpt}}"))) {
// If it doesn't have the excerpt yet, update it.
t.setHtmlTemplate(getNewsGridHtml());
templateRepository.save(t);
LOG.info("Updated news-grid component template to include excerpt");
} else if (t.getHtmlTemplate().contains("<style>")) {
t.setHtmlTemplate(getNewsGridHtml());
templateRepository.save(t);
LOG.info("Cleaned up hardcoded CSS in news-grid component template");
}
return;
}
@@ -313,7 +327,10 @@ public class ComponentTemplateService {
" <span>{{title}}</span>\n" +
" </a>\n" +
" </h3>\n" +
" <div class=\"news-card__date\">{{date}}</div>\n" +
" <div class=\"news-card__summary\" style=\"margin-top: 10px; font-size: 0.9em; color: #555; display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden;\">\n" +
" {{excerpt}}\n" +
" </div>\n" +
" <div class=\"news-card__date\" style=\"margin-top: auto; padding-top: 10px;\">{{date}}</div>\n" +
" </div>\n" +
" </article>\n" +
"</div>\n" +
@@ -94,6 +94,19 @@ public class PostService {
return postRepository.findAllByOrderByCreatedDateDesc();
}
/**
* Find all posts filtered by category, status, and search string, with pagination.
*/
@Transactional(readOnly = true)
public org.springframework.data.domain.Page<Post> findFilteredAndSearch(Long categoryId, PageStatus status, String search, org.springframework.data.domain.Pageable pageable) {
LOG.debug("Request to get Posts filtered by categoryId={}, status={}, search={}", categoryId, status, search);
String searchParam = (search != null && !search.trim().isEmpty()) ? "%" + search.trim() + "%" : null;
org.springframework.data.domain.Page<Post> page = postRepository.findFilteredAndSearch(categoryId, status, searchParam, pageable);
// Eagerly initialize tags within the transaction to avoid LazyInitializationException in Thymeleaf
page.getContent().forEach(post -> org.hibernate.Hibernate.initialize(post.getTags()));
return page;
}
/**
* Save a post (create or update).
* Auto-generates a URL slug from the title if the slug is empty.
@@ -304,7 +304,7 @@
height: 100%;
display: flex;
}
.featured-grid__item .news-card--featured {
/* .featured-grid__item .news-card--featured {
display: flex;
flex-direction: column;
align-items: flex-start;
@@ -317,7 +317,7 @@
gap: 16px;
background: #fff;
margin-bottom: 0 !important;
}
} */
.featured-grid__item .news-card__image {
flex: none;
width: 100%;
@@ -398,7 +398,7 @@
height: 100%;
display: flex;
}
.featured-grid__item .news-card--featured {
/* .featured-grid__item .news-card--featured {
display: flex;
flex-direction: row;
align-items: flex-start;
@@ -411,11 +411,11 @@
gap: 16px;
background: #fff;
margin-bottom: 0 !important;
}
} */
.featured-grid__item .news-card__image {
flex: 0 0 170px;
width: 100%;
height: 170px;
height: auto;
margin-bottom: 0 !important;
}
.featured-grid__item .news-card__image > div {
@@ -41,12 +41,16 @@
</div>
<div class="card-body">
<form th:action="@{/manage/posts}" method="get" class="form-inline">
<div class="form-group mr-3 mb-2">
<label for="filterSearch" class="mr-2 font-weight-bold">Search:</label>
<input type="text" class="form-control form-control-sm" id="filterSearch" name="search" th:value="${selectedSearch}" placeholder="Title or content...">
</div>
<div class="form-group mr-3 mb-2">
<label for="filterCategory" class="mr-2 font-weight-bold">Category:</label>
<select class="form-control form-control-sm" id="filterCategory" name="categoryId">
<option value="">All Categories</option>
<option th:each="cat : ${categories}" th:value="${cat.id}" th:text="${cat.name}"
th:selected="${selectedCategoryId != null && selectedCategoryId == cat.id}"></option>
th:selected="${selectedCategoryId != null and selectedCategoryId == cat.id}"></option>
</select>
</div>
<div class="form-group mr-3 mb-2">
@@ -54,7 +58,16 @@
<select class="form-control form-control-sm" id="filterStatus" name="status">
<option value="">All Statuses</option>
<option th:each="s : ${statuses}" th:value="${s}" th:text="${s}"
th:selected="${selectedStatus != null && selectedStatus == s.name()}"></option>
th:selected="${selectedStatus != null and selectedStatus == s.name()}"></option>
</select>
</div>
<div class="form-group mr-3 mb-2">
<label for="filterSize" class="mr-2 font-weight-bold">Show:</label>
<select class="form-control form-control-sm" id="filterSize" name="size">
<option value="10" th:selected="${pageSize == 10}">10</option>
<option value="20" th:selected="${pageSize == 20}">20</option>
<option value="50" th:selected="${pageSize == 50}">50</option>
<option value="100" th:selected="${pageSize == 100}">100</option>
</select>
</div>
<button type="submit" class="btn btn-sm btn-primary mb-2 mr-2">
@@ -89,7 +102,7 @@
</thead>
<tbody>
<tr th:each="post, iterStat : ${posts}">
<td th:text="${iterStat.count}"></td>
<td th:text="${(postPage.number * postPage.size) + iterStat.count}"></td>
<td>
<a th:href="@{/manage/posts/{id}/edit(id=${post.id})}" class="font-weight-bold text-primary"
th:text="${post.title}"></a>
@@ -146,12 +159,38 @@
<tr th:if="${#lists.isEmpty(posts)}">
<td colspan="8" class="text-center text-muted py-4">
<i class="fas fa-newspaper fa-2x mb-2 d-block"></i>
No posts found. Click "Add New Post" to create one.
No posts found.
</td>
</tr>
</tbody>
</table>
</div>
<!-- Pagination -->
<div th:if="${postPage.totalPages gt 1}" class="d-flex justify-content-between align-items-center mt-3">
<div>
Showing <span th:text="${(postPage.number * postPage.size) + 1}"></span> to
<span th:text="${(postPage.number * postPage.size) + postPage.numberOfElements}"></span> of
<span th:text="${postPage.totalElements}"></span> entries
</div>
<nav>
<ul class="pagination mb-0">
<li class="page-item" th:classappend="${postPage.first ? 'disabled' : ''}">
<a class="page-link" th:href="@{/manage/posts(page=${postPage.number - 1}, size=${pageSize}, categoryId=${selectedCategoryId}, status=${selectedStatus}, search=${selectedSearch})}">Previous</a>
</li>
<li class="page-item" th:each="i : ${#numbers.sequence(0, postPage.totalPages - 1)}"
th:classappend="${postPage.number == i ? 'active' : ''}">
<a class="page-link" th:href="@{/manage/posts(page=${i}, size=${pageSize}, categoryId=${selectedCategoryId}, status=${selectedStatus}, search=${selectedSearch})}" th:text="${i + 1}"></a>
</li>
<li class="page-item" th:classappend="${postPage.last ? 'disabled' : ''}">
<a class="page-link" th:href="@{/manage/posts(page=${postPage.number + 1}, size=${pageSize}, categoryId=${selectedCategoryId}, status=${selectedStatus}, search=${selectedSearch})}">Next</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
</div>