Skip to content

Commit

Permalink
Allow users to delete their profile image
Browse files Browse the repository at this point in the history
  • Loading branch information
Nonononoki committed Aug 30, 2020
1 parent d61be65 commit e1616fc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/nonononoki/alovoa/rest/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public void deleteAccount(@RequestBody UserDeleteAccountDto dto) throws Exceptio
public void getUserdata(@RequestBody (required=false) String password) throws Exception {
userService.getUserdata(password);
}

@PostMapping(value = "/delete/profile-picture")
public void deleteProfilePicture() throws Exception {
userService.deleteProfilePicture();
}

@PostMapping(value = "/update/profile-picture", consumes = "text/plain")
public void updateProfilePicture(@RequestBody String imageB64) throws Exception {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/nonononoki/alovoa/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,11 @@ public void getUserdata(String password) throws Exception {
}
mailService.sendUserDataMail(user);
}

public void deleteProfilePicture() throws Exception {
User user = authService.getCurrentUser();
user.setProfilePicture(null);
userRepo.saveAndFlush(user);

}
}
23 changes: 22 additions & 1 deletion src/main/resources/static/js/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,28 @@ $(function() {
updateProfileWarning();

$("#profilePicture").click(function(e) {
$("#profilePictureUpload").click();
let val = $("#profilePicture").attr("value");
console.log(val)
if(val == 0) {
$("#profilePictureUpload").click();
} else {
if (confirm(getText("profile.js.delete-image.confirm"))) {
$.ajax({
type : "POST",
url : "/user/delete/profile-picture",
headers : {
"X-CSRF-TOKEN" : $("input[name='_csrf']").val()
},
success : function(e) {
location.reload(true);
},
error : function(e) {
console.log(e);
alert(getGenericErrorText());
}
});
}
}
});

$("#addImageDiv").click(function(e) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
<div class="center-parent">
<img id="profilePicture" class="profile-pic"
src="/img/profile.jpg" style="cursor: pointer;" height=400
width="400" th:unless="${user.profilePicture}" /> <img
width="400" th:unless="${user.profilePicture}" th:value=0 /> <img
id="profilePicture" style="cursor: pointer;" height=400
width="400" th:if="${user.profilePicture}"
class="profile-pic " th:src="${user.profilePicture}" /> <input
class="profile-pic " th:src="${user.profilePicture}" th:value=1 /> <input
id="profilePictureUpload" type="file" accept="image/*"
style="display: none">
</div>
Expand Down

0 comments on commit e1616fc

Please sign in to comment.