mealie/frontend/composables/use-group-mealplan.ts

103 lines
2.8 KiB
TypeScript
Raw Normal View History

import { useAsync, ref, Ref, watch } from "@nuxtjs/composition-api";
import { format } from "date-fns";
import { useAsyncKey } from "./use-utils";
import { useUserApi } from "~/composables/api";
2022-05-21 19:22:02 +00:00
import { CreatePlanEntry, PlanEntryType, UpdatePlanEntry } from "~/types/api-types/meal-plan";
export const planTypeOptions = [
{ text: "Breakfast", value: "breakfast" },
{ text: "Lunch", value: "lunch" },
{ text: "Dinner", value: "dinner" },
{ text: "Side", value: "side" },
];
export interface DateRange {
start: Date;
end: Date;
}
export const useMealplans = function (range: Ref<DateRange>) {
const api = useUserApi();
const loading = ref(false);
const validForm = ref(true);
const actions = {
getAll() {
loading.value = true;
const units = useAsync(async () => {
const query = {
start_date: format(range.value.start, "yyyy-MM-dd"),
end_date: format(range.value.end, "yyyy-MM-dd"),
};
// @ts-ignore TODO Modify typing to allow for string start+limit for mealplans
const { data } = await api.mealplans.getAll(1, -1, { start_date: query.start_date, end_date: query.end_date });
if (data) {
return data.items;
} else {
return null;
}
}, useAsyncKey());
loading.value = false;
return units;
},
Use composition API for more components, enable more type checking (#914) * Activate more linting rules from eslint and typescript * Properly add VForm as type information * Fix usage of native types * Fix more linting issues * Rename vuetify types file, add VTooltip * Fix some more typing problems * Use composition API for more components * Convert RecipeRating * Convert RecipeNutrition * Convert more components to composition API * Fix globals plugin for type checking * Add missing icon types * Fix vuetify types in Nuxt context * Use composition API for RecipeActionMenu * Convert error.vue to composition API * Convert RecipeContextMenu to composition API * Use more composition API and type checking in recipe/create * Convert AppButtonUpload to composition API * Fix some type checking in RecipeContextMenu * Remove unused components BaseAutoForm and BaseColorPicker * Convert RecipeCategoryTagDialog to composition API * Convert RecipeCardSection to composition API * Convert RecipeCategoryTagSelector to composition API * Properly import vuetify type definitions * Convert BaseButton to composition API * Convert AutoForm to composition API * Remove unused requests API file * Remove static routes from recipe API * Fix more type errors * Convert AppHeader to composition API, fixing some search bar focus problems * Convert RecipeDialogSearch to composition API * Update API types from pydantic models, handle undefined values * Improve more typing problems * Add types to other plugins * Properly type the CRUD API access * Fix typing of static image routes * Fix more typing stuff * Fix some more typing problems * Turn off more rules
2022-01-09 06:15:23 +00:00
async refreshAll(this: void) {
loading.value = true;
const query = {
start_date: format(range.value.start, "yyyy-MM-dd"),
end_date: format(range.value.end, "yyyy-MM-dd"),
};
// @ts-ignore TODO Modify typing to allow for string start+limit for mealplans
const { data } = await api.mealplans.getAll(1, -1, { start_date: query.start_date, end_date: query.end_date });
if (data && data.items) {
mealplans.value = data.items;
}
loading.value = false;
},
2022-05-21 19:22:02 +00:00
async createOne(payload: CreatePlanEntry) {
loading.value = true;
const { data } = await api.mealplans.createOne(payload);
if (data) {
this.refreshAll();
}
loading.value = false;
},
2022-05-21 19:22:02 +00:00
async updateOne(updateData: UpdatePlanEntry) {
if (!updateData.id) {
return;
}
loading.value = true;
const { data } = await api.mealplans.updateOne(updateData.id, updateData);
if (data) {
this.refreshAll();
}
loading.value = false;
},
async deleteOne(id: string | number) {
loading.value = true;
const { data } = await api.mealplans.deleteOne(id);
if (data) {
this.refreshAll();
}
},
2022-05-21 19:22:02 +00:00
async setType(payload: UpdatePlanEntry, type: PlanEntryType) {
payload.entryType = type;
await this.updateOne(payload);
},
};
const mealplans = actions.getAll();
watch(range, actions.refreshAll);
return { mealplans, actions, validForm, loading };
};