Redirect when photo can't be retrieved
This commit is contained in:
parent
47c9374a5a
commit
7f14b69605
@ -2,6 +2,7 @@ import PhotoForm from '@/photo/PhotoForm';
|
||||
import { convertPhotoToFormData } from '@/photo/form';
|
||||
import AdminChildPage from '@/components/AdminChildPage';
|
||||
import { getPhoto } from '@/services/postgres';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export const runtime = 'edge';
|
||||
|
||||
@ -12,6 +13,8 @@ interface Props {
|
||||
export default async function PhotoPageEdit({ params: { photoId } }: Props) {
|
||||
const photo = await getPhoto(photoId);
|
||||
|
||||
if (!photo) { redirect('/admin'); }
|
||||
|
||||
return (
|
||||
<AdminChildPage>
|
||||
<PhotoForm
|
||||
|
||||
@ -9,6 +9,7 @@ import { cc } from '@/utility/css';
|
||||
import { Metadata } from 'next';
|
||||
import { BASE_URL } from '@/site/config';
|
||||
import { getPhoto, getPhotos } from '@/services/postgres';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export const runtime = 'edge';
|
||||
|
||||
@ -20,9 +21,13 @@ export async function generateMetadata(
|
||||
{ params: { photoId } }: Props
|
||||
): Promise<Metadata> {
|
||||
const photo = await getPhoto(photoId);
|
||||
|
||||
if (!photo) { return {}; }
|
||||
|
||||
const title = photo.title;
|
||||
const description = ogImageDescriptionForPhoto(photo);
|
||||
const images = ogImageUrlForPhoto(photo);
|
||||
|
||||
return {
|
||||
title,
|
||||
description,
|
||||
@ -46,6 +51,9 @@ export default async function PhotoPage({
|
||||
children,
|
||||
}: Props) {
|
||||
const photo = await getPhoto(photoId);
|
||||
|
||||
if (!photo) { redirect('/'); }
|
||||
|
||||
const photos = await getPhotos();
|
||||
|
||||
return <>
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import PhotoModal from '@/photo/PhotoModal';
|
||||
import { getPhoto } from '@/services/postgres';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export const runtime = 'edge';
|
||||
|
||||
@ -9,5 +10,8 @@ interface Props {
|
||||
|
||||
export default async function Share({ params: { photoId }}: Props) {
|
||||
const photo = await getPhoto(photoId);
|
||||
|
||||
if (!photo) { return redirect('/'); }
|
||||
|
||||
return <PhotoModal photo={photo} />;
|
||||
}
|
||||
|
||||
@ -4,7 +4,9 @@ import {
|
||||
PhotoDbInsert,
|
||||
translatePhotoId,
|
||||
parsePhotoFromDb,
|
||||
Photo,
|
||||
} from '@/photo';
|
||||
import { isValidUUID } from '@/utility/string';
|
||||
|
||||
const PHOTO_DEFAULT_LIMIT = 100;
|
||||
|
||||
@ -184,10 +186,12 @@ export const getPhotos = async (
|
||||
return photos;
|
||||
};
|
||||
|
||||
export const getPhoto = (id: string) =>
|
||||
sqlGetPhotoFromDb(
|
||||
// Check for photo id forwarding
|
||||
// and convert short ids to uuids
|
||||
translatePhotoId(id)
|
||||
)
|
||||
.then(photos => photos[0]);
|
||||
export const getPhoto = async (id: string): Promise<Photo | undefined> => {
|
||||
// Check for photo id forwarding
|
||||
// and convert short ids to uuids
|
||||
const photoId = translatePhotoId(id);
|
||||
return isValidUUID(photoId)
|
||||
? sqlGetPhotoFromDb(photoId)
|
||||
.then(photos => photos.length > 0 ? photos[0] : undefined)
|
||||
: Promise.resolve(undefined);
|
||||
};
|
||||
|
||||
2
src/utility/string.ts
Normal file
2
src/utility/string.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export const isValidUUID = (id: string): boolean =>
|
||||
/^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/i.test(id);
|
||||
Loading…
Reference in New Issue
Block a user