Files
Brenda WallaceandDaniel O'Connor 75595350bc Easier recording of harvests from the front page (#5077)
* Implement AddSeedsPicker component and enhance planting selection UI; update styles and serializers for better seed management

* Enhance PlantingPicker component: add hints and titles for harvest and seed actions; update test to reflect changes in seed saving behavior

* Enhance harvest recording: allow harvesting from crops without plantings; update forms and modals for better user experience

* Refactor code structure for improved readability and maintainability

* Remove AddSeedsPicker component and its associated tests

* Add planting picker functionality for harvesting and seed saving

- Introduced PlantingPickersController to handle JSON responses for member's plantings.
- Updated HomeHelper to include crop ID in planting picker properties.
- Refactored PlantPicker and PlantSomethingModal components to support multiple gardens.
- Created PlantingFlow component to manage planting actions (harvest and save seeds).
- Implemented RecordDialog component to handle server-rendered links for harvesting and seed saving.
- Updated various views to include data attributes for triggering the record dialog.
- Added routes for planting picker and corresponding request specs for testing.
- Adjusted styles in _plantings.scss for better layout and spacing.

* Add job management features including job creation and wishlist functionality

* Remove WishlistJobsController and associated views and tests

* Add suggestion links rendering and fix routes formatting

* Refactor AddJobModal and JobPicker components to simplify props and enhance user experience

* Update seed addition test to check dialog presence and current path

* Apply suggestion from @CloCkWeRX

---------

Co-authored-by: Daniel O'Connor <daniel.oconnor@gmail.com>
2026-10-26 21:38:12 +10:30

51 lines
1.9 KiB
React

import React, {useEffect, useRef, useState} from 'react';
import {getJson} from '../api';
import PlantingFlow from './PlantingFlow';
// The record harvest and save seeds dialogs for every server-rendered link to
// the full form. Nothing of its own is drawn: it listens for clicks on any
// [data-record-dialog="harvest"] or [data-record-dialog="seeds"] link on the
// page and opens the same dialog the home page does. A link for a particular
// crop says which in data-crop-id and data-crop-name. The layout mounts it once.
//
// The member's plantings come from plantingsUrl when first needed. The link's
// own href stays the fallback without JavaScript, and when the plantings cannot
// be loaded.
export default function RecordDialog({plantings_url: plantingsUrl}) {
const [opened, setOpened] = useState(null); // {action, crop}, or null when closed
const [plantings, setPlantings] = useState(null);
const open = useRef(null);
open.current = async (trigger) => {
const action = trigger.dataset.recordDialog;
const crop = trigger.dataset.cropId ? {id: Number(trigger.dataset.cropId), name: trigger.dataset.cropName} : null;
if (!plantings) {
try {
const {ok, data} = await getJson(plantingsUrl);
if (!ok) throw new Error('load failed');
setPlantings(data);
} catch (error) {
window.location.href = trigger.getAttribute('href');
return;
}
}
setOpened({action, crop});
};
useEffect(() => {
function onClick(event) {
const trigger = event.target.closest('[data-record-dialog]');
if (!trigger) return;
event.preventDefault();
open.current(trigger);
}
document.addEventListener('click', onClick);
return () => document.removeEventListener('click', onClick);
}, []);
if (!opened || !plantings) return null;
return <PlantingFlow action={opened.action} crop={opened.crop} plantings={plantings} onClose={() => setOpened(null)} />;
}