nidus-sync/ts/store/user.ts
2026-03-22 02:37:10 +00:00

89 lines
2.1 KiB
TypeScript

import { defineStore } from "pinia";
import { ref, computed } from "vue";
// Define interfaces matching your Go structs
interface URLsAPI {
communication: string;
}
interface URLs {
api: URLsAPI;
tegola: string;
}
interface User {
display_name: string;
initials: string;
notification_counts: NotificationCounts;
notifications: any[]; // Replace with proper type
organization: string; // Replace with proper type
role: string;
username: string;
}
interface UserResponse {
self: User;
urls: URLs;
}
interface NotificationCounts {
// Add the actual structure based on your API
[key: string]: number;
}
export const useUserStore = defineStore("user", () => {
// State
const display_name = ref<string | null>(null);
const error = ref<string | null>(null);
const initials = ref<string | null>(null);
const loading = ref(false);
const notification_counts = ref<NotificationCounts | null>(null);
const notifications = ref<any[] | null>(null);
const organization = ref<string | null>(null);
const role = ref<string | null>(null);
const urls = ref<URLs | null>(null);
const username = ref<string | null>(null);
// Actions
async function fetchUser() {
loading.value = true;
error.value = null;
try {
const response = await fetch("/api/user/self");
if (!response.ok) throw new Error("Failed to fetch user");
const data: UserResponse = await response.json();
display_name.value = data.self.display_name;
initials.value = data.self.initials;
notification_counts.value = data.self.notification_counts;
notifications.value = data.self.notifications;
organization.value = data.self.organization;
role.value = data.self.role;
urls.value = data.urls;
username.value = data.self.username;
console.log("loaded user data", data);
} catch (e) {
error.value = e instanceof Error ? e.message : "an error ocurred";
console.error("Error fetching user:", e);
} finally {
loading.value = false;
}
}
return {
// State
display_name,
error,
initials,
loading,
notification_counts,
notifications,
organization,
role,
urls,
username,
// Actions
fetchUser,
};
});