import React, {useState} from 'react'; import {patchJson} from '../api'; import Modal from './Modal'; // A button on a post's page that opens a dialog for choosing the card photo: // the photo that stands for the post on the posts list, an author's page and // shared links. The photos are the post's own. Saving reloads, because the // page is server rendered. export default function CardPhotoDialog({ url, photos, selectedId = null, subject, excerpt = '', byline = '', addPhotoUrl = null, addPhotoLabel = '', }) { const [open, setOpen] = useState(false); const [chosenId, setChosenId] = useState(selectedId || (photos[0] && photos[0].id)); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const chosen = photos.find((p) => p.id === chosenId); async function save() { setSaving(true); setError(null); const {ok} = await patchJson(url, {post: {card_photo_id: chosenId}}); if (ok) { setTimeout(() => window.location.reload(), 0); return; } setSaving(false); setError('The photo could not be saved. Try again.'); } return ( <> {open && ( setOpen(false)}>

Photos come from your Flickr account. This photo stands for your post wherever it appears as a card: the posts list, your page and links shared elsewhere.

{photos.length === 0 ? (

Pick a photo from Flickr for this post first, then you can choose it here.

) : (
Photos in this post
{photos.map((photo) => ( ))}
)} {addPhotoUrl && (

{/* The page's photo dialog opens from any [data-photo-dialog] link. */} setOpen(false)} > Pick a different photo from Flickr Or leave it empty and the first photo is used.

)} {chosen && ( <>

How it will look

Preview of the card photo
{subject}

{excerpt}

{byline}
)} {error &&
{error}
}
)} ); }