1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-07-23 15:19:41 +02:00
mealie/frontend/composables/use-households.ts

112 lines
2.6 KiB
TypeScript
Raw Normal View History

import { useAdminApi, useUserApi } from "~/composables/api";
import type { HouseholdCreate, HouseholdInDB } from "~/lib/api/types/household";
2024-08-22 10:14:32 -05:00
const householdSelfRef = ref<HouseholdInDB | null>(null);
const loading = ref(false);
export const useHouseholdSelf = function () {
const api = useUserApi();
async function refreshHouseholdSelf() {
loading.value = true;
const { data } = await api.households.getCurrentUserHousehold();
householdSelfRef.value = data;
loading.value = false;
}
const actions = {
get() {
if (!(householdSelfRef.value || loading.value)) {
refreshHouseholdSelf();
}
return householdSelfRef;
},
async updatePreferences() {
if (!householdSelfRef.value) {
await refreshHouseholdSelf();
}
if (!householdSelfRef.value?.preferences) {
return;
}
const { data } = await api.households.setPreferences(householdSelfRef.value.preferences);
if (data) {
householdSelfRef.value.preferences = data;
}
return data || undefined;
2024-08-22 10:14:32 -05:00
},
};
const household = actions.get();
return { actions, household };
};
export const useAdminHouseholds = function () {
const api = useAdminApi();
2024-08-22 10:14:32 -05:00
const loading = ref(false);
2025-07-13 15:57:28 +02:00
const households = ref<HouseholdInDB[] | null>(null);
2024-08-22 10:14:32 -05:00
2025-07-13 15:57:28 +02:00
async function getAllHouseholds() {
2024-08-22 10:14:32 -05:00
loading.value = true;
2025-07-13 15:57:28 +02:00
const { data } = await api.households.getAll(1, -1, { orderBy: "name, group.name", orderDirection: "asc" });
2024-08-22 10:14:32 -05:00
if (data) {
households.value = data.items;
}
else {
households.value = null;
2024-08-22 10:14:32 -05:00
}
loading.value = false;
}
2025-07-13 15:57:28 +02:00
async function refreshAllHouseholds() {
await getAllHouseholds();
}
2024-08-22 10:14:32 -05:00
async function deleteHousehold(id: string | number) {
loading.value = true;
const { data } = await api.households.deleteOne(id);
loading.value = false;
2025-07-13 15:57:28 +02:00
await refreshAllHouseholds();
2024-08-22 10:14:32 -05:00
return data;
}
async function createHousehold(payload: HouseholdCreate) {
loading.value = true;
const { data } = await api.households.createOne(payload);
if (data && households.value) {
households.value.push(data);
2024-08-22 10:14:32 -05:00
}
2025-07-13 15:57:28 +02:00
loading.value = false;
2024-08-22 10:14:32 -05:00
}
function useHouseholdsInGroup(groupIdRef: Ref<string>) {
return computed(
() => {
return (households.value && groupIdRef.value)
? households.value.filter(h => h.groupId === groupIdRef.value)
: [];
2024-08-22 10:14:32 -05:00
},
);
}
2025-07-13 15:57:28 +02:00
if (!households.value) {
getAllHouseholds();
}
2024-08-22 10:14:32 -05:00
return {
households,
useHouseholdsInGroup,
getAllHouseholds,
refreshAllHouseholds,
deleteHousehold,
createHousehold,
};
};