This commit is contained in:
amishabagri 2025-06-18 16:29:16 -04:00 committed by GitHub
commit 6887155f46
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 102 additions and 47 deletions

View file

@ -1,47 +1,49 @@
<template>
<div class="text-center"> <template>
<v-menu v-model="menu" offset-y top nudge-top="6" :close-on-content-click="false"> <div class = "text-center">
<template #activator="{ on, attrs }"> <v-menu v-model="menu" offset-y top nudge-top="6" :close-on-content-click="false">
<v-btn color="accent" dark v-bind="attrs" v-on="on"> <template #activator="{ on, attrs }">
<v-icon left> <v-btn color="accent" dark v-bind="attrs" v-on="on">
{{ $globals.icons.fileImage }} <v-icon left>
</v-icon> {{ $globals.icons.fileImage }}
{{ $t("general.image") }} </v-icon>
</v-btn> {{ $t("general.image") }}
</template> </v-btn>
<v-card width="400"> </template>
<v-card-title class="headline flex mb-0"> <v-card width="400">
<div> <v-card-title class="headline flex mb-0">
{{ $t("recipe.recipe-image") }} <div>
</div> {{ $t("recipe.recipe-image") }}
<AppButtonUpload </div>
class="ml-auto" <AppButtonUpload
url="none" class="ml-auto"
file-name="image" url="none"
:text-btn="false" file-name="image"
:post="false" :text-btn="false"
@uploaded="uploadImage" :post="false"
/> @uploaded="uploadImage"
</v-card-title> />
<v-card-text class="mt-n5"> </v-card-title>
<div> <v-card-text class="mt-n5">
<v-text-field v-model="url" :label="$t('general.url')" class="pt-5" clearable :messages="messages"> <div>
<template #append-outer> <v-text-field v-model="url" :label="$t('general.url')" class="pt-5" clearable :messages="messages">
<v-btn class="ml-2" color="primary" :loading="loading" :disabled="!slug" @click="getImageFromURL"> <template #append-outer>
{{ $t("general.get") }} <v-btn class="ml-2" color="primary" :loading="loading" :disabled="!slug" @click="getImageFromURL">
</v-btn> {{ $t("general.get") }}
</template> </v-btn>
</v-text-field> </template>
</div> </v-text-field>
</v-card-text> </div>
</v-card> </v-card-text>
</v-menu> </v-card>
</div> </v-menu>
</div>
</template> </template>
<script lang="ts"> <script lang="ts">
import { defineComponent, reactive, toRefs, useContext } from "@nuxtjs/composition-api"; import { defineComponent, reactive, toRefs, useContext } from "@nuxtjs/composition-api";
import { useUserApi } from "~/composables/api"; import { useUserApi } from "~/composables/api";
import { alert } from "~/composables/use-toast"; // Import the alert functions
const REFRESH_EVENT = "refresh"; const REFRESH_EVENT = "refresh";
const UPLOAD_EVENT = "upload"; const UPLOAD_EVENT = "upload";
@ -58,31 +60,81 @@ export default defineComponent({
url: "", url: "",
loading: false, loading: false,
menu: false, menu: false,
}) messages: [],
});
// Function to upload the image file
function uploadImage(fileObject: File) {
try{
// Check if the file has an acceptable extension
const allowedExtensions = [".jpg", ".jpeg", ".png", ".gif", ".webp",".heic",".avif"];
const fileExtension = fileObject.name.split(".").pop()?.toLowerCase();
// If the file extension is not in the allowed extensions list, show an error message
if (!fileExtension || !allowedExtensions.includes(`.${fileExtension}`)) {
alert.error(i18n.tc("general.invalid-image-extension"));
return;
}
function uploadImage(fileObject: File) {
context.emit(UPLOAD_EVENT, fileObject); context.emit(UPLOAD_EVENT, fileObject);
state.menu = false; state.menu = false;
// Show success message for image upload
alert.success(i18n.tc("general.image-uploaded"));
}
catch(error){
alert.error(i18n.tc("general.image-upload-failed"));
}
} }
const api = useUserApi(); const api = useUserApi();
const { i18n } = useContext();
// Function to check if the URL is reachable
async function isURLReachable(url: string) {
try {
const response = await fetch(url, { method: "HEAD" });
return response.ok; // If status is 200, response.ok will be true
} catch (error) {
return false; // Network error or invalid URL
}
}
// Function to get image from URL
async function getImageFromURL() { async function getImageFromURL() {
state.loading = true; state.loading = true;
if (await api.recipes.updateImagebyURL(props.slug, state.url)) {
context.emit(REFRESH_EVENT); // Check if the URL is reachable it uses HTTP codes
const isReachable = await isURLReachable(state.url);
if (!isReachable) {
alert.error(i18n.tc("general.invalid-url"));
state.loading = false;
return;
} }
// If the URL is reachable, make the API request to update the image using the URL
const response = await api.recipes.updateImagebyURL(props.slug, state.url);
// Check the response from the API and show appropriate alert
if (response) {
alert.success(i18n.tc("general.image-uploaded"));
context.emit(REFRESH_EVENT);
} else {
alert.error(i18n.tc("general.invalid-url"));
}
// Finalize loading state and menu visibility
state.loading = false; state.loading = false;
state.menu = false; state.menu = false;
} }
const { i18n } = useContext();
const messages = props.slug ? [""] : [i18n.t("recipe.save-recipe-before-use")];
return { return {
...toRefs(state), ...toRefs(state),
uploadImage, uploadImage,
getImageFromURL, getImageFromURL,
messages,
}; };
}, },
}); });

View file

@ -114,6 +114,9 @@
"home": "Home", "home": "Home",
"image": "Image", "image": "Image",
"image-upload-failed": "Image upload failed", "image-upload-failed": "Image upload failed",
"image-uploaded": "Image Uploaded",
"invalid-url": "The provided URL is invalid. Please check and try again.",
"invalid-image-extension": "Invalid file extension",
"import": "Import", "import": "Import",
"json": "JSON", "json": "JSON",
"keyword": "Keyword", "keyword": "Keyword",