From bc5f6c90015fe512af7cd56efccc8d91a386304d Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 26 Sep 2022 16:09:10 +0200 Subject: Manager avatar --- pages/settings.vue | 47 +++++++++++++++++++++++++++++++++++++++++++++++ store/users.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/pages/settings.vue b/pages/settings.vue index 6535dd6..fc08199 100644 --- a/pages/settings.vue +++ b/pages/settings.vue @@ -6,6 +6,27 @@ form.mt-3(v-if="me") .overflow-hidden.shadow(class="sm:rounded-md") .space-y-6.bg-white.px-4.py-5(class="sm:p-6") + div + label.block.text-sm.font-medium.text-gray-700 Avatar + span.text-green-500.text-xs(v-if="avatarChanged") Avatar changed. Reload this page and/or clear your browser cache. + .mt-1.flex.items-center + user-avatar(:data="me") + input(type="file" hidden id="avatar" @change="editAvatar" accept="image/*") + label( + for="avatar" + class="ml-5 cursor-pointer rounded-md border border-gray-300 bg-white py-2 px-3 text-sm font-medium leading-4 text-gray-700 shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2" + ) Change + button( + type="button" + class="ml-5 cursor-pointer rounded-md border border-red-600 flex bg-red-600 py-2 px-3 text-sm font-medium leading-4 text-white shadow-sm hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2" + @click="deleteAvatar" + v-if="me.avatar" + ) + + + + | Delete + div label.block.text-sm.font-medium.text-gray-700(for="name") Name .mt-1 @@ -62,6 +83,7 @@ export default { data() { return { form: {}, + avatarChanged: false, }; }, created() { @@ -73,6 +95,31 @@ export default { }, }, methods: { + editAvatar(event) { + const file = event.target.files[0]; + this.$store + .dispatch("users/editAvatar", { id: this.me.id, file }) + .then((response) => { + if (response.status == 200) { + this.$toast.success(`Avatar has been uploaded`); + this.avatarChanged = true; + } else { + this.$toast.error(`Error uploading '${file.name}'`); + } + }); + }, + deleteAvatar() { + this.$store + .dispatch("users/deleteAvatar", this.me.id) + .then((response) => { + if (response.status == 200) { + this.$toast.success("Avatar has been removed"); + this.avatarChanged = true; + } else { + this.$toast.error("Error removing"); + } + }); + }, save(event) { const f = this.form; diff --git a/store/users.js b/store/users.js index e1f6767..cf1ca86 100644 --- a/store/users.js +++ b/store/users.js @@ -85,6 +85,60 @@ export const actions = { commit("loadingStatus", false, { root: true }); + return res; + }, + // Edit avatar + // FIX: use the right endpoint with `payload.id`, not `me` + async editAvatar({ commit, rootGetters }, payload) { + commit("loadingStatus", true, { root: true }); + let res = { status: 0, data: null }; + let api = this.$config.api; + + const body = new FormData(); + body.append("file", payload.file); + + await fetch(`${api}/v1/users/me/avatar`, { + method: "PUT", + headers: { + Authorization: `Bearer ${rootGetters["auth/accessToken"]}`, + }, + body, + }) + .then(async (response) => { + res.data = await response.json(); + res.status = response.status; + }) + .catch((e) => { + res.status = e.status; + }); + + commit("loadingStatus", false, { root: true }); + + return res; + }, + // Delete the avatar + // FIX: use the right endpoint with `id`, not `me` + async deleteAvatar({ commit, rootGetters }, id) { + commit("loadingStatus", true, { root: true }); + let res = { status: 0, data: null }; + let api = this.$config.api; + + await fetch(`${api}/v1/users/me/avatar`, { + method: "DELETE", + headers: { + Authorization: `Bearer ${rootGetters["auth/accessToken"]}`, + }, + }) + .then(async (response) => { + res.status = response.status; + if (res.status != 200) res.data = await response.json(); + }) + .catch((e) => { + res.status = e.status; + }); + + commit("loadingStatus", false, { root: true }); + return res; }, }; -- cgit v1.2.3-18-g5258