The EventCard logic assume interestedIds not being present means the account can't set events as interested. Fix this logic by checking if the account is valid instead and always have interestedIds present on the account store.
60 lines
1.8 KiB
TypeScript
60 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>>(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.type.value = account?.type;
|
|
state.interestedIds.value = new Set(account?.interestedIds ?? []);
|
|
});
|
|
|
|
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/auth/account", {
|
|
method: "PATCH",
|
|
body: { interestedIds: newIds },
|
|
})
|
|
await sessionStore.fetch();
|
|
},
|
|
};
|
|
return {
|
|
...state,
|
|
...getters,
|
|
...actions,
|
|
};
|
|
});
|