1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-07-19 05:09:40 +02:00
mealie/frontend/composables/use-groups.ts

93 lines
2.1 KiB
TypeScript
Raw Normal View History

import { useUserApi } from "~/composables/api";
import type { GroupBase, GroupSummary } from "~/lib/api/types/user";
2024-03-15 19:41:26 +00:00
const groupSelfRef = ref<GroupSummary | null>(null);
2024-03-15 19:41:26 +00:00
const loading = ref(false);
export const useGroupSelf = function () {
const api = useUserApi();
2024-03-15 19:41:26 +00:00
async function refreshGroupSelf() {
loading.value = true;
const { data } = await api.groups.getCurrentUserGroup();
groupSelfRef.value = data;
loading.value = false;
}
const actions = {
get() {
2024-03-15 19:41:26 +00:00
if (!(groupSelfRef.value || loading.value)) {
refreshGroupSelf();
}
2024-03-15 19:41:26 +00:00
return groupSelfRef;
},
async updatePreferences() {
2024-03-15 19:41:26 +00:00
if (!groupSelfRef.value) {
await refreshGroupSelf();
}
if (!groupSelfRef.value?.preferences) {
return;
}
2024-03-15 19:41:26 +00:00
const { data } = await api.groups.setPreferences(groupSelfRef.value.preferences);
if (data) {
2024-03-15 19:41:26 +00:00
groupSelfRef.value.preferences = data;
}
},
};
const group = actions.get();
return { actions, group };
};
export const useGroups = function () {
const api = useUserApi();
const loading = ref(false);
2025-07-13 15:57:28 +02:00
const groups = ref<GroupSummary[] | null>(null);
2025-07-13 15:57:28 +02:00
async function getAllGroups() {
loading.value = true;
2025-07-13 15:57:28 +02:00
const { data } = await api.groups.getAll(1, -1, { orderBy: "name", orderDirection: "asc" });
if (data) {
groups.value = data.items;
}
else {
groups.value = null;
}
loading.value = false;
}
2025-07-13 15:57:28 +02:00
async function refreshAllGroups() {
await getAllGroups();
}
async function deleteGroup(id: string | number) {
loading.value = true;
const { data } = await api.groups.deleteOne(id);
loading.value = false;
2025-07-13 15:57:28 +02:00
await refreshAllGroups();
return data;
}
2022-05-21 21:22:02 +02:00
async function createGroup(payload: GroupBase) {
loading.value = true;
const { data } = await api.groups.createOne(payload);
if (data && groups.value) {
groups.value.push(data);
}
2025-07-13 15:57:28 +02:00
loading.value = false;
}
2025-07-13 15:57:28 +02:00
// Initialize data on first call
if (!groups.value) {
getAllGroups();
}
return { groups, getAllGroups, refreshAllGroups, deleteGroup, createGroup };
};