-
-
Notifications
You must be signed in to change notification settings - Fork 814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Create Recipe From HTML or JSON #4274
Merged
Kuchenpirat
merged 18 commits into
mealie-recipes:mealie-next
from
michael-genson:feat/create-recipe-from-json
Sep 30, 2024
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
00fc61a
renamed create routes
michael-genson b1977c3
refactor/add route for directly providing HTML
michael-genson 6bf48d6
allow passing JSON string to html route
michael-genson 288fe40
add dummy URL for recipe scrapers
michael-genson 3f6e816
dev:generate
michael-genson f6dc3eb
add html/json page
michael-genson a8cfc5f
fix tests
michael-genson 00fd020
added tests
michael-genson f372d48
added broken routes to docs
michael-genson 3ad48fb
add note on schema.org format
michael-genson d371c07
formatting
michael-genson 04243d0
lint
michael-genson 53bf00d
weird leaky test data?
michael-genson 4a1ce4e
maybe fixes broken tests??
michael-genson 046a9f2
fixed wrong route on test
michael-genson b5a03f7
Merge remote-tracking branch 'upstream/mealie-next' into feat/create-…
michael-genson 6461f05
Merge branch 'mealie-next' into feat/create-recipe-from-json
Kuchenpirat bca6e02
Merge branch 'mealie-next' into feat/create-recipe-from-json
Kuchenpirat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
<template> | ||
<v-form ref="domUrlForm" @submit.prevent="createFromHtmlOrJson(newRecipeData, importKeywordsAsTags, stayInEditMode)"> | ||
<div> | ||
<v-card-title class="headline"> {{ $tc('recipe.import-from-html-or-json') }} </v-card-title> | ||
<v-card-text> | ||
<p> | ||
{{ $tc("recipe.import-from-html-or-json-description") }} | ||
</p> | ||
<p> | ||
{{ $tc("recipe.json-import-format-description-colon") }} | ||
<a href="https://schema.org/Recipe" target="_blank">https://schema.org/Recipe</a> | ||
</p> | ||
<v-switch | ||
v-model="isEditJSON" | ||
:label="$tc('recipe.json-editor')" | ||
class="mt-2" | ||
@change="handleIsEditJson" | ||
/> | ||
<LazyRecipeJsonEditor | ||
v-if="isEditJSON" | ||
v-model="newRecipeData" | ||
height="250px" | ||
class="mt-10" | ||
:options="EDITOR_OPTIONS" | ||
/> | ||
<v-textarea | ||
v-else | ||
v-model="newRecipeData" | ||
:label="$tc('new-recipe.recipe-html-or-json')" | ||
:prepend-inner-icon="$globals.icons.codeTags" | ||
validate-on-blur | ||
autofocus | ||
filled | ||
clearable | ||
class="rounded-lg mt-2" | ||
rounded | ||
:hint="$tc('new-recipe.url-form-hint')" | ||
persistent-hint | ||
/> | ||
<v-checkbox v-model="importKeywordsAsTags" hide-details :label="$tc('recipe.import-original-keywords-as-tags')" /> | ||
<v-checkbox v-model="stayInEditMode" hide-details :label="$tc('recipe.stay-in-edit-mode')" /> | ||
</v-card-text> | ||
<v-card-actions class="justify-center"> | ||
<div style="width: 250px"> | ||
<BaseButton | ||
:disabled="!newRecipeData" | ||
large | ||
rounded | ||
block | ||
type="submit" | ||
:loading="loading" | ||
/> | ||
</div> | ||
</v-card-actions> | ||
</div> | ||
</v-form> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { computed, defineComponent, reactive, toRefs, ref, useContext, useRoute, useRouter } from "@nuxtjs/composition-api"; | ||
import { AxiosResponse } from "axios"; | ||
import { useTagStore } from "~/composables/store/use-tag-store"; | ||
import { useUserApi } from "~/composables/api"; | ||
import { validators } from "~/composables/use-validators"; | ||
import { VForm } from "~/types/vuetify"; | ||
|
||
const EDITOR_OPTIONS = { | ||
mode: "code", | ||
search: false, | ||
mainMenuBar: false, | ||
}; | ||
|
||
export default defineComponent({ | ||
setup() { | ||
const state = reactive({ | ||
error: false, | ||
loading: false, | ||
isEditJSON: false, | ||
}); | ||
const { $auth } = useContext(); | ||
const route = useRoute(); | ||
const groupSlug = computed(() => route.value.params.groupSlug || $auth.user?.groupSlug || ""); | ||
const domUrlForm = ref<VForm | null>(null); | ||
|
||
const api = useUserApi(); | ||
const router = useRouter(); | ||
const tags = useTagStore(); | ||
|
||
const importKeywordsAsTags = computed({ | ||
get() { | ||
return route.value.query.use_keywords === "1"; | ||
}, | ||
set(v: boolean) { | ||
router.replace({ query: { ...route.value.query, use_keywords: v ? "1" : "0" } }); | ||
}, | ||
}); | ||
|
||
const stayInEditMode = computed({ | ||
get() { | ||
return route.value.query.edit === "1"; | ||
}, | ||
set(v: boolean) { | ||
router.replace({ query: { ...route.value.query, edit: v ? "1" : "0" } }); | ||
}, | ||
}); | ||
|
||
function handleResponse(response: AxiosResponse<string> | null, edit = false, refreshTags = false) { | ||
if (response?.status !== 201) { | ||
state.error = true; | ||
state.loading = false; | ||
return; | ||
} | ||
if (refreshTags) { | ||
tags.actions.refresh(); | ||
} | ||
|
||
router.push(`/g/${groupSlug.value}/r/${response.data}?edit=${edit.toString()}`); | ||
} | ||
|
||
const newRecipeData = ref<string | object | null>(null); | ||
|
||
function handleIsEditJson() { | ||
if (state.isEditJSON) { | ||
if (newRecipeData.value) { | ||
try { | ||
newRecipeData.value = JSON.parse(newRecipeData.value as string); | ||
} catch { | ||
newRecipeData.value = { "data": newRecipeData.value }; | ||
} | ||
} else { | ||
newRecipeData.value = {}; | ||
} | ||
} else if (newRecipeData.value && Object.keys(newRecipeData.value).length > 0) { | ||
newRecipeData.value = JSON.stringify(newRecipeData.value); | ||
} else { | ||
newRecipeData.value = null; | ||
} | ||
} | ||
handleIsEditJson(); | ||
|
||
async function createFromHtmlOrJson(htmlOrJsonData: string | object | null, importKeywordsAsTags: boolean, stayInEditMode: boolean) { | ||
if (!htmlOrJsonData || !domUrlForm.value?.validate()) { | ||
return; | ||
} | ||
|
||
let dataString; | ||
if (typeof htmlOrJsonData === "string") { | ||
dataString = htmlOrJsonData; | ||
} else { | ||
dataString = JSON.stringify(htmlOrJsonData); | ||
} | ||
|
||
state.loading = true; | ||
const { response } = await api.recipes.createOneByHtmlOrJson(dataString, importKeywordsAsTags); | ||
handleResponse(response, stayInEditMode, importKeywordsAsTags); | ||
} | ||
|
||
return { | ||
EDITOR_OPTIONS, | ||
domUrlForm, | ||
importKeywordsAsTags, | ||
stayInEditMode, | ||
newRecipeData, | ||
handleIsEditJson, | ||
createFromHtmlOrJson, | ||
...toRefs(state), | ||
validators, | ||
}; | ||
}, | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at some point we should try to simplify that list somehow. It kepps getting longer and longer...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I agree, we're gonna have to do something about it soon