import React, {useState} from 'react'; import {postJson} from '../api'; import {daysBefore, localToday} from '../dates'; import {errorMessages} from '../errors'; import Modal from './Modal'; import WhenPicker from './WhenPicker'; // Transplanting a batch of seedlings out of the nursery: which garden they went // into, and when. The planting moves there; it isn't copied. export default function TransplantModal({planting, gardens, onClose, onDone}) { const today = localToday(); const [gardenId, setGardenId] = useState(gardens.length > 0 ? String(gardens[0].id) : ''); const [when, setWhen] = useState('today'); const [customDate, setCustomDate] = useState(''); const [saving, setSaving] = useState(false); const [errors, setErrors] = useState([]); const movedOn = {today, yesterday: daysBefore(today, 1), other: customDate}[when]; async function save() { setSaving(true); setErrors([]); try { const body = {garden_id: gardenId, date: movedOn}; const {ok, status, data} = await postJson(`/plantings/${planting.slug}/transplant.json`, body); if (ok) { await onDone(data); // keeps saying so until the page shows it return; } setErrors(errorMessages(status, data, {forbidden: 'You can\'t transplant into that garden.'})); } catch (error) { setErrors(['Couldn\'t reach the server. Please try again.']); } setSaving(false); } return (
{errors.length > 0 && (
That didn’t work.
    {errors.map((message) =>
  • {message}
  • )}
)}
Which garden did they go into? {gardens.length > 0 ? (
{gardens.map((g) => ( ))}
) :

You don’t have another garden to transplant into.

}
{ setWhen(choice); if (choice === 'other' && !customDate) setCustomDate(today); }} date={customDate} onDate={(event) => setCustomDate(event.target.value)} max={today} hint={movedOn} />
{saving ? 'Transplanting…' : ''}