import React, {useState} from 'react';
import {patchJson} from '../api';
import {daysBefore, formatDate} from '../dates';
import {errorMessages} from '../errors';
import Modal from './Modal';
// Marking a planting finished, from a garden card, as a dialog over the list:
// one question, when it finished, which is today unless you say otherwise. It
// saves with PATCH /plantings/:slug.json and hands the garden's refreshed card
// to onSaved (the planting then leaves the list), so the page is never left.
//
// The server needs the finish to come after the planting, so today is not offered
// for a planting made today, and the date box starts the day after it was planted.
export default function MarkFinishedModal({planting, iconUrl, onClose, onSaved}) {
const {today, planted_at: plantedOn} = planting;
const earliest = plantedOn ? daysBefore(plantedOn, -1) : undefined;
const todayAllowed = !plantedOn || today > plantedOn;
const [when, setWhen] = useState(todayAllowed ? 'today' : 'other');
const [customDate, setCustomDate] = useState(todayAllowed ? today : earliest);
const [saving, setSaving] = useState(false);
const [errors, setErrors] = useState([]);
// The server's "today" is the one that counts, not the browser's.
const finishedAt = when === 'today' ? today : customDate;
const choices = [todayAllowed && ['today', 'Today'], ['other', 'Enter date']].filter(Boolean);
async function save(event) {
event.preventDefault();
setSaving(true);
setErrors([]);
try {
const {ok, status, data} = await patchJson(planting.url, {planting: {finished: true, finished_at: finishedAt}});
if (ok) {
onSaved(data.garden); // closes this dialog
return;
}
setErrors(errorMessages(status, data, {signedOut: 'Please sign in again to mark this as finished.', forbidden: 'You can\'t change this planting.'}));
} catch (error) {
setErrors(['Couldn\'t reach the server. Please try again.']);
}
setSaving(false);
}
const title = (
<>
{iconUrl && }
Mark {planting.crop.name} as finished
>
);
return (
);
}