change profile name

This commit is contained in:
artem 2024-05-04 08:16:10 +03:00
parent 6678c6d191
commit 787bd70499
3 changed files with 41 additions and 19 deletions

View File

@ -8,9 +8,10 @@
close-button close-button
@update:modelValue="emits('cancel')" @update:modelValue="emits('cancel')"
> >
<h1 class="va-h5 mb-4">Reset password</h1> <h1 class="va-h5 mb-4">Изменить имя</h1>
<VaForm ref="form" @submit.prevent="submit"> <VaForm ref="form" @submit.prevent="submit">
<VaInput v-model="Name" class="mb-4" label="Name" placeholder="Name" /> <VaInput v-model="Name" class="mb-4" label="Имя" placeholder="Имя" />
<VaInput v-model="Surname" class="mb-4" label="Фамилия" placeholder="Фамилия" />
<div <div
class="flex flex-col-reverse md:flex-row md:items-center md:justify-end md:space-x-4" class="flex flex-col-reverse md:flex-row md:items-center md:justify-end md:space-x-4"
> >
@ -35,7 +36,10 @@
</VaModal> </VaModal>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { inject } from 'vue'
import { ref } from "vue"; import { ref } from "vue";
import axios from "axios";
import { useUserStore } from "../../../stores/user-store"; import { useUserStore } from "../../../stores/user-store";
import { buttonStyles } from "../styles"; import { buttonStyles } from "../styles";
@ -48,14 +52,38 @@ const { init } = useToast();
const emits = defineEmits(["cancel"]); const emits = defineEmits(["cancel"]);
const Name = ref<string>(store.userName); const Name = ref<string>(store.userName);
const Surname = ref<string>(store.userSurname);
const HOST = inject('HOST');
const submit = () => { const submit = () => {
if (!Name.value || Name.value === store.userName) { if (!Name.value && !Surname.value) {
return emits("cancel"); return emits("cancel");
} }
if (Surname.value === store.userSurname && Name.value === store.userName) {
return emits("cancel");
}
let dataUser = JSON.parse(localStorage.getItem("profile")!);
dataUser.first_name = Name.value;
dataUser.surname = Surname.value;
const config = {
headers: { Authorization: `Bearer ${localStorage.getItem("token")}` }
};
axios
.patch(`${HOST}/api/v0/profiles/${store.profileID}`, {
"profile": dataUser,
}, config)
.then((response) => {
store.changeUserName(Name.value, Surname.value);
})
.catch((error) => {
init({
message: "Что-то пошло не так.",
color: "error",
});
store.changeUserName(Name.value); });
init({ message: "You've successfully changed your name", color: "success" });
init({ message: "Вы успешно изменили имя!", color: "success" });
emits("cancel"); emits("cancel");
}; };
</script> </script>

View File

@ -5,7 +5,7 @@
<p class="font-bold w-[200px]">Имя</p> <p class="font-bold w-[200px]">Имя</p>
<div class="flex-1"> <div class="flex-1">
<div class="max-w-[748px]"> <div class="max-w-[748px]">
{{ store.userName }} {{ store.userSurname }} {{ store.userName }}
</div> </div>
</div> </div>
<VaButton <VaButton

View File

@ -4,6 +4,7 @@ export const useUserStore = defineStore("user", {
state: () => { state: () => {
if (!localStorage.getItem("profile")) { if (!localStorage.getItem("profile")) {
return { return {
profileID: "",
userName: "", userName: "",
email: "", email: "",
memberSince: "", memberSince: "",
@ -12,18 +13,10 @@ export const useUserStore = defineStore("user", {
} }
} }
const dataUser = JSON.parse(localStorage.getItem("profile")!); const dataUser = JSON.parse(localStorage.getItem("profile")!);
let userName = "";
if (dataUser.first_name) {
userName = dataUser.first_name;
}
if (dataUser.surname) {
if (userName.length == 0) {
userName += " ";
}
userName += dataUser.surname;
}
return { return {
userName: userName, profileID: dataUser.id || "",
userName: dataUser.first_name || "",
userSurname: dataUser.surname || "",
email: dataUser.email || "", email: dataUser.email || "",
memberSince: "8/12/2020", memberSince: "8/12/2020",
pfp: "https://picsum.photos/id/22/200/300", pfp: "https://picsum.photos/id/22/200/300",
@ -36,8 +29,9 @@ export const useUserStore = defineStore("user", {
this.is2FAEnabled = !this.is2FAEnabled; this.is2FAEnabled = !this.is2FAEnabled;
}, },
changeUserName(userName: string) { changeUserName(userFirst: string, userSurname: string) {
this.userName = userName; this.userName = userFirst;
this.userSurname = userSurname;
}, },
}, },
}); });