2022-01-05 21:20:57 +00:00
|
|
|
import { useAsync, ref, Ref, watch } from "@nuxtjs/composition-api";
|
|
|
|
import { format } from "date-fns";
|
2021-09-12 19:05:09 +00:00
|
|
|
import { useAsyncKey } from "./use-utils";
|
2021-11-06 19:28:47 +00:00
|
|
|
import { useUserApi } from "~/composables/api";
|
2022-05-21 19:22:02 +00:00
|
|
|
import { CreatePlanEntry, PlanEntryType, UpdatePlanEntry } from "~/types/api-types/meal-plan";
|
2021-11-20 23:30:38 +00:00
|
|
|
|
|
|
|
export const planTypeOptions = [
|
|
|
|
{ text: "Breakfast", value: "breakfast" },
|
|
|
|
{ text: "Lunch", value: "lunch" },
|
|
|
|
{ text: "Dinner", value: "dinner" },
|
2022-02-08 04:03:11 +00:00
|
|
|
{ text: "Side", value: "side" },
|
2021-11-20 23:30:38 +00:00
|
|
|
];
|
|
|
|
|
2022-01-05 21:20:57 +00:00
|
|
|
export interface DateRange {
|
|
|
|
start: Date;
|
|
|
|
end: Date;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useMealplans = function (range: Ref<DateRange>) {
|
2021-11-06 19:28:47 +00:00
|
|
|
const api = useUserApi();
|
2021-09-12 19:05:09 +00:00
|
|
|
const loading = ref(false);
|
|
|
|
const validForm = ref(true);
|
|
|
|
|
|
|
|
const actions = {
|
|
|
|
getAll() {
|
|
|
|
loading.value = true;
|
|
|
|
const units = useAsync(async () => {
|
|
|
|
const query = {
|
2022-07-02 17:44:01 +00:00
|
|
|
start_date: format(range.value.start, "yyyy-MM-dd"),
|
|
|
|
end_date: format(range.value.end, "yyyy-MM-dd"),
|
2021-09-12 19:05:09 +00:00
|
|
|
};
|
2022-01-16 02:38:11 +00:00
|
|
|
// @ts-ignore TODO Modify typing to allow for string start+limit for mealplans
|
2022-07-02 17:44:01 +00:00
|
|
|
const { data } = await api.mealplans.getAll(1, -1, { start_date: query.start_date, end_date: query.end_date });
|
2021-09-12 19:05:09 +00:00
|
|
|
|
2022-06-25 19:39:38 +00:00
|
|
|
if (data) {
|
|
|
|
return data.items;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-12 19:05:09 +00:00
|
|
|
}, useAsyncKey());
|
|
|
|
|
|
|
|
loading.value = false;
|
|
|
|
return units;
|
|
|
|
},
|
2022-01-09 06:15:23 +00:00
|
|
|
async refreshAll(this: void) {
|
2021-09-12 19:05:09 +00:00
|
|
|
loading.value = true;
|
|
|
|
const query = {
|
2022-07-02 17:44:01 +00:00
|
|
|
start_date: format(range.value.start, "yyyy-MM-dd"),
|
|
|
|
end_date: format(range.value.end, "yyyy-MM-dd"),
|
2021-09-12 19:05:09 +00:00
|
|
|
};
|
2022-01-16 02:38:11 +00:00
|
|
|
// @ts-ignore TODO Modify typing to allow for string start+limit for mealplans
|
2022-07-02 17:44:01 +00:00
|
|
|
const { data } = await api.mealplans.getAll(1, -1, { start_date: query.start_date, end_date: query.end_date });
|
2021-09-12 19:05:09 +00:00
|
|
|
|
2022-06-25 19:39:38 +00:00
|
|
|
if (data && data.items) {
|
|
|
|
mealplans.value = data.items;
|
2021-09-12 19:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loading.value = false;
|
|
|
|
},
|
2022-05-21 19:22:02 +00:00
|
|
|
async createOne(payload: CreatePlanEntry) {
|
2021-09-12 19:05:09 +00:00
|
|
|
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) {
|
2021-09-12 19:05:09 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
},
|
2021-11-20 23:30:38 +00:00
|
|
|
|
2022-05-21 19:22:02 +00:00
|
|
|
async setType(payload: UpdatePlanEntry, type: PlanEntryType) {
|
|
|
|
payload.entryType = type;
|
2021-11-20 23:30:38 +00:00
|
|
|
await this.updateOne(payload);
|
|
|
|
},
|
2021-09-12 19:05:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const mealplans = actions.getAll();
|
|
|
|
|
2022-01-05 21:20:57 +00:00
|
|
|
watch(range, actions.refreshAll);
|
|
|
|
|
2021-11-20 23:30:38 +00:00
|
|
|
return { mealplans, actions, validForm, loading };
|
2021-09-12 19:05:09 +00:00
|
|
|
};
|