Rename and refactor the types passed over the API to be based on an entity that's either living or a tombstone. A living entity has a deleted property that's either undefined or false, while a tombstone has a deleted property set to true. All entities have a numeric id and an updatedAt timestamp. To sync entities, an array of replacements are passed around. Living entities are replaced with tombstones when they're deleted. And tombstones are replaced with living entities when restored.
73 lines
2.2 KiB
TypeScript
73 lines
2.2 KiB
TypeScript
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>(),
|
|
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.type.value = account?.type;
|
|
state.interestedEventIds.value = new Set(account?.interestedEventIds ?? []);
|
|
state.interestedEventSlotIds.value = new Set(account?.interestedEventSlotIds ?? []);
|
|
});
|
|
|
|
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(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,
|
|
};
|
|
});
|