UserCard: improve general code quality

This commit is contained in:
Marco Realacci 2023-01-12 00:52:23 +01:00
parent 20dadfe6b6
commit b827b06240

View file

@ -3,78 +3,109 @@ export default {
props: ["user_id", "name", "followed", "banned", "show_new_post"], props: ["user_id", "name", "followed", "banned", "show_new_post"],
watch: { watch: {
name: function (new_val, old_val) { name: function (new_val, old_val) {
this.username = new_val; this.username = new_val
}, },
banned: function (new_val, old_val) { banned: function (new_val, old_val) {
this.user_banned = new_val; this.user_banned = new_val
}, },
followed: function (new_val, old_val) { followed: function (new_val, old_val) {
this.user_followed = new_val; this.user_followed = new_val
}, },
}, },
data: function () { data: function () {
return { return {
// User data
username: this.name, username: this.name,
user_followed: this.followed, user_followed: this.followed,
user_banned: this.banned, user_banned: this.banned,
// Whether the user is the currently logged in user
myself: this.$currentSession() == this.user_id, myself: this.$currentSession() == this.user_id,
// Whether to show the buttons to post a new photo and update the username
show_post_form: false, show_post_form: false,
show_username_form: false, show_username_form: false,
// The new username
newUsername: "", newUsername: "",
// The file to upload
upload_file: null, upload_file: null,
} }
}, },
methods: { methods: {
// Logout the user
logout() { logout() {
this.$root.logout() this.$root.logout()
}, },
// Visit the user's profile
visit() { visit() {
this.$router.push({ path: "/profile/" + this.user_id }); this.$router.push({ path: "/profile/" + this.user_id });
}, },
// Follow the user
follow() { follow() {
this.$axios.put("/users/" + this.user_id + "/followers/" + this.$currentSession()) this.$axios.put("/users/" + this.user_id + "/followers/" + this.$currentSession())
.then(response => { .then(response => {
if (response == null) return // the interceptors returns null if something goes bad
this.user_followed = true this.user_followed = true
this.$emit('updateInfo') this.$emit('updateInfo')
}) })
}, },
// Unfollow the user
unfollow() { unfollow() {
this.$axios.delete("/users/" + this.user_id + "/followers/" + this.$currentSession()) this.$axios.delete("/users/" + this.user_id + "/followers/" + this.$currentSession())
.then(response => { .then(response => {
if (response == null) return
this.user_followed = false this.user_followed = false
this.$emit('updateInfo') this.$emit('updateInfo')
}) })
}, },
// Ban the user
ban() { ban() {
this.$axios.put("/users/" + this.$currentSession() + "/bans/" + this.user_id) this.$axios.put("/users/" + this.$currentSession() + "/bans/" + this.user_id)
.then(response => { .then(response => {
if (response == null) return
this.user_banned = true this.user_banned = true
this.$emit('updateInfo') this.$emit('updateInfo')
}) })
}, },
// Unban the user
unban() { unban() {
this.$axios.delete("/users/" + this.$currentSession() + "/bans/" + this.user_id) this.$axios.delete("/users/" + this.$currentSession() + "/bans/" + this.user_id)
.then(response => { .then(response => {
if (response == null) return
this.user_banned = false this.user_banned = false
this.$emit('updateInfo') this.$emit('updateInfo')
}) })
}, },
// Prepare the file to upload
load_file(e) { load_file(e) {
let files = e.target.files || e.dataTransfer.files; let files = e.target.files || e.dataTransfer.files;
if (!files.length) return; if (!files.length) return
this.upload_file = files[0]; this.upload_file = files[0]
}, },
// Upload the file
submit_file() { submit_file() {
this.$axios.post("/users/" + this.$currentSession() + "/photos", this.upload_file) this.$axios.post("/users/" + this.$currentSession() + "/photos", this.upload_file)
.then(response => { .then(response => {
if (response == null) return
this.show_post_form = false this.show_post_form = false
this.$emit('updatePosts') this.$emit('updatePosts')
}) })
}, },
// Update the username
updateUsername() { updateUsername() {
this.$axios.put("/users/" + this.$currentSession() + "/username", { name: this.newUsername }) this.$axios.put("/users/" + this.$currentSession() + "/username", { name: this.newUsername })
.then(response => { .then(response => {
if (response == null) return; // the interceptors returns null if something goes bad if (response == null) return
this.show_username_form = false this.show_username_form = false
this.$emit('updateInfo') this.$emit('updateInfo')
this.username = this.newUsername this.username = this.newUsername