Files
social-media/src/stores/user.js
Dominic Villemure 5f54b69e00 Added images
2024-06-30 23:39:56 -04:00

46 lines
1.6 KiB
JavaScript

import { ref } from 'vue'
import { defineStore } from 'pinia'
import MyUserModel from "@/models/myUserModel.js";
export const useUserStore = defineStore('user', () => {
const user = ref({});
function getCurrentUser() {
return this.user.value;
}
async function setCurrentUser(client) {
try {
const myUser = await client.get("/api/GetMyUser");
this.user.value = MyUserModel.createFromApiResult(myUser.data);
} catch (e){
this.user.value = MyUserModel.getDefaultUser();
console.log("User not logged.")
}
}
async function updateCurrentUser(client, myUserModel, profilePicture, bannerPicture, websiteIcon) {
this.user.value = myUserModel;
await client.patch("/api/UpdateMyUser/profile", myUserModel)
this.user.value.storedDataUrls.profilePictureUrl = await client.post("/api/UpdateMyUser/profile-picture", profilePicture, {
headers: {
'Content-Type': 'application/octet-stream',
}
});
this.user.value.storedDataUrls.bannerPictureUrl = await client.post("/api/UpdateMyUser/banner-picture", bannerPicture, {
headers: {
'Content-Type': 'application/octet-stream',
}
});
this.user.value.storedDataUrls.websiteIconUrl = await client.post("/api/UpdateMyUser/website-icon", websiteIcon, {
headers: {
'Content-Type': 'application/octet-stream',
}
});
}
return { user, getCurrentUser, setCurrentUser, updateCurrentUser }
})