PostCard: improve code readability

This commit is contained in:
Marco Realacci 2023-01-12 01:02:12 +01:00
parent b827b06240
commit bef9d69028

View file

@ -3,8 +3,10 @@ export default {
props: ["user_id", "name", "date", "comments", "likes", "photo_id", "liked"], props: ["user_id", "name", "date", "comments", "likes", "photo_id", "liked"],
data: function () { data: function () {
return { return {
imageSrc: "", // Whether the image is loaded (disables the spinner)
imageReady: false, imageReady: false,
// Likes and comments
post_liked: this.liked, post_liked: this.liked,
post_like_cnt: this.likes, post_like_cnt: this.likes,
post_comments_cnt: this.comments, post_comments_cnt: this.comments,
@ -12,26 +14,37 @@ export default {
comments_start_idx: 0, comments_start_idx: 0,
comments_shown: false, comments_shown: false,
commentMsg: "", commentMsg: "",
// Whether the comments have ended (no more comments to load)
data_ended: false, data_ended: false,
} }
}, },
methods: { methods: {
// Visit the user's profile
visitUser() { visitUser() {
this.$router.push({ path: "/profile/" + this.user_id }); this.$router.push({ path: "/profile/" + this.user_id });
}, },
// Post a new comment
postComment() { postComment() {
this.$axios.post("/users/" + this.user_id + "/photos/" + this.photo_id + "/comments", { this.$axios.post("/users/" + this.user_id + "/photos/" + this.photo_id + "/comments", {
"comment": this.commentMsg, "comment": this.commentMsg,
"user_id": this.$currentSession(), "user_id": this.$currentSession(),
}).then(response => { }).then(response => {
this.commentMsg = ""; if (response == null) return // the interceptors returns null if something goes bad
// Reset the comment input and update the counter
this.commentMsg = "";
this.post_comments_cnt++; this.post_comments_cnt++;
// Fetch comments from the server
this.comments_data = []; this.comments_data = [];
this.comments_start_idx = 0; this.comments_start_idx = 0;
this.getComments(); this.getComments();
}) })
}, },
// Show or hide the comments section
showHideComments() { showHideComments() {
// If comments are already shown, hide them and reset the data // If comments are already shown, hide them and reset the data
if (this.comments_shown) { if (this.comments_shown) {
@ -42,28 +55,39 @@ export default {
} }
this.getComments(); this.getComments();
}, },
// Fetch comments from the server
getComments() { getComments() {
this.data_ended = false this.data_ended = false
// Get comments from the server
this.$axios.get("/users/" + this.user_id + "/photos/" + this.photo_id + this.$axios.get("/users/" + this.user_id + "/photos/" + this.photo_id +
"/comments?limit=5&start_index=" + this.comments_start_idx).then(response => { "/comments?limit=5&start_index=" + this.comments_start_idx).then(response => {
// If there are no more comments, set the flag
if (response.data.length == 0) this.data_ended = true; if (response.data.length == 0) this.data_ended = true;
// Otherwise increment the start index
else this.comments_start_idx += 5; else this.comments_start_idx += 5;
// Append the comments to the array (they will be rendered)
this.comments_data = this.comments_data.concat(response.data); this.comments_data = this.comments_data.concat(response.data);
this.comments_shown = true; this.comments_shown = true;
}) })
}, },
// Like the photo
like() { like() {
this.$axios.put("/users/" + this.user_id + "/photos/" + this.photo_id + "/likes/" + this.$currentSession()).then(response => { this.$axios.put("/users/" + this.user_id + "/photos/" + this.photo_id + "/likes/" + this.$currentSession()).then(response => {
if (response == null) return
this.post_liked = true; this.post_liked = true;
this.post_like_cnt++; this.post_like_cnt++;
}) })
}, },
// Unlike the photo
unlike() { unlike() {
this.$axios.delete("/users/" + this.user_id + "/photos/" + this.photo_id + "/likes/" + this.$currentSession()).then(response => { this.$axios.delete("/users/" + this.user_id + "/photos/" + this.photo_id + "/likes/" + this.$currentSession()).then(response => {
if (response == null) return
this.post_liked = false; this.post_liked = false;
this.post_like_cnt--; this.post_like_cnt--;
}) })
@ -71,12 +95,18 @@ export default {
}, },
created() { created() {
// Fetch the image from the server and display it
this.$axios.get("/users/" + this.user_id + "/photos/" + this.photo_id, { this.$axios.get("/users/" + this.user_id + "/photos/" + this.photo_id, {
responseType: 'arraybuffer' responseType: 'arraybuffer'
}).then(response => { }).then(response => {
// Create an image element and append it to the container
const img = document.createElement('img'); const img = document.createElement('img');
// Set image source and css class
img.src = URL.createObjectURL(new Blob([response.data])); img.src = URL.createObjectURL(new Blob([response.data]));
img.classList.add("card-img-top"); img.classList.add("card-img-top");
// Append the image to the container and disable the spinner
this.$refs.imageContainer.appendChild(img); this.$refs.imageContainer.appendChild(img);
this.imageReady = true; this.imageReady = true;
}); });