feat: manage timeline event image (#8928)
This commit is contained in:
1 parent
144a2c335c
commit
ac2a4b6ca6
6 files changed
+157
-13
No files matched your search
@@ -103,11 +103,12 @@
|
||||
<script setup lang="ts">
|
||||
import { useThrottleFn, whenever } from "@vueuse/core";
|
||||
import RecipeTimelineItem from "./RecipeTimelineItem.vue";
|
||||
import type { TimelineEventEdit } from "./RecipeTimelineContextMenu.vue";
|
||||
import { useTimelinePreferences } from "~/composables/use-users/preferences";
|
||||
import { useTimelineEventTypes } from "~/composables/recipes/use-recipe-timeline-events";
|
||||
import { alert } from "~/composables/use-toast";
|
||||
import { useUserApi } from "~/composables/api";
|
||||
import type { Recipe, RecipeTimelineEventOut, RecipeTimelineEventUpdate, TimelineEventType } from "~/lib/api/types/recipe";
|
||||
import type { Recipe, RecipeTimelineEventOut, TimelineEventType } from "~/lib/api/types/recipe";
|
||||
|
||||
interface Props {
|
||||
modelValue?: boolean;
|
||||
@@ -180,16 +181,33 @@ function toggleEventTypeOption(option: TimelineEventType) {
|
||||
}
|
||||
|
||||
// Timeline Actions
|
||||
async function updateTimelineEvent(index: number, event: RecipeTimelineEventUpdate) {
|
||||
async function updateTimelineEvent(index: number, edit: TimelineEventEdit) {
|
||||
const eventId = timelineEvents.value[index].id;
|
||||
const { response } = await api.recipes.updateTimelineEvent(eventId, event);
|
||||
const { response } = await api.recipes.updateTimelineEvent(eventId, edit.event);
|
||||
if (response?.status !== 200) {
|
||||
alert.error(i18n.t("events.something-went-wrong") as string);
|
||||
return;
|
||||
}
|
||||
|
||||
const updatedEvent = response.data;
|
||||
if (edit.image) {
|
||||
try {
|
||||
const { data } = await api.recipes.updateTimelineEventImage(eventId, edit.image.file, edit.image.fileName);
|
||||
if (!data) {
|
||||
throw new Error("Failed to upload image");
|
||||
}
|
||||
updatedEvent.image = data.image;
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Failed to upload image for timeline event:", error);
|
||||
timelineEvents.value[index] = updatedEvent;
|
||||
alert.error(i18n.t("events.something-went-wrong") as string);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the local event data to reflect the changes in the UI
|
||||
timelineEvents.value[index] = response.data;
|
||||
timelineEvents.value[index] = updatedEvent;
|
||||
|
||||
alert.success(i18n.t("events.event-updated") as string);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,41 @@
|
||||
<v-form ref="domEditEventForm" @submit.prevent>
|
||||
<v-text-field v-model="localEvent.subject" :label="$t('general.subject')" />
|
||||
<v-textarea v-model="localEvent.eventMessage" :label="$t('general.message')" rows="4" />
|
||||
<v-row>
|
||||
<v-spacer />
|
||||
<v-col cols="auto">
|
||||
<AppButtonUpload
|
||||
v-if="!newImage && !hasExistingImage"
|
||||
url="none"
|
||||
file-name="image"
|
||||
accept="image/*"
|
||||
:text="$t('recipe.upload-image')"
|
||||
:text-btn="false"
|
||||
:post="false"
|
||||
@uploaded="uploadImage"
|
||||
/>
|
||||
<v-btn v-else color="error" @click="clearImage">
|
||||
<v-icon start>
|
||||
{{ $globals.icons.close }}
|
||||
</v-icon>
|
||||
{{ $t("recipe.remove-image") }}
|
||||
</v-btn>
|
||||
</v-col>
|
||||
</v-row>
|
||||
<v-img
|
||||
v-if="hasExistingImage"
|
||||
:src="imageUrl"
|
||||
max-height="300"
|
||||
contain
|
||||
class="mt-4"
|
||||
/>
|
||||
<ImageCropper
|
||||
v-if="newImage && newImagePreviewUrl"
|
||||
:img="newImagePreviewUrl"
|
||||
cropper-width="100%"
|
||||
class="mt-4"
|
||||
@save="updateUploadedImage"
|
||||
/>
|
||||
</v-form>
|
||||
</v-card-text>
|
||||
</BaseDialog>
|
||||
@@ -75,13 +110,18 @@
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useI18n, useNuxtApp } from "#imports";
|
||||
import type { RecipeTimelineEventOut } from "~/lib/api/types/recipe";
|
||||
import type { RecipeTimelineEventOut, RecipeTimelineEventUpdate } from "~/lib/api/types/recipe";
|
||||
|
||||
export interface TimelineContextMenuIncludes {
|
||||
edit: boolean;
|
||||
delete: boolean;
|
||||
}
|
||||
|
||||
export interface TimelineEventEdit {
|
||||
event: RecipeTimelineEventUpdate;
|
||||
image?: { file: Blob | File; fileName: string };
|
||||
}
|
||||
|
||||
export interface ContextMenuItem {
|
||||
title: string;
|
||||
icon: string;
|
||||
@@ -98,6 +138,7 @@ const props = defineProps<{
|
||||
elevation?: number | null;
|
||||
color?: string;
|
||||
event: RecipeTimelineEventOut;
|
||||
imageUrl?: string;
|
||||
menuIcon?: string | null;
|
||||
}>();
|
||||
|
||||
@@ -149,8 +190,36 @@ watch(() => props.event, (val) => {
|
||||
localEvent.value = { ...val };
|
||||
});
|
||||
|
||||
const newImage = ref<Blob | File>();
|
||||
const newImageName = ref("");
|
||||
const newImagePreviewUrl = ref<string>();
|
||||
const removeExistingImage = ref(false);
|
||||
const hasExistingImage = computed(() => !!props.imageUrl && !removeExistingImage.value && !newImage.value);
|
||||
|
||||
function uploadImage(fileObject: File) {
|
||||
newImage.value = fileObject;
|
||||
newImageName.value = fileObject.name;
|
||||
newImagePreviewUrl.value = URL.createObjectURL(fileObject);
|
||||
}
|
||||
|
||||
function updateUploadedImage(fileObject: Blob) {
|
||||
newImage.value = fileObject;
|
||||
newImagePreviewUrl.value = URL.createObjectURL(fileObject);
|
||||
}
|
||||
|
||||
function clearImage() {
|
||||
newImage.value = undefined;
|
||||
newImageName.value = "";
|
||||
newImagePreviewUrl.value = undefined;
|
||||
removeExistingImage.value = true;
|
||||
}
|
||||
|
||||
function openEditDialog() {
|
||||
localEvent.value = { ...props.event };
|
||||
newImage.value = undefined;
|
||||
newImageName.value = "";
|
||||
newImagePreviewUrl.value = undefined;
|
||||
removeExistingImage.value = false;
|
||||
recipeEventEditDialog.value = true;
|
||||
}
|
||||
function openDeleteDialog() {
|
||||
@@ -171,7 +240,14 @@ function contextMenuEventHandler(eventKey: string) {
|
||||
loading.value = false;
|
||||
}
|
||||
function submitEdit() {
|
||||
emit("update", { ...localEvent.value });
|
||||
const edit: TimelineEventEdit = {
|
||||
event: {
|
||||
...localEvent.value,
|
||||
image: removeExistingImage.value ? "does not have image" : localEvent.value.image,
|
||||
},
|
||||
image: newImage.value ? { file: newImage.value, fileName: newImageName.value } : undefined,
|
||||
};
|
||||
emit("update", edit);
|
||||
recipeEventEditDialog.value = false;
|
||||
}
|
||||
</script>
|
||||
@@ -25,14 +25,15 @@
|
||||
{{ $d(new Date(event.timestamp || "")) }}
|
||||
</v-chip>
|
||||
</v-col>
|
||||
<v-col v-else cols="9" class="text-wrap break-word" style="margin: auto; text-align: center">
|
||||
<v-col v-else class="text-wrap break-word" style="margin: auto; text-align: center">
|
||||
{{ event.subject }}
|
||||
</v-col>
|
||||
<v-col :cols="useMobileFormat ? 'auto' : '1'" class="px-0 pt-0">
|
||||
<v-col cols="auto" class="px-0 pt-0">
|
||||
<RecipeTimelineContextMenu
|
||||
v-if="currentUser && currentUser.id == event.userId && event.eventType != 'system'"
|
||||
:menu-top="false"
|
||||
:event="event"
|
||||
:image-url="eventImageUrl"
|
||||
:menu-icon="$globals.icons.dotsVertical"
|
||||
color="transparent"
|
||||
:elevation="0"
|
||||
@@ -92,9 +93,10 @@
|
||||
<script setup lang="ts">
|
||||
import RecipeCardMobile from "./RecipeCardMobile.vue";
|
||||
import RecipeTimelineContextMenu from "./RecipeTimelineContextMenu.vue";
|
||||
import type { TimelineEventEdit } from "./RecipeTimelineContextMenu.vue";
|
||||
import { useStaticRoutes } from "~/composables/api";
|
||||
import { useTimelineEventTypes } from "~/composables/recipes/use-recipe-timeline-events";
|
||||
import type { Recipe, RecipeTimelineEventOut, RecipeTimelineEventUpdate } from "~/lib/api/types/recipe";
|
||||
import type { Recipe, RecipeTimelineEventOut } from "~/lib/api/types/recipe";
|
||||
import UserAvatar from "~/components/Domain/User/UserAvatar.vue";
|
||||
import SafeMarkdown from "~/components/global/SafeMarkdown.vue";
|
||||
|
||||
@@ -111,7 +113,7 @@ const props = withDefaults(defineProps<Props>(), {
|
||||
|
||||
defineEmits<{
|
||||
selected: [];
|
||||
update: [event: RecipeTimelineEventUpdate];
|
||||
update: [edit: TimelineEventEdit];
|
||||
delete: [];
|
||||
}>();
|
||||
|
||||
@@ -166,12 +168,18 @@ const icon = computed(() => {
|
||||
});
|
||||
|
||||
const hideImage = ref(false);
|
||||
const imageKey = ref(1);
|
||||
watch(() => props.event, () => {
|
||||
hideImage.value = false;
|
||||
imageKey.value++;
|
||||
});
|
||||
|
||||
const eventImageUrl = computed<string>(() => {
|
||||
if (props.event.image !== "has image") {
|
||||
return "";
|
||||
}
|
||||
|
||||
return recipeTimelineEventSmallImage(props.event.recipeId, props.event.id);
|
||||
return recipeTimelineEventSmallImage(props.event.recipeId, props.event.id, imageKey.value);
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
@@ -27,8 +27,8 @@ export const useStaticRoutes = () => {
|
||||
return `${prefix}/media/recipes/${recipeId}/images/timeline/${timelineEventId}/original.webp`;
|
||||
}
|
||||
|
||||
function recipeTimelineEventSmallImage(recipeId: string, timelineEventId: string) {
|
||||
return `${prefix}/media/recipes/${recipeId}/images/timeline/${timelineEventId}/min-original.webp`;
|
||||
function recipeTimelineEventSmallImage(recipeId: string, timelineEventId: string, key: string | number = 1) {
|
||||
return `${prefix}/media/recipes/${recipeId}/images/timeline/${timelineEventId}/min-original.webp?rnd=${key}`;
|
||||
}
|
||||
|
||||
function recipeTimelineEventTinyImage(recipeId: string, timelineEventId: string) {
|
||||
|
||||
@@ -115,6 +115,9 @@ class RecipeTimelineEventsController(BaseCrudController):
|
||||
@router.put("/{item_id}", response_model=RecipeTimelineEventOut)
|
||||
def update_one(self, item_id: UUID4, data: RecipeTimelineEventUpdate):
|
||||
event = self.mixins.patch_one(data, item_id)
|
||||
if data.image == TimelineEventImage.does_not_have_image.value:
|
||||
RecipeDataService(event.recipe_id).delete_image(event.image_dir)
|
||||
|
||||
recipe = self.group_recipes.get_one(event.recipe_id, "id")
|
||||
if recipe:
|
||||
self.publish_event(
|
||||
|
||||
@@ -359,6 +359,45 @@ def test_timeline_event_update_image(
|
||||
assert updated_event.image == TimelineEventImage.has_image.value
|
||||
|
||||
|
||||
def test_timeline_event_remove_image(
|
||||
api_client: TestClient,
|
||||
unique_user: TestUser,
|
||||
recipes: list[Recipe],
|
||||
test_image_jpg: str,
|
||||
):
|
||||
new_event_data = {
|
||||
"recipe_id": str(recipes[0].id),
|
||||
"user_id": str(unique_user.user_id),
|
||||
"subject": random_string(),
|
||||
"event_type": "info",
|
||||
}
|
||||
event_response = api_client.post(api_routes.recipes_timeline_events, json=new_event_data, headers=unique_user.token)
|
||||
new_event = RecipeTimelineEventOut.model_validate(event_response.json())
|
||||
|
||||
with open(test_image_jpg, "rb") as f:
|
||||
r = api_client.put(
|
||||
api_routes.recipes_timeline_events_item_id_image(new_event.id),
|
||||
files={"image": ("test_image_jpg.jpg", f, "image/jpeg")},
|
||||
data={"extension": "jpg"},
|
||||
headers=unique_user.token,
|
||||
)
|
||||
r.raise_for_status()
|
||||
|
||||
image_files = list(new_event.image_dir.glob("*.webp"))
|
||||
assert image_files
|
||||
|
||||
event_response = api_client.put(
|
||||
api_routes.recipes_timeline_events_item_id(new_event.id),
|
||||
json={"subject": new_event.subject, "image": TimelineEventImage.does_not_have_image.value},
|
||||
headers=unique_user.token,
|
||||
)
|
||||
assert event_response.status_code == 200
|
||||
|
||||
updated_event = RecipeTimelineEventOut.model_validate(event_response.json())
|
||||
assert updated_event.image == TimelineEventImage.does_not_have_image.value
|
||||
assert not any(image_file.exists() for image_file in image_files)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("use_other_household_user", [True, False])
|
||||
def test_create_recipe_with_timeline_event(
|
||||
api_client: TestClient,
|
||||
|
||||
Reference in new issue
Block a user