1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-08-02 20:15:24 +02:00

feat: Use Backend for Recipe Post Actions (#4163)

This commit is contained in:
Michael Genson 2024-09-14 09:59:36 -05:00 committed by GitHub
parent 8bd26d2230
commit d8dbcac196
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 159 additions and 25 deletions

View file

@ -376,7 +376,7 @@ export default defineComponent({
const response = await groupRecipeActionsStore.execute(action, props.recipe);
if (action.actionType === "post") {
if (!response || (response.status >= 200 && response.status < 300)) {
if (!response?.error) {
alert.success(i18n.tc("events.message-sent"));
} else {
alert.error(i18n.tc("events.something-went-wrong"));

View file

@ -2,6 +2,7 @@ import { computed, reactive, ref } from "@nuxtjs/composition-api";
import { useStoreActions } from "./partials/use-actions-factory";
import { useUserApi } from "~/composables/api";
import { GroupRecipeActionOut, GroupRecipeActionType } from "~/lib/api/types/household";
import { RequestResponse } from "~/lib/api/types/non-generated";
import { Recipe } from "~/lib/api/types/recipe";
const groupRecipeActions = ref<GroupRecipeActionOut[] | null>(null);
@ -54,26 +55,15 @@ export const useGroupRecipeActions = function (
/* eslint-enable no-template-curly-in-string */
};
async function execute(action: GroupRecipeActionOut, recipe: Recipe): Promise<void | Response> {
async function execute(action: GroupRecipeActionOut, recipe: Recipe): Promise<void | RequestResponse<unknown>> {
const url = parseRecipeActionUrl(action.url, recipe);
switch (action.actionType) {
case "link":
window.open(url, "_blank")?.focus();
break;
return;
case "post":
return await fetch(url, {
method: "POST",
headers: {
// The "text/plain" content type header is used here to skip the CORS preflight request,
// since it may fail. This is fine, since we don't care about the response, we just want
// the request to get sent.
"Content-Type": "text/plain",
},
body: JSON.stringify(recipe),
}).catch((error) => {
console.error(error);
});
return await api.groupRecipeActions.triggerAction(action.id, recipe.slug || "");
default:
break;
}

View file

@ -165,6 +165,10 @@ export interface GroupRecipeActionOut {
householdId: string;
id: string;
}
export interface GroupRecipeActionPayload {
action: GroupRecipeActionOut;
content: unknown;
}
export interface HouseholdCreate {
groupId?: string | null;
name: string;

View file

@ -6,9 +6,14 @@ const prefix = "/api";
const routes = {
groupRecipeActions: `${prefix}/households/recipe-actions`,
groupRecipeActionsId: (id: string | number) => `${prefix}/households/recipe-actions/${id}`,
groupRecipeActionsIdTriggerRecipeSlug: (id: string | number, recipeSlug: string) => `${prefix}/households/recipe-actions/${id}/trigger/${recipeSlug}`,
};
export class GroupRecipeActionsAPI extends BaseCRUDAPI<CreateGroupRecipeAction, GroupRecipeActionOut> {
baseRoute = routes.groupRecipeActions;
itemRoute = routes.groupRecipeActionsId;
async triggerAction(id: string | number, recipeSlug: string) {
return await this.requests.post(routes.groupRecipeActionsIdTriggerRecipeSlug(id, recipeSlug), {});
}
}