mealie/frontend/composables/use-groups.ts
Hayden d1024e272d
Feature/automated meal planner (#939)
* cleanup oversized buttons

* add get all by category function to reciep repos

* fix shopping-list can_merge logic

* use randomized data for testing

* add random getter to repository for meal-planner

* add stub route for random meals

* cleanup global namespace

* add rules database type

* fix type

* add plan rules schema

* test plan rules methods

* add mealplan rules controller

* add new repository

* update frontend types

* formatting

* fix regression

* update autogenerated types

* add api class for mealplan rules

* add tests and fix bugs

* fix data returns

* proof of concept rules editor

* add tag support

* remove old group categories

* add tag support

* implement random by rules api

* change snack to sides

* remove incorrect typing

* split repo for custom methods

* fix query and use and_ clause

* use repo function

* remove old test

* update changelog
2022-02-07 19:03:11 -09:00

80 lines
1.8 KiB
TypeScript

import { useAsync, ref } from "@nuxtjs/composition-api";
import { useAsyncKey } from "./use-utils";
import { useUserApi } from "~/composables/api";
import { CreateGroup } from "~/api/class-interfaces/groups";
export const useGroupSelf = function () {
const api = useUserApi();
const actions = {
get() {
const group = useAsync(async () => {
const { data } = await api.groups.getCurrentUserGroup();
return data;
}, useAsyncKey());
return group;
},
async updatePreferences() {
if (!group.value) {
return;
}
const { data } = await api.groups.setPreferences(group.value.preferences);
if (data) {
group.value.preferences = data;
}
},
};
const group = actions.get();
return { actions, group };
};
export const useGroups = function () {
const api = useUserApi();
const loading = ref(false);
function getAllGroups() {
loading.value = true;
const asyncKey = String(Date.now());
const groups = useAsync(async () => {
const { data } = await api.groups.getAll();
return data;
}, asyncKey);
loading.value = false;
return groups;
}
async function refreshAllGroups() {
loading.value = true;
const { data } = await api.groups.getAll();
groups.value = data;
loading.value = false;
}
async function deleteGroup(id: string | number) {
loading.value = true;
const { data } = await api.groups.deleteOne(id);
loading.value = false;
refreshAllGroups();
return data;
}
async function createGroup(payload: CreateGroup) {
loading.value = true;
const { data } = await api.groups.createOne(payload);
if (data && groups.value) {
groups.value.push(data);
}
}
const groups = getAllGroups();
return { groups, getAllGroups, refreshAllGroups, deleteGroup, createGroup };
};