61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
|
import type { Account } from "~/shared/types/account";
|
||
|
|
||
|
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>(),
|
||
|
type: ref<Account["type"]>(),
|
||
|
interestedIds: ref<Set<string>>(),
|
||
|
};
|
||
|
|
||
|
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.type.value = account?.type;
|
||
|
state.interestedIds.value = account?.interestedIds ? new Set(account.interestedIds) : undefined;
|
||
|
});
|
||
|
|
||
|
const getters = {
|
||
|
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),
|
||
|
};
|
||
|
|
||
|
const actions = {
|
||
|
async toggleInterestedId(id: string, slotIds?: string[]) {
|
||
|
if (!state.interestedIds.value) {
|
||
|
throw Error("accountStore.toggleInterestedId: Invalid state")
|
||
|
}
|
||
|
let newIds = [...state.interestedIds.value ?? []];
|
||
|
if (state.interestedIds.value.has(id)) {
|
||
|
newIds = newIds.filter(newId => newId !== id);
|
||
|
} else {
|
||
|
newIds.push(id);
|
||
|
if (slotIds) {
|
||
|
const filterIds = new Set(slotIds);
|
||
|
newIds = newIds.filter(newId => !filterIds.has(newId));
|
||
|
}
|
||
|
}
|
||
|
await $fetch("/api/account", {
|
||
|
method: "PATCH",
|
||
|
body: { interestedIds: newIds },
|
||
|
})
|
||
|
await sessionStore.fetch();
|
||
|
},
|
||
|
};
|
||
|
return {
|
||
|
...state,
|
||
|
...getters,
|
||
|
...actions,
|
||
|
};
|
||
|
});
|