2024-05-01 02:20:52 -05:00
|
|
|
import { computed, reactive, ref } from "@nuxtjs/composition-api";
|
|
|
|
import { useStoreActions } from "./partials/use-actions-factory";
|
|
|
|
import { useUserApi } from "~/composables/api";
|
2024-08-22 10:14:32 -05:00
|
|
|
import { GroupRecipeActionOut, GroupRecipeActionType } from "~/lib/api/types/household";
|
2024-09-14 09:59:36 -05:00
|
|
|
import { RequestResponse } from "~/lib/api/types/non-generated";
|
2024-05-01 02:20:52 -05:00
|
|
|
import { Recipe } from "~/lib/api/types/recipe";
|
|
|
|
|
|
|
|
const groupRecipeActions = ref<GroupRecipeActionOut[] | null>(null);
|
|
|
|
const loading = ref(false);
|
|
|
|
|
|
|
|
export function useGroupRecipeActionData() {
|
|
|
|
const data = reactive({
|
|
|
|
id: "",
|
2024-08-22 10:14:32 -05:00
|
|
|
actionType: "link" as GroupRecipeActionType,
|
2024-05-01 02:20:52 -05:00
|
|
|
title: "",
|
|
|
|
url: "",
|
|
|
|
});
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
data.id = "";
|
|
|
|
data.actionType = "link";
|
|
|
|
data.title = "";
|
|
|
|
data.url = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
data,
|
|
|
|
reset,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useGroupRecipeActions = function (
|
|
|
|
orderBy: string | null = "title",
|
|
|
|
orderDirection: string | null = "asc",
|
|
|
|
) {
|
|
|
|
const api = useUserApi();
|
|
|
|
async function refreshGroupRecipeActions() {
|
|
|
|
loading.value = true;
|
|
|
|
const { data } = await api.groupRecipeActions.getAll(1, -1, { orderBy, orderDirection });
|
|
|
|
groupRecipeActions.value = data?.items || null;
|
|
|
|
loading.value = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const recipeActions = computed<GroupRecipeActionOut[] | null>(() => {
|
|
|
|
return groupRecipeActions.value;
|
|
|
|
});
|
|
|
|
|
2024-12-03 22:24:59 +01:00
|
|
|
function getTokenLink(token: string, groupSlug: string) {
|
|
|
|
return `${window.location.origin}/g/${groupSlug}/shared/r/${token}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function parseRecipeActionUrl(url: string, recipe: Recipe): Promise<string> {
|
2024-05-01 02:20:52 -05:00
|
|
|
/* eslint-disable no-template-curly-in-string */
|
2024-12-03 22:24:59 +01:00
|
|
|
const shareLinkRegex = /\$\{share-link-expires-seconds-[0-9]+\}/g;
|
|
|
|
const group = (await api.groups.getOne(recipe.groupId || "")).data;
|
|
|
|
|
|
|
|
|
|
|
|
const shareLinkStringMatches = url.matchAll(shareLinkRegex);
|
|
|
|
if (shareLinkStringMatches) {
|
|
|
|
const shareLinkSet = new Set<string>();
|
|
|
|
for (const match of shareLinkStringMatches) {
|
|
|
|
shareLinkSet.add(match[0]);
|
|
|
|
}
|
|
|
|
const shareLinkStrings = Array.from(shareLinkSet.values());
|
|
|
|
|
|
|
|
for (let i = 0; i < shareLinkStrings.length; i++) {
|
|
|
|
const shareLinkString = shareLinkStrings[i];
|
|
|
|
const seconds = parseInt(shareLinkString.split("-")[4]);
|
|
|
|
const expires = new Date();
|
|
|
|
expires.setSeconds(expires.getSeconds() + seconds);
|
|
|
|
|
|
|
|
const shareLink = await api.recipes.share.createOne({
|
|
|
|
recipeId: recipe.id || "",
|
|
|
|
expiresAt: expires.toISOString(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const groupSlug = group?.slug || "";
|
|
|
|
url = url.replace(shareLinkString, getTokenLink(shareLink.data?.id || "", groupSlug));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-01 02:20:52 -05:00
|
|
|
return url
|
|
|
|
.replace("${url}", window.location.href)
|
|
|
|
.replace("${id}", recipe.id || "")
|
|
|
|
.replace("${slug}", recipe.slug || "")
|
|
|
|
/* eslint-enable no-template-curly-in-string */
|
|
|
|
};
|
|
|
|
|
2024-09-14 09:59:36 -05:00
|
|
|
async function execute(action: GroupRecipeActionOut, recipe: Recipe): Promise<void | RequestResponse<unknown>> {
|
2024-12-03 22:24:59 +01:00
|
|
|
const url = await parseRecipeActionUrl(action.url, recipe);
|
2024-05-01 02:20:52 -05:00
|
|
|
|
|
|
|
switch (action.actionType) {
|
|
|
|
case "link":
|
2024-12-03 22:24:59 +01:00
|
|
|
|
2024-05-01 02:20:52 -05:00
|
|
|
window.open(url, "_blank")?.focus();
|
2024-09-14 09:59:36 -05:00
|
|
|
return;
|
2024-05-01 02:20:52 -05:00
|
|
|
case "post":
|
2024-09-14 09:59:36 -05:00
|
|
|
return await api.groupRecipeActions.triggerAction(action.id, recipe.slug || "");
|
2024-05-01 02:20:52 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!groupRecipeActions.value && !loading.value) {
|
|
|
|
refreshGroupRecipeActions();
|
|
|
|
};
|
|
|
|
|
|
|
|
const actions = {
|
|
|
|
...useStoreActions<GroupRecipeActionOut>(api.groupRecipeActions, groupRecipeActions, loading),
|
|
|
|
flushStore() {
|
|
|
|
groupRecipeActions.value = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
actions,
|
|
|
|
execute,
|
|
|
|
recipeActions,
|
|
|
|
};
|
|
|
|
};
|