Skip to content

Commit 41d9d4d

Browse files
committed
task: implement a basic controller and client-server communication
Part of #1383
1 parent f27faf0 commit 41d9d4d

File tree

6 files changed

+114
-35
lines changed

6 files changed

+114
-35
lines changed

src/main/frontend/src/components/HideImageForm.js

+36-31
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ class HideImageForm extends React.PureComponent {
2020

2121
handleChange(event) {
2222
event.preventDefault();
23+
const id = parseInt(event.target.value, 10);
24+
if (isNaN(id)) {
25+
return;
26+
}
2327
this.setState({
24-
comment: event.target.value
28+
imageId: id
2529
});
2630
}
2731

@@ -34,45 +38,46 @@ class HideImageForm extends React.PureComponent {
3438
validationErrors: []
3539
});
3640

37-
axios.patch(
38-
this.props.url, [
39-
{
40-
op: 'add',
41-
path: '/comment',
42-
value: this.state.imageId
43-
}
44-
],
41+
axios.post(
42+
this.props.url,
43+
{
44+
'action': 'HIDE',
45+
'imageId': this.state.imageId
46+
},
4547
{
4648
headers: {
4749
[this.props.csrfHeaderName]: this.props.csrfTokenValue,
4850
'Cache-Control': 'no-store'
4951
},
50-
validateStatus: status => {
52+
validateStatus: (status) => {
5153
return status == 204 || status == 400;
5254
}
53-
})
54-
.then(response => {
55-
const data = response.data;
55+
}
56+
).then(response => {
57+
const data = response.data;
5658

57-
if (data.hasOwnProperty('fieldErrors')) {
58-
const fieldErrors = [];
59-
if (data.fieldErrors.value) {
60-
fieldErrors.push(...data.fieldErrors.value);
61-
}
62-
this.setState({
63-
isDisabled: false,
64-
validationErrors: fieldErrors
65-
});
66-
return;
59+
if (data.hasOwnProperty('fieldErrors')) {
60+
const fieldErrors = [];
61+
if (data.fieldErrors.action) {
62+
fieldErrors.push(...data.fieldErrors.action);
6763
}
64+
if (data.fieldErrors.imageId) {
65+
fieldErrors.push(...data.fieldErrors.imageId);
66+
}
67+
this.setState({
68+
isDisabled: false,
69+
validationErrors: fieldErrors
70+
});
71+
return;
72+
}
6873

69-
// no need to reset the state as page will be reloaded
70-
window.location.reload();
71-
})
72-
.catch(error => {
73-
console.error(error);
74-
this.setState({ isDisabled: false, hasServerError: true });
75-
});
74+
// no need to reset the state as page will be reloaded
75+
window.location.reload();
76+
})
77+
.catch(error => {
78+
console.error(error);
79+
this.setState({ isDisabled: false, hasServerError: true });
80+
});
7681
}
7782
render() {
7883
return (
@@ -118,7 +123,7 @@ class HideImageFormView extends React.PureComponent {
118123
<option value="">{ l10n['t_not_chosen'] || 'Not chosen' }</option>
119124
{
120125
this.props.imageIds.map(imageId =>
121-
<option key={ imageId } value="{ imageId }">{ imageId }</option>
126+
<option key={ imageId } value={ imageId }>{ imageId }</option>
122127
)
123128
}
124129
</select>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) 2009-2021 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+
package ru.mystamps.web.feature.series;
19+
20+
import lombok.Getter;
21+
import lombok.Setter;
22+
23+
import javax.validation.constraints.NotNull;
24+
25+
@Getter
26+
@Setter
27+
public class ModifySeriesImageForm {
28+
29+
@NotNull
30+
private ModifySeriesImageAction action;
31+
32+
@NotNull
33+
private Integer imageId;
34+
35+
public enum ModifySeriesImageAction {
36+
HIDE
37+
}
38+
39+
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.validation.annotation.Validated;
2424
import org.springframework.web.bind.annotation.PatchMapping;
2525
import org.springframework.web.bind.annotation.PathVariable;
26+
import org.springframework.web.bind.annotation.PostMapping;
2627
import org.springframework.web.bind.annotation.RequestBody;
2728
import org.springframework.web.bind.annotation.RestController;
2829
import ru.mystamps.web.support.spring.mvc.PatchRequest;
@@ -112,6 +113,21 @@ public ResponseEntity<Void> updateSeries(
112113
return ResponseEntity.noContent().build();
113114
}
114115

116+
@PostMapping(path = SeriesUrl.ADD_IMAGE_SERIES_PAGE)
117+
public ResponseEntity<Void> modifySeriesImage(
118+
@PathVariable("id") Integer seriesId,
119+
@RequestBody @Valid ModifySeriesImageForm form,
120+
HttpServletResponse response
121+
) throws IOException {
122+
123+
if (seriesId == null) {
124+
response.sendError(HttpServletResponse.SC_NOT_FOUND);
125+
return null;
126+
}
127+
128+
return ResponseEntity.noContent().build();
129+
}
130+
115131
private static StampsCatalog extractCatalog(String path) {
116132
// "/catalog_something" => "catalog" => "CATALOG"
117133
String catalogName = StringUtils.substringBetween(path, "/", "_")

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.slf4j.LoggerFactory;
2424
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
2525
import org.springframework.http.HttpStatus;
26+
import org.springframework.http.MediaType;
2627
import org.springframework.http.ResponseEntity;
2728
import org.springframework.security.core.annotation.AuthenticationPrincipal;
2829
import org.springframework.stereotype.Controller;
@@ -349,7 +350,11 @@ public String processImageWithImageUrl(
349350
}
350351

351352
@SuppressWarnings("checkstyle:parameternumber")
352-
@PostMapping(path = SeriesUrl.ADD_IMAGE_SERIES_PAGE, params = "!imageUrl")
353+
@PostMapping(
354+
path = SeriesUrl.ADD_IMAGE_SERIES_PAGE,
355+
params = "!imageUrl",
356+
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE
357+
)
353358
public String processImage(
354359
@Validated({
355360
AddImageForm.RequireImageCheck.class,

src/main/java/ru/mystamps/web/feature/site/ResourceUrl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public final class ResourceUrl {
3232
public static final String STATIC_RESOURCES_URL = "https://stamps.filezz.ru";
3333

3434
// MUST be updated when any of our resources were modified
35-
public static final String RESOURCES_VERSION = "v0.4.5.4";
35+
public static final String RESOURCES_VERSION = "v0.4.5.5";
3636

3737
// CheckStyle: ignore LineLength for next 17 lines
3838
private static final String CATALOG_UTILS_JS = "/public/js/" + RESOURCES_VERSION + "/CatalogUtils.min.js";

src/main/webapp/WEB-INF/views/series/info.html

+16-2
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,20 @@ <h5 th:text="#{t_add_info_who_selling_series}">Add info about selling/buying thi
917917
data: {}
918918
}
919919
},
920+
'/series/100/image': {
921+
'failOnField': {
922+
status: 400,
923+
data: {
924+
'fieldErrors': {
925+
'imageId': [ 'Invalid image id' ]
926+
}
927+
}
928+
},
929+
'success': {
930+
status: 204,
931+
data: {}
932+
}
933+
},
920934
'/series/sales/import': {
921935
'failOnField': {
922936
status: 400,
@@ -1344,7 +1358,7 @@ <h5 th:text="#{t_add_info_who_selling_series}">Add info about selling/buying thi
13441358
<script th:inline="javascript">
13451359
/*[+
13461360
var hideImageProps = {
1347-
'url': [[ '__@{${INFO_SERIES_PAGE}(id=${series.id})}__' ]],
1361+
'url': [[ '__@{${ADD_IMAGE_SERIES_PAGE}(id=${series.id})}__' ]],
13481362
'imageIds': [[ ${series.imageIds} ]],
13491363
'csrfHeaderName': [[ ${_csrf.headerName} ]],
13501364
'csrfTokenValue': [[ ${_csrf.token} ]],
@@ -1359,7 +1373,7 @@ <h5 th:text="#{t_add_info_who_selling_series}">Add info about selling/buying thi
13591373

13601374
/*[- */
13611375
var hideImageProps = {
1362-
'url': '/series/100',
1376+
'url': '/series/100/image',
13631377
'l10n': {},
13641378
'imageIds': [ 1 ]
13651379
};

0 commit comments

Comments
 (0)