I firmly believe in free software. The application I'm making here have capabilities that I've not seen in any system. It presents itself as an opportunity to collaborate on a tool that serves the people rather than corporations. Whose incentives are to help people rather, not make the most money. And whose terms ensure that these freedoms and incentives cannot be taken back or subverted. I license this software under the AGPL.
82 lines
2.6 KiB
TypeScript
82 lines
2.6 KiB
TypeScript
/*
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
import type { ApiAccount, ApiAccountPatch } from "~/shared/types/api";
|
|
|
|
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>(),
|
|
locale: ref<string>(),
|
|
type: ref<ApiAccount["type"]>(),
|
|
interestedEventIds: ref<Set<number>>(new Set()),
|
|
interestedEventSlotIds: ref<Set<number>>(new Set()),
|
|
};
|
|
|
|
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;
|
|
state.locale.value = account?.locale;
|
|
state.type.value = account?.type;
|
|
state.interestedEventIds.value = new Set(account?.interestedEventIds ?? []);
|
|
state.interestedEventSlotIds.value = new Set(account?.interestedEventSlotIds ?? []);
|
|
});
|
|
|
|
const getters = {
|
|
isAdmin: computed(() => state.type.value === "admin"),
|
|
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),
|
|
activeLocale: computed(() => state.locale.value || runtimeConfig.public.defaultLocale),
|
|
defaultLocale: computed(() => runtimeConfig.public.defaultLocale),
|
|
};
|
|
|
|
const actions = {
|
|
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);
|
|
}
|
|
}
|
|
const patch: ApiAccountPatch = {
|
|
interestedEventIds: [...newEventIds],
|
|
interestedEventSlotIds: [...newSlotIds],
|
|
}
|
|
await $fetch("/api/auth/account", {
|
|
method: "PATCH",
|
|
body: patch,
|
|
})
|
|
await sessionStore.fetch();
|
|
},
|
|
};
|
|
return {
|
|
...state,
|
|
...getters,
|
|
...actions,
|
|
};
|
|
});
|