1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-07-25 08:09:41 +02:00

Feature/shareable recipes (#866)

* simplify context menu

* move computed to comp-api

* feat:  create share tokens for recipes for sharing recieps to non-users

* feat:  shareable recipe links with og tags
This commit is contained in:
Hayden 2021-12-05 11:55:46 -09:00 committed by GitHub
parent ba4107348f
commit b2673d75bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 914 additions and 199 deletions

View file

@ -34,7 +34,7 @@
:name="name"
:recipe-id="recipeId"
:use-items="{
delete: true,
delete: false,
edit: true,
download: true,
mealplanner: true,

View file

@ -43,7 +43,7 @@
:name="name"
:recipe-id="recipeId"
:use-items="{
delete: true,
delete: false,
edit: true,
download: true,
mealplanner: true,

View file

@ -1,5 +1,7 @@
<template>
<div class="text-center">
<!-- Recipe Share Dialog -->
<RecipeDialogShare v-model="shareDialog" :recipe-id="recipeId" :name="name" />
<BaseDialog
v-model="recipeDeleteDialog"
:title="$t('recipe.delete-recipe')"
@ -75,7 +77,7 @@
<script lang="ts">
import { defineComponent, reactive, toRefs, useContext, useRouter } from "@nuxtjs/composition-api";
import { useClipboard, useShare } from "@vueuse/core";
import RecipeDialogShare from "./RecipeDialogShare.vue";
import { useUserApi } from "~/composables/api";
import { alert } from "~/composables/use-toast";
import { MealType, planTypeOptions } from "~/composables/use-group-mealplan";
@ -92,11 +94,14 @@ export interface ContextMenuIncludes {
export interface ContextMenuItem {
title: string;
icon: string;
color: string;
color: string | undefined;
event: string;
}
export default defineComponent({
components: {
RecipeDialogShare,
},
props: {
useItems: {
type: Object as () => ContextMenuIncludes,
@ -152,6 +157,7 @@ export default defineComponent({
const api = useUserApi();
const state = reactive({
shareDialog: false,
recipeDeleteDialog: false,
mealplannerDialog: false,
loading: false,
@ -171,7 +177,7 @@ export default defineComponent({
edit: {
title: i18n.t("general.edit") as string,
icon: $globals.icons.edit,
color: "primary",
color: undefined,
event: "edit",
},
delete: {
@ -183,25 +189,25 @@ export default defineComponent({
download: {
title: i18n.t("general.download") as string,
icon: $globals.icons.download,
color: "primary",
color: undefined,
event: "download",
},
mealplanner: {
title: "Add to Plan",
icon: $globals.icons.calendar,
color: "primary",
color: undefined,
event: "mealplanner",
},
print: {
title: i18n.t("general.print") as string,
icon: $globals.icons.printer,
color: "primary",
color: undefined,
event: "print",
},
share: {
title: i18n.t("general.share") as string,
icon: $globals.icons.shareVariant,
color: "primary",
color: undefined,
event: "share",
},
};
@ -221,14 +227,6 @@ export default defineComponent({
const icon = props.menuIcon || $globals.icons.dotsVertical;
function getRecipeUrl() {
return `${window.location.origin}/recipe/${props.slug}`;
}
function getRecipeText() {
return i18n.t("recipe.share-recipe-message", [props.name]);
}
// ===========================================================================
// Context Menu Event Handler
@ -247,23 +245,6 @@ export default defineComponent({
}
}
const { share, isSupported: shareIsSupported } = useShare();
const { copy } = useClipboard();
async function handleShareEvent() {
if (shareIsSupported) {
share({
title: props.name,
url: getRecipeUrl(),
text: getRecipeText() as string,
});
} else {
await copy(getRecipeUrl());
alert.success("Recipe link copied to clipboard");
}
}
async function addRecipeToPlan() {
const { response } = await api.mealplans.createOne({
date: state.newMealdate,
@ -292,7 +273,9 @@ export default defineComponent({
mealplanner: () => {
state.mealplannerDialog = true;
},
share: handleShareEvent,
share: () => {
state.shareDialog = true;
},
};
function contextMenuEventHandler(eventKey: string) {

View file

@ -1,88 +0,0 @@
<template>
<div class="text-center">
<v-dialog v-model="dialog" width="700">
<template #activator="{ on, attrs }">
<v-btn color="accent" dark v-bind="attrs" v-on="on"> {{ $t("recipe.api-extras") }} </v-btn>
</template>
<v-card>
<v-card-title> {{ $t("recipe.api-extras") }} </v-card-title>
<v-card-text :key="formKey">
<v-row v-for="(value, key, index) in extras" :key="index" align="center">
<v-col cols="12" sm="1">
<v-btn fab text x-small color="white" elevation="0" @click="removeExtra(key)">
<v-icon color="error">{{ $globals.icons.delete }}</v-icon>
</v-btn>
</v-col>
<v-col cols="12" md="3" sm="6">
<v-text-field :label="$t('recipe.object-key')" :value="key" @input="updateKey(index)"> </v-text-field>
</v-col>
<v-col cols="12" md="8" sm="6">
<v-text-field v-model="extras[key]" :label="$t('recipe.object-value')"> </v-text-field>
</v-col>
</v-row>
</v-card-text>
<v-divider></v-divider>
<v-card-actions>
<v-form ref="addKey">
<v-text-field
v-model="newKeyName"
:label="$t('recipe.new-key-name')"
class="pr-4"
:rules="[rules.required, rules.whiteSpace]"
></v-text-field>
</v-form>
<v-btn color="info" text @click="append"> {{ $t("recipe.add-key") }} </v-btn>
<v-spacer></v-spacer>
<v-btn color="success" text @click="save"> {{ $t("general.save") }} </v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</div>
</template>
<script>
export default {
props: {
extras: {
type: Object,
default: () => ({}),
},
},
data() {
return {
newKeyName: null,
dialog: false,
formKey: 1,
rules: {
required: (v) => !!v || this.$i18n.t("recipe.key-name-required"),
whiteSpace: (v) => !v || v.split(" ").length <= 1 || this.$i18n.t("recipe.no-white-space-allowed"),
},
};
},
methods: {
save() {
this.$emit("save", this.extras);
this.dialog = false;
},
append() {
if (this.$refs.addKey.validate()) {
this.extras[this.newKeyName] = "value";
this.formKey += 1;
}
},
removeExtra(key) {
delete this.extras[key];
this.formKey += 1;
},
},
};
</script>
<style></style>

View file

@ -0,0 +1,181 @@
<template>
<div>
<BaseDialog v-model="dialog" title="Share Recipe" :icon="$globals.icons.link">
<v-card-text>
<v-menu
v-model="datePickerMenu"
:close-on-content-click="false"
transition="scale-transition"
offset-y
max-width="290px"
min-width="auto"
>
<template #activator="{ on, attrs }">
<v-text-field
v-model="expirationDate"
label="Expiration Date"
hint="Default 30 Days"
persistent-hint
:prepend-icon="$globals.icons.calendar"
v-bind="attrs"
readonly
v-on="on"
></v-text-field>
</template>
<v-date-picker v-model="expirationDate" no-title @input="pickerMenu = false"></v-date-picker>
</v-menu>
</v-card-text>
<v-card-actions class="justify-end">
<BaseButton small @click="createNewToken"> {{ $t("general.new") }}</BaseButton>
</v-card-actions>
<v-list-item v-for="token in tokens" :key="token.id" @click="shareRecipe(token.id)">
<v-list-item-avatar color="grey">
<v-icon dark class="pa-2"> {{ $globals.icons.link }} </v-icon>
</v-list-item-avatar>
<v-list-item-content>
<v-list-item-title> Expires At </v-list-item-title>
<v-list-item-subtitle>{{ $d(new Date(token.expiresAt), "long") }}</v-list-item-subtitle>
</v-list-item-content>
<v-list-item-action>
<v-btn icon @click.stop="deleteToken(token.id)">
<v-icon color="error lighten-1"> {{ $globals.icons.delete }} </v-icon>
</v-btn>
</v-list-item-action>
<v-list-item-action>
<v-btn icon @click.stop="copyTokenLink(token.id)">
<v-icon color="info lighten-1"> {{ $globals.icons.contentCopy }} </v-icon>
</v-btn>
</v-list-item-action>
</v-list-item>
</BaseDialog>
</div>
</template>
<script lang="ts">
import { defineComponent, computed, toRefs, reactive, useContext } from "@nuxtjs/composition-api";
import { whenever } from "@vueuse/shared";
import { useClipboard, useShare } from "@vueuse/core";
import { RecipeShareToken } from "~/api/class-interfaces/recipes/recipe-share";
import { useUserApi } from "~/composables/api";
import { alert } from "~/composables/use-toast";
export default defineComponent({
props: {
value: {
type: Boolean,
default: false,
},
recipeId: {
type: Number,
required: true,
},
name: {
type: String,
required: true,
},
},
setup(props, context) {
// V-Model Support
const dialog = computed({
get: () => {
return props.value;
},
set: (val) => {
console.log(val);
context.emit("input", val);
},
});
const state = reactive({
datePickerMenu: false,
expirationDate: "",
tokens: [] as RecipeShareToken[],
});
whenever(
() => props.value,
() => {
// Set expiration date to today + 30 Days
const today = new Date();
const expirationDate = new Date(today.getTime() + 30 * 24 * 60 * 60 * 1000);
state.expirationDate = expirationDate.toISOString().substring(0, 10);
refreshTokens();
}
);
// ============================================================
// Token Actions
const userApi = useUserApi();
async function createNewToken() {
// Convert expiration date to timestamp
const expirationDate = new Date(state.expirationDate);
const { data } = await userApi.recipes.share.createOne({
recipeId: props.recipeId,
expiresAt: expirationDate,
});
if (data) {
state.tokens.push(data);
}
}
async function deleteToken(id: string) {
await userApi.recipes.share.deleteOne(id);
state.tokens = state.tokens.filter((token) => token.id !== id);
}
async function refreshTokens() {
const { data } = await userApi.recipes.share.getAll(0, 999, { recipe_id: props.recipeId });
if (data) {
state.tokens = data;
}
}
const { i18n } = useContext();
const { share, isSupported: shareIsSupported } = useShare();
const { copy } = useClipboard();
function getRecipeText() {
return i18n.t("recipe.share-recipe-message", [props.name]);
}
function getTokenLink(token: string) {
return `${window.location.origin}/shared/recipes/${token}`;
}
async function copyTokenLink(token: string) {
await copy(getTokenLink(token));
alert.success("Recipe link copied to clipboard");
}
async function shareRecipe(token: string) {
if (shareIsSupported) {
share({
title: props.name,
url: getTokenLink(token),
text: getRecipeText() as string,
});
} else {
await copyTokenLink(token);
}
}
return {
...toRefs(state),
dialog,
createNewToken,
deleteToken,
shareRecipe,
copyTokenLink,
};
},
});
</script>

View file

@ -60,7 +60,7 @@
<div class="d-flex justify-space-between justify-start">
<h2 class="mb-4 mt-1">{{ $t("recipe.instructions") }}</h2>
<BaseButton minor :to="$router.currentRoute.path + '/cook'" cancel color="primary">
<BaseButton v-if="!public" minor :to="$router.currentRoute.path + '/cook'" cancel color="primary">
<template #icon>
{{ $globals.icons.primary }}
</template>
@ -186,6 +186,10 @@ export default defineComponent({
type: Boolean,
default: false,
},
public: {
type: Boolean,
default: false,
},
},
setup(props, context) {