-
-
Notifications
You must be signed in to change notification settings - Fork 795
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
fix: Message alerts for image upload using image upload button and url button #4665
Open
amishabagri
wants to merge
3
commits into
mealie-recipes:mealie-next
Choose a base branch
from
amishabagri:mealie_messages_image
base: mealie-next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,46 @@ | ||
<template> | ||
<div class="text-center"> | ||
<v-menu v-model="menu" offset-y top nudge-top="6" :close-on-content-click="false"> | ||
<template #activator="{ on, attrs }"> | ||
<v-btn color="accent" dark v-bind="attrs" v-on="on"> | ||
<v-icon left> | ||
{{ $globals.icons.fileImage }} | ||
</v-icon> | ||
{{ $t("general.image") }} | ||
</v-btn> | ||
</template> | ||
<v-card width="400"> | ||
<v-card-title class="headline flex mb-0"> | ||
<div> | ||
{{ $t("recipe.recipe-image") }} | ||
</div> | ||
<AppButtonUpload | ||
class="ml-auto" | ||
url="none" | ||
file-name="image" | ||
:text-btn="false" | ||
:post="false" | ||
@uploaded="uploadImage" | ||
/> | ||
</v-card-title> | ||
<v-card-text class="mt-n5"> | ||
<div> | ||
<v-text-field v-model="url" :label="$t('general.url')" class="pt-5" clearable :messages="messages"> | ||
<template #append-outer> | ||
<v-btn class="ml-2" color="primary" :loading="loading" :disabled="!slug" @click="getImageFromURL"> | ||
{{ $t("general.get") }} | ||
</v-btn> | ||
</template> | ||
</v-text-field> | ||
</div> | ||
</v-card-text> | ||
</v-card> | ||
</v-menu> | ||
</div> | ||
<template> | ||
<v-menu v-model="menu" offset-y top nudge-top="6" :close-on-content-click="false"> | ||
<template #activator="{ on, attrs }"> | ||
<v-btn color="accent" dark v-bind="attrs" v-on="on"> | ||
<v-icon left> | ||
{{ $globals.icons.fileImage }} | ||
</v-icon> | ||
{{ $t("general.image") }} | ||
</v-btn> | ||
</template> | ||
<v-card width="400"> | ||
<v-card-title class="headline flex mb-0"> | ||
<div> | ||
{{ $t("recipe.recipe-image") }} | ||
</div> | ||
<AppButtonUpload | ||
class="ml-auto" | ||
url="none" | ||
file-name="image" | ||
:text-btn="false" | ||
:post="false" | ||
@uploaded="uploadImage" | ||
/> | ||
</v-card-title> | ||
<v-card-text class="mt-n5"> | ||
<div> | ||
<v-text-field v-model="url" :label="$t('general.url')" class="pt-5" clearable :messages="messages"> | ||
<template #append-outer> | ||
<v-btn class="ml-2" color="primary" :loading="loading" :disabled="!slug" @click="getImageFromURL"> | ||
{{ $t("general.get") }} | ||
</v-btn> | ||
</template> | ||
</v-text-field> | ||
</div> | ||
</v-card-text> | ||
</v-card> | ||
</v-menu> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, reactive, toRefs, useContext } from "@nuxtjs/composition-api"; | ||
import { useUserApi } from "~/composables/api"; | ||
import { alert } from "~/composables/use-toast"; // Import the alert functions | ||
|
||
const REFRESH_EVENT = "refresh"; | ||
const UPLOAD_EVENT = "upload"; | ||
|
@@ -58,31 +57,81 @@ export default defineComponent({ | |
url: "", | ||
loading: 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"]; | ||
const fileExtension = fileObject.name.split(".").pop()?.toLowerCase(); | ||
Comment on lines
+67
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are missing |
||
|
||
// 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); | ||
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 { 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() { | ||
state.loading = true; | ||
if (await api.recipes.updateImagebyURL(props.slug, state.url)) { | ||
|
||
// 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.menu = false; | ||
} | ||
|
||
const { i18n } = useContext(); | ||
const messages = props.slug ? [""] : [i18n.t("recipe.save-recipe-before-use")]; | ||
|
||
return { | ||
...toRefs(state), | ||
uploadImage, | ||
getImageFromURL, | ||
messages, | ||
}; | ||
}, | ||
}); | ||
|
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.
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.
fix indentation