diff options
author | Santo Cariotti <santo@dcariotti.me> | 2022-09-25 16:16:31 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2022-09-25 16:16:31 +0200 |
commit | 179c470ff9c91f8206b9e0d53d5b6563656e28b1 (patch) | |
tree | 8a766eee58447ce13b5dc222460841e7e41fb459 | |
parent | 6481d712dbe58b5996a33567fe314f8a513023fa (diff) |
Edit user
-rw-r--r-- | components/VHeader.vue | 2 | ||||
-rw-r--r-- | pages/settings.vue | 90 | ||||
-rw-r--r-- | store/users.js | 26 |
3 files changed, 117 insertions, 1 deletions
diff --git a/components/VHeader.vue b/components/VHeader.vue index 0886f08..3ab2ca7 100644 --- a/components/VHeader.vue +++ b/components/VHeader.vue @@ -48,7 +48,7 @@ <path stroke-linecap="round" stroke-linejoin="round" d="M15.75 6a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0zM4.501 20.118a7.5 7.5 0 0114.998 0A17.933 17.933 0 0112 21.75c-2.676 0-5.216-.584-7.499-1.632z" /> </svg> | Your Profile - a.flex.leanding-6.block.px-4.py-2.text-sm.text-gray-700(href="#" role="menuitem" tabindex="-1") + a.flex.leanding-6.block.px-4.py-2.text-sm.text-gray-700(href="/settings" role="menuitem" tabindex="-1") <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"> <path stroke-linecap="round" stroke-linejoin="round" d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.324.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 011.37.49l1.296 2.247a1.125 1.125 0 01-.26 1.431l-1.003.827c-.293.24-.438.613-.431.992a6.759 6.759 0 010 .255c-.007.378.138.75.43.99l1.005.828c.424.35.534.954.26 1.43l-1.298 2.247a1.125 1.125 0 01-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.57 6.57 0 01-.22.128c-.331.183-.581.495-.644.869l-.213 1.28c-.09.543-.56.941-1.11.941h-2.594c-.55 0-1.02-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 01-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 01-1.369-.49l-1.297-2.247a1.125 1.125 0 01.26-1.431l1.004-.827c.292-.24.437-.613.43-.992a6.932 6.932 0 010-.255c.007-.378-.138-.75-.43-.99l-1.004-.828a1.125 1.125 0 01-.26-1.43l1.297-2.247a1.125 1.125 0 011.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.087.22-.128.332-.183.582-.495.644-.869l.214-1.281z" /> <path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" /> diff --git a/pages/settings.vue b/pages/settings.vue new file mode 100644 index 0000000..e9a02fb --- /dev/null +++ b/pages/settings.vue @@ -0,0 +1,90 @@ +<template lang="pug"> + .mx-auto.w-90p.py-6(class="sm:px-6 lg:px-8 md:max-w-7xl") + h1.text-3xl.font-bold Settings + p Change your informations + + 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(for="name") Name + .mt-1 + input#name.mt-1.block.w-full.rounded-md.border-gray-300.border-1.px-2.py-1( + name="name" + class="focus:border-green-500 focus:ring-green-500 sm:text-sm" + placeholder="John Doe" + v-model="me.name" + ) + div + label.block.text-sm.font-medium.text-gray-700(for="username") Username + .mt-1 + input#email.mt-1.block.w-full.rounded-md.border-gray-300.border-1.px-2.py-1( + name="username" + class="focus:border-green-500 focus:ring-green-500 sm:text-sm" + placeholder="john" + v-model="me.username" + ) + div + label.block.text-sm.font-medium.text-gray-700(for="email") Email + .mt-1 + input#email.mt-1.block.w-full.rounded-md.border-gray-300.border-1.px-2.py-1( + type="email" + name="email" + class="focus:border-green-500 focus:ring-green-500 sm:text-sm" + placeholder="john@example.com" + v-model="me.email" + ) + .py-3.text-right(class="sm:px-6") + button.inline-flex.justify-center.rounded-md.border.border-transparent.bg-green-600.py-2.px-4.text-sm.font-medium.text-white.shadow-sm( + type="submit" + :class="{'hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-green-500 focus:ring-offset-2': true, 'opacity-25 cursor-default': isLoading}" + :disabled="isLoading" + :readonly="isLoading" + @click="save" + ) + <svg class="animate-spin -ml-1 mr-3 h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" v-if="isLoading"> + <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle> + <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path> + </svg> + | Save + +</template> + +<script> +import { mapGetters } from "vuex"; + +export default { + name: "SettingsView", + computed: { + ...mapGetters(["isLoading"]), + ...mapGetters("auth", ["isLogged", "me"]), + }, + data() { + return { + form: {}, + }; + }, + created() { + if (!this.isLogged) window.location.href = "/signin?ref=/settings"; + }, + methods: { + save(event) { + const f = this.me; + + if (f.username && f.email) { + this.$store.dispatch("users/editUser", f).then((response) => { + if (response.status == 200) { + this.$toast.success("User has been saved"); + setTimeout(() => { + window.location.href = "/settings"; + }, 1000); + } else { + this.$toast.error(response.data.error); + } + }); + } + event.preventDefault(); + }, + }, +}; +</script> diff --git a/store/users.js b/store/users.js index cb9b4b4..e1f6767 100644 --- a/store/users.js +++ b/store/users.js @@ -61,4 +61,30 @@ export const actions = { return res; }, + // Edit an user + async editUser({ commit, rootGetters }, payload) { + commit("loadingStatus", true, { root: true }); + let res = { status: 0, data: null }; + let api = this.$config.api; + + await fetch(`${api}/v1/users/${payload.id}`, { + method: "PUT", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${rootGetters["auth/accessToken"]}`, + }, + body: JSON.stringify(payload), + }) + .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; + }, }; |