Skip to content

Commit 6315235

Browse files
committed
task: rewrite AddCommentForm to HTMX
Closes #1489 Closes #1500 Closes #1544 Part of #1671
1 parent be34c9c commit 6315235

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+289
-282
lines changed

.github/dependabot.yml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ updates:
1717
- dependency-name: "org.liquibase:liquibase-core"
1818
- dependency-name: "org.projectlombok:lombok"
1919
- dependency-name: "org.hibernate.validator:hibernate-validator"
20+
- dependency-name: "org.webjars.npm:htmx.org"
2021
# https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#ignore
2122
ignore:
2223
- dependency-name: "org.hibernate.validator:hibernate-validator"

pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@
299299
</exclusions>
300300
</dependency>
301301

302+
<dependency>
303+
<groupId>org.webjars.npm</groupId>
304+
<artifactId>htmx.org</artifactId>
305+
<version>${htmx.version}</version>
306+
<scope>runtime</scope>
307+
</dependency>
308+
302309
<dependency>
303310
<groupId>org.webjars.npm</groupId>
304311
<artifactId>react</artifactId>
@@ -575,6 +582,9 @@
575582
<!-- Redefine default value from spring-boot-dependencies -->
576583
<htmlunit.version>2.70.0</htmlunit.version>
577584

585+
<!-- Don't forget to update version in the ResourceUrl class and in the src/main/webapp/WEB-INF/views/series/info.html -->
586+
<htmx.version>2.0.4</htmx.version>
587+
578588
<!-- Redefine default value from spring-boot-dependencies -->
579589
<jakarta-mail.version>1.6.7</jakarta-mail.version>
580590

@@ -987,6 +997,10 @@
987997
<nonCriticalTags>
988998
<!-- Allow to tests with this tag to fail without changing final status -->
989999
<nonCriticalTag>unstable</nonCriticalTag>
1000+
<!--
1001+
@todo #1671 Fix integration tests because of unsupported HTMX in htmlunit
1002+
-->
1003+
<nonCriticalTag>htmx</nonCriticalTag>
9901004
</nonCriticalTags>
9911005
</configuration>
9921006
<dependencies>

src/main/frontend/src/components/AddCommentForm.js

-143
This file was deleted.

src/main/frontend/src/components/AddCommentForm.test.js

-60
This file was deleted.

src/main/frontend/webpack.config.js

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module.exports = {
1010

1111
'components/AddCatalogNumbersForm': './src/components/AddCatalogNumbersForm.js',
1212
'components/AddCatalogPriceForm': './src/components/AddCatalogPriceForm.js',
13-
'components/AddCommentForm': './src/components/AddCommentForm.js',
1413
'components/AddReleaseYearForm': './src/components/AddReleaseYearForm.js',
1514
'components/HideImageForm': './src/components/HideImageForm.js',
1615
'components/SeriesSaleImportForm': './src/components/SeriesSaleImportForm.js',

src/main/java/ru/mystamps/web/config/MvcConfig.java

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
130130
.addResourceLocations("classpath:/META-INF/resources/webjars/axios/");
131131
registry.addResourceHandler("/public/bootstrap/**")
132132
.addResourceLocations("classpath:/META-INF/resources/webjars/bootstrap/");
133+
registry.addResourceHandler("/public/htmx/**")
134+
.addResourceLocations("classpath:/META-INF/resources/webjars/htmx.org/");
133135
registry.addResourceHandler("/public/jquery/**")
134136
.addResourceLocations("classpath:/META-INF/resources/webjars/jquery/");
135137
registry.addResourceHandler("/public/react/**")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) 2009-2025 Slava Semushin <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
19+
package ru.mystamps.web.feature.series;
20+
21+
import lombok.Getter;
22+
import lombok.Setter;
23+
24+
import javax.validation.constraints.NotBlank;
25+
import javax.validation.constraints.Size;
26+
27+
import static ru.mystamps.web.feature.series.SeriesValidation.MAX_SERIES_COMMENT_LENGTH;
28+
29+
@Getter
30+
@Setter
31+
public class AddCommentForm {
32+
@NotBlank
33+
@Size(max = MAX_SERIES_COMMENT_LENGTH, message = "{value.too-long}")
34+
private String comment;
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (C) 2009-2025 Slava Semushin <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
19+
package ru.mystamps.web.feature.series;
20+
21+
import lombok.RequiredArgsConstructor;
22+
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
23+
import org.springframework.http.HttpStatus;
24+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
25+
import org.springframework.stereotype.Controller;
26+
import org.springframework.ui.Model;
27+
import org.springframework.validation.BindingResult;
28+
import org.springframework.web.bind.WebDataBinder;
29+
import org.springframework.web.bind.annotation.InitBinder;
30+
import org.springframework.web.bind.annotation.PatchMapping;
31+
import org.springframework.web.bind.annotation.PathVariable;
32+
import ru.mystamps.web.support.spring.security.CustomUserDetails;
33+
34+
import javax.servlet.http.HttpServletResponse;
35+
import javax.validation.Valid;
36+
import java.io.IOException;
37+
38+
@Controller
39+
@RequiredArgsConstructor
40+
public class HtmxSeriesController {
41+
42+
private final SeriesService seriesService;
43+
44+
@InitBinder("addCommentForm")
45+
protected void initBinder(WebDataBinder binder) {
46+
binder.registerCustomEditor(String.class, "comment", new StringTrimmerEditor(true));
47+
}
48+
49+
@PatchMapping(
50+
path = SeriesUrl.INFO_SERIES_PAGE,
51+
headers = "HX-Trigger=add-comment-form"
52+
)
53+
public String updateSeriesComment(
54+
@PathVariable("id") Integer seriesId,
55+
@Valid AddCommentForm form,
56+
BindingResult result,
57+
@AuthenticationPrincipal CustomUserDetails currentUser,
58+
Model model,
59+
HttpServletResponse response
60+
) throws IOException {
61+
62+
if (seriesId == null) {
63+
response.sendError(HttpServletResponse.SC_NOT_FOUND);
64+
return null;
65+
}
66+
67+
if (!seriesService.isSeriesExist(seriesId)) {
68+
response.sendError(HttpServletResponse.SC_NOT_FOUND);
69+
return null;
70+
}
71+
72+
if (result.hasErrors()) {
73+
model.addAttribute("isHtmx", true);
74+
model.addAttribute("seriesId", seriesId);
75+
response.sendError(HttpStatus.UNPROCESSABLE_ENTITY.value());
76+
return "series/info :: AddCommentForm";
77+
}
78+
79+
String comment = form.getComment();
80+
Integer currentUserId = currentUser.getUserId();
81+
seriesService.addComment(seriesId, currentUserId, comment);
82+
83+
model.addAttribute("comment", comment);
84+
return "series/partial/comment";
85+
}
86+
87+
}

src/main/java/ru/mystamps/web/feature/series/RestSeriesController.java

-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ public ResponseEntity<Void> updateSeries(
7575
Integer currentUserId = currentUser.getUserId();
7676
String path = patch.getPath();
7777
switch (path) {
78-
case "/comment":
79-
seriesService.addComment(seriesId, currentUserId, patch.getValue());
80-
break;
8178
case "/release_year":
8279
seriesService.addReleaseYear(seriesId, patch.integerValue(), currentUserId);
8380
break;

0 commit comments

Comments
 (0)