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 { useAsyncKey } from "./use-utils";
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 () {
const api = useUserApi();
async function refreshGroupSelf() {
loading.value = true;
const { data } = await api.groups.getCurrentUserGroup();
groupSelfRef.value = data;
loading.value = false;
}
const actions = {
get() {
const group = useAsync(async () => {
const { data } = await api.groups.getCurrentUserGroup();
if (!(groupSelfRef.value || loading.value)) {
refreshGroupSelf();
}
return data;
}, useAsyncKey());
return group;
return groupSelfRef;
},
async updatePreferences() {
if (!group.value?.preferences) {
if (!groupSelfRef.value) {
await refreshGroupSelf();
}
if (!groupSelfRef.value?.preferences) {
return;
}
const { data } = await api.groups.setPreferences(group.value.preferences);
const { data } = await api.groups.setPreferences(groupSelfRef.value.preferences);
if (data) {
group.value.preferences = data;
groupSelfRef.value.preferences = data;
}
},
};