diff --git a/src/admin/AdminPhotoMenu.tsx b/src/admin/AdminPhotoMenu.tsx index 5ce5a001..3d57b160 100644 --- a/src/admin/AdminPhotoMenu.tsx +++ b/src/admin/AdminPhotoMenu.tsx @@ -3,6 +3,7 @@ import { ComponentProps, useMemo, useRef } from 'react'; import { getPathComponents, + PARAM_REDIRECT, PATH_ROOT, pathForAdminPhotoEdit, pathForTag, @@ -81,7 +82,8 @@ export default function AdminPhotoMenu({ size={14} className="translate-x-[1px] translate-y-[-0.5px]" />, - href: pathForAdminPhotoEdit(photo.id), + href: pathForAdminPhotoEdit(photo.id) + + `?${PARAM_REDIRECT}=${encodeURIComponent(path)}`, ...showKeyCommands && { keyCommand: KEY_COMMANDS.edit }, }]; if (includeFavorite) { @@ -184,6 +186,7 @@ export default function AdminPhotoMenu({ return { items }; }, [ + path, appText, photo, showKeyCommands, diff --git a/src/app/path.ts b/src/app/path.ts index 29d4e5b6..7573f070 100644 --- a/src/app/path.ts +++ b/src/app/path.ts @@ -88,8 +88,11 @@ export const PATH_API_PRESIGNED_URL = `${PATH_API_STORAGE}/presigned-url`; // Modifiers const EDIT = 'edit'; const IMAGE = 'image'; + +// Parameters export const PARAM_UPLOAD_TITLE = 'title'; export const PARAM_SELECT = 'select'; +export const PARAM_REDIRECT = 'redirect'; // Special characters export const MISSING_FIELD = '-'; diff --git a/src/photo/actions.ts b/src/photo/actions.ts index 38beed2e..5e350684 100644 --- a/src/photo/actions.ts +++ b/src/photo/actions.ts @@ -40,7 +40,6 @@ import { } from '@/cache'; import { revalidatePhoto, getPhotosCached } from './cache'; import { - PATH_ADMIN_PHOTOS, PATH_ADMIN_RECIPES, PATH_ADMIN_TAGS, PATH_ROOT, @@ -108,7 +107,6 @@ export const createPhotoAction = async (formData: FormData) => await addAlbumTitlesToPhoto(albumTitles, photo.id, false); await propagateRecipeTitleIfNecessary(formData, photo); revalidateAllKeysAndPaths(); - redirect(PATH_ADMIN_PHOTOS); } }); @@ -343,8 +341,6 @@ export const updatePhotoAction = async (formData: FormData) => }); revalidateAllKeysAndPaths(); - - redirect(PATH_ADMIN_PHOTOS); }); export const toggleFavoritePhotoAction = async ( diff --git a/src/photo/form/PhotoForm.tsx b/src/photo/form/PhotoForm.tsx index 082505fe..59d3422d 100644 --- a/src/photo/form/PhotoForm.tsx +++ b/src/photo/form/PhotoForm.tsx @@ -26,7 +26,11 @@ import { createPhotoAction, updatePhotoAction } from '../actions'; import SubmitButtonWithStatus from '@/components/SubmitButtonWithStatus'; import Link from 'next/link'; import { clsx } from 'clsx/lite'; -import { PATH_ADMIN_PHOTOS, PATH_ADMIN_UPLOADS } from '@/app/path'; +import { + PARAM_REDIRECT, + PATH_ADMIN_PHOTOS, + PATH_ADMIN_UPLOADS, +} from '@/app/path'; import { toastSuccess, toastWarning } from '@/toast'; import { getDimensionsFromSize } from '@/utility/size'; import ImageWithFallback from '@/components/image/ImageWithFallback'; @@ -66,6 +70,7 @@ import { TbPhoto } from 'react-icons/tb'; import { Albums } from '@/album'; import FieldsetAlbum from '@/album/FieldsetAlbum'; import Form from 'next/form'; +import { useRouter, useSearchParams } from 'next/navigation'; const THUMBNAIL_SIZE = 300; @@ -102,6 +107,10 @@ export default function PhotoForm({ onFormDataChange?: (formData: Partial) => void, onFormStatusChange?: (pending: boolean) => void }) { + const router = useRouter(); + + const redirectParam = useSearchParams().get(PARAM_REDIRECT); + const [formData, setFormData] = useState>(initialPhotoForm); const [formErrors, setFormErrors] = @@ -481,6 +490,9 @@ export default function PhotoForm({ ? createPhotoAction : updatePhotoAction )(data) + .then(() => { + router.push(redirectParam ?? PATH_ADMIN_PHOTOS); + }) .catch(e => { if (e.message !== 'NEXT_REDIRECT') { setFormActionErrorMessage(e.message);