add shared group self to composable

This commit is contained in:
Michael Genson 2024-03-15 19:41:26 +00:00
parent f83ab6ecc1
commit b6ccb9fbdb

View File

@ -1,30 +1,39 @@
import { useAsync, ref } from "@nuxtjs/composition-api"; import { useAsync, ref } from "@nuxtjs/composition-api";
import { useAsyncKey } from "./use-utils";
import { useUserApi } from "~/composables/api"; import { useUserApi } from "~/composables/api";
import { GroupBase } from "~/lib/api/types/user"; import { GroupBase, GroupInDB } from "~/lib/api/types/user";
const groupSelfRef = ref<GroupInDB | null>(null);
const loading = ref(false);
export const useGroupSelf = function () { export const useGroupSelf = function () {
const api = useUserApi(); const api = useUserApi();
async function refreshGroupSelf() {
loading.value = true;
const { data } = await api.groups.getCurrentUserGroup();
groupSelfRef.value = data;
loading.value = false;
}
const actions = { const actions = {
get() { get() {
const group = useAsync(async () => { if (!(groupSelfRef.value || loading.value)) {
const { data } = await api.groups.getCurrentUserGroup(); refreshGroupSelf();
}
return data; return groupSelfRef;
}, useAsyncKey());
return group;
}, },
async updatePreferences() { async updatePreferences() {
if (!group.value?.preferences) { if (!groupSelfRef.value) {
await refreshGroupSelf();
}
if (!groupSelfRef.value?.preferences) {
return; return;
} }
const { data } = await api.groups.setPreferences(group.value.preferences); const { data } = await api.groups.setPreferences(groupSelfRef.value.preferences);
if (data) { if (data) {
group.value.preferences = data; groupSelfRef.value.preferences = data;
} }
}, },
}; };