2021-11-06 11:28:47 -08:00
|
|
|
import { useUserApi } from "~/composables/api";
|
2025-06-20 00:09:12 +07:00
|
|
|
import type { GroupBase, GroupSummary } from "~/lib/api/types/user";
|
2024-03-15 19:41:26 +00:00
|
|
|
|
2024-03-15 19:50:39 +00:00
|
|
|
const groupSelfRef = ref<GroupSummary | null>(null);
|
2024-03-15 19:41:26 +00:00
|
|
|
const loading = ref(false);
|
2021-08-06 16:28:12 -08:00
|
|
|
|
2021-09-05 22:05:29 -08:00
|
|
|
export const useGroupSelf = function () {
|
2021-11-06 11:28:47 -08:00
|
|
|
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;
|
|
|
|
}
|
2021-09-05 22:05:29 -08:00
|
|
|
|
|
|
|
const actions = {
|
|
|
|
get() {
|
2024-03-15 19:41:26 +00:00
|
|
|
if (!(groupSelfRef.value || loading.value)) {
|
|
|
|
refreshGroupSelf();
|
|
|
|
}
|
2021-09-05 22:05:29 -08:00
|
|
|
|
2024-03-15 19:41:26 +00:00
|
|
|
return groupSelfRef;
|
2021-09-05 22:05:29 -08:00
|
|
|
},
|
|
|
|
async updatePreferences() {
|
2024-03-15 19:41:26 +00:00
|
|
|
if (!groupSelfRef.value) {
|
|
|
|
await refreshGroupSelf();
|
|
|
|
}
|
|
|
|
if (!groupSelfRef.value?.preferences) {
|
2021-09-05 22:05:29 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-15 19:41:26 +00:00
|
|
|
const { data } = await api.groups.setPreferences(groupSelfRef.value.preferences);
|
2021-09-05 22:05:29 -08:00
|
|
|
|
|
|
|
if (data) {
|
2024-03-15 19:41:26 +00:00
|
|
|
groupSelfRef.value.preferences = data;
|
2021-09-05 22:05:29 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const group = actions.get();
|
|
|
|
|
|
|
|
return { actions, group };
|
|
|
|
};
|
|
|
|
|
2021-08-06 16:28:12 -08:00
|
|
|
export const useGroups = function () {
|
2021-11-06 11:28:47 -08:00
|
|
|
const api = useUserApi();
|
2021-08-06 16:28:12 -08:00
|
|
|
const loading = ref(false);
|
2025-07-13 15:57:28 +02:00
|
|
|
const groups = ref<GroupSummary[] | null>(null);
|
2021-08-06 16:28:12 -08:00
|
|
|
|
2025-07-13 15:57:28 +02:00
|
|
|
async function getAllGroups() {
|
2021-08-06 16:28:12 -08:00
|
|
|
loading.value = true;
|
2025-07-13 15:57:28 +02:00
|
|
|
const { data } = await api.groups.getAll(1, -1, { orderBy: "name", orderDirection: "asc" });
|
2022-06-25 14:39:38 -05:00
|
|
|
|
|
|
|
if (data) {
|
|
|
|
groups.value = data.items;
|
2025-06-20 00:09:12 +07:00
|
|
|
}
|
|
|
|
else {
|
2022-06-25 14:39:38 -05:00
|
|
|
groups.value = null;
|
|
|
|
}
|
|
|
|
|
2021-08-06 16:28:12 -08:00
|
|
|
loading.value = false;
|
|
|
|
}
|
|
|
|
|
2025-07-13 15:57:28 +02:00
|
|
|
async function refreshAllGroups() {
|
|
|
|
await getAllGroups();
|
|
|
|
}
|
|
|
|
|
2021-08-06 16:28:12 -08:00
|
|
|
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();
|
2021-08-06 16:28:12 -08:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:22:02 +02:00
|
|
|
async function createGroup(payload: GroupBase) {
|
2021-08-06 16:28:12 -08:00
|
|
|
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;
|
2021-08-06 16:28:12 -08:00
|
|
|
}
|
|
|
|
|
2025-07-13 15:57:28 +02:00
|
|
|
// Initialize data on first call
|
|
|
|
if (!groups.value) {
|
|
|
|
getAllGroups();
|
|
|
|
}
|
2021-08-06 16:28:12 -08:00
|
|
|
|
|
|
|
return { groups, getAllGroups, refreshAllGroups, deleteGroup, createGroup };
|
|
|
|
};
|