Files
WTP-front/store/user.store.ts
T
2025-05-06 12:41:50 +00:00

51 lines
1.4 KiB
TypeScript

import { defineStore } from "pinia";
import { UserResponseDTO } from "~/netcode/events/dto/userresponse.dto";
import { type FetchContext } from "ofetch";
export const useUserStore = defineStore("user", {
state: () => ({
users: {} as Record<string, UserResponseDTO>,
self: null as UserResponseDTO | null,
has_contacted: false,
}),
actions: {
async fetchUser(id: string) {
if (id in this.users) {
return this.users[id];
}
const config = useRuntimeConfig();
const data = await useFetch<UserResponseDTO>(
config.public.endpoint + `/users/` + id,
{
credentials: "include",
immediate: true,
},
);
if (data.data.value) {
this.users[data.data.value.id] = data.data.value;
}
return data.data.value;
},
async whoami() {
const config = useRuntimeConfig();
const req = await useFetch<UserResponseDTO>(
config.public.endpoint + "/whoami",
{
credentials: "include",
server: false,
immediate: true,
onResponse: (data: FetchContext<UserResponseDTO>) => {
if (data.response && data.response.ok && data.response._data) {
this.self = data.response._data;
}
this.has_contacted = true;
},
lazy: true,
},
);
return req;
},
},
});