import {createPortal, useRef} from "preact/compat" import { useContext, useEffect, useState } from "preact/hooks" import {Button, SmallButton} from "../components/Button" import {Modal, ModalTitle} from "../components/Modals" import { PageLayout } from "../components/PageLayout" import { PlantsContext } from "../Contexts" import {InputField, SelectField, TextAreaField} from "../components/Form" import {getPicture, storePicture} from "../utilities/pictures" import {EditSVG, PlusSVG} from "../components/SVG"; import {classNames} from "../utilities/classNames"; const Plant = ({id}) => { const [addModal, setAddModal] = useState(false) const [editModal, setEditModal] = useState(false) const [plantForm, setPlantForm] = useState({}) const {plants, editPlant, removePlant, addAction, doneTask, history} = useContext(PlantsContext) const [plant, setPlant] = useState({}) const [actionForm, setActionForm] = useState({}) const [image, setImage] = useState(localStorage.getItem("image" + id) ?? '') const pictureName = 'picture-' + id const picture = useRef(null) const action_types = ['watering', 'spraying', 'bathing'] useEffect(() => { const plantFind = plants.find(plant => plant.id === Number(id)) setPlant(plantFind) }, []) useEffect(() => { setImage(localStorage.getItem(pictureName)) }, [picture]) const handleSubmit = (e) => { e.preventDefault() e.stopPropagation() if (!actionForm.action_type) { actionForm.action_type = action_types[0] } addAction(plant, actionForm) setAddModal(false) } const handleEditSubmit = (e) => { e.preventDefault() e.stopPropagation() editPlant(plantForm) setEditModal(false) setPlant(plantForm) } const isCloseModal = e => e.target.classList.contains("overlay") || e.target.classList.contains("close-button") const handleCloseAddModal = (e) => { if (isCloseModal(e)) { setAddModal(false) } } const handleOpenEditModal = () => { setEditModal(true) console.log(plant) setPlantForm(plant) } const handleCloseEditModal = (e) => { if (isCloseModal(e)) { setEditModal(false) } } const addPicture = e => storePicture(e, id) return

{ plant.name }

{/**/}

{ plant.description }

Actions

setAddModal(true)}>
{plant.actions && plant.actions.map(action => { let isDone = false let lastTask = false if (history()[action.id]) { lastTask = new Date(history()[action.id]) isDone = lastTask.addDays(Number(action.frequency)).toSQLDate() >= (new Date()).toSQLDate() } return
{action.action_type} every {action.frequency} days last task {lastTask ? lastTask.toFrDate() : 'never'}
})}
{createPortal( Add Action
setActionForm({ ...actionForm, action_type: e.target.value })}>Name setActionForm({ ...actionForm, frequency: e.target.value })}>Frequency
, app )} {createPortal( Edit Plant
setPlantForm({ ...plantForm, name: e.target.value }) }>Name setPlantForm({ ...plantForm, description: e.target.value })}>Description
, app )}
} export default Plant