2025-06-30 18:58:24 +02:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
*/
|
2025-06-11 21:05:17 +02:00
|
|
|
import type { ApiAccount, ApiAccountPatch } from "~/shared/types/api";
|
2025-05-24 20:01:23 +02:00
|
|
|
|
|
|
|
export const useAccountStore = defineStore("account", () => {
|
|
|
|
const runtimeConfig = useRuntimeConfig();
|
|
|
|
const sessionStore = useSessionStore();
|
|
|
|
const state = {
|
|
|
|
valid: ref<boolean>(false),
|
|
|
|
id: ref<number>(),
|
|
|
|
name: ref<string>(),
|
|
|
|
timezone: ref<string>(),
|
2025-06-13 21:50:22 +02:00
|
|
|
locale: ref<string>(),
|
2025-06-11 21:05:17 +02:00
|
|
|
type: ref<ApiAccount["type"]>(),
|
|
|
|
interestedEventIds: ref<Set<number>>(new Set()),
|
|
|
|
interestedEventSlotIds: ref<Set<number>>(new Set()),
|
2025-05-24 20:01:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
watchEffect(() => {
|
|
|
|
const account = sessionStore.account;
|
|
|
|
state.valid.value = Boolean(account);
|
|
|
|
state.id.value = account?.id;
|
|
|
|
state.name.value = account?.name;
|
|
|
|
state.timezone.value = account?.timezone;
|
2025-06-13 21:50:22 +02:00
|
|
|
state.locale.value = account?.locale;
|
2025-05-24 20:01:23 +02:00
|
|
|
state.type.value = account?.type;
|
2025-06-11 21:05:17 +02:00
|
|
|
state.interestedEventIds.value = new Set(account?.interestedEventIds ?? []);
|
|
|
|
state.interestedEventSlotIds.value = new Set(account?.interestedEventSlotIds ?? []);
|
2025-05-24 20:01:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const getters = {
|
2025-06-23 00:20:33 +02:00
|
|
|
isAdmin: computed(() => state.type.value === "admin"),
|
2025-05-24 20:01:23 +02:00
|
|
|
isCrew: computed(() => state.type.value === "crew" || state.type.value === "admin"),
|
|
|
|
canEdit: computed(() => state.type.value === "admin" || state.type.value === "crew" ),
|
|
|
|
canEditPublic: computed(() => state.type.value === "admin"),
|
|
|
|
activeTimezone: computed(() => state.timezone.value || runtimeConfig.public.defaultTimezone),
|
|
|
|
defaultTimezone: computed(() => runtimeConfig.public.defaultTimezone),
|
2025-06-13 21:50:22 +02:00
|
|
|
activeLocale: computed(() => state.locale.value || runtimeConfig.public.defaultLocale),
|
|
|
|
defaultLocale: computed(() => runtimeConfig.public.defaultLocale),
|
2025-05-24 20:01:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const actions = {
|
2025-06-11 21:05:17 +02:00
|
|
|
async toggleInterestedId(type: "event" | "slot", id: number, slotIds?: number[]) {
|
|
|
|
let newEventIds = new Set(state.interestedEventIds.value);
|
|
|
|
let newSlotIds = new Set(state.interestedEventSlotIds.value);
|
|
|
|
if (type === "event") {
|
|
|
|
if (newEventIds.has(id)) {
|
|
|
|
newEventIds.delete(id)
|
|
|
|
} else {
|
|
|
|
newEventIds.add(id);
|
|
|
|
if (slotIds) {
|
|
|
|
for (const slotId of slotIds) {
|
|
|
|
newSlotIds.delete(slotId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (type === "slot") {
|
|
|
|
if (newSlotIds.has(id)) {
|
|
|
|
newSlotIds.delete(id);
|
|
|
|
} else {
|
|
|
|
newSlotIds.add(id);
|
2025-05-24 20:01:23 +02:00
|
|
|
}
|
|
|
|
}
|
2025-06-11 21:05:17 +02:00
|
|
|
const patch: ApiAccountPatch = {
|
|
|
|
interestedEventIds: [...newEventIds],
|
|
|
|
interestedEventSlotIds: [...newSlotIds],
|
|
|
|
}
|
2025-05-31 21:44:19 +02:00
|
|
|
await $fetch("/api/auth/account", {
|
2025-05-24 20:01:23 +02:00
|
|
|
method: "PATCH",
|
2025-06-11 21:05:17 +02:00
|
|
|
body: patch,
|
2025-05-24 20:01:23 +02:00
|
|
|
})
|
|
|
|
await sessionStore.fetch();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
...getters,
|
|
|
|
...actions,
|
|
|
|
};
|
|
|
|
});
|