import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from "react" import EntityDisplayComponent from "../EntityDisplayComponent" import { InnerFormContext } from "../formContext" export interface SiteAnswerComponentProps { value?: any onValueChange: any siteTypes?: any onNextOrComments?: (ev: any) => void entityFilter?: any formCtx: InnerFormContext } /** Imperative handle exposed by SiteAnswerComponent via ref. */ export interface SiteAnswerComponentRef { focus(): void } /** Gets the entity type from siteTypes, normalising legacy formats. */ function getEntityType(siteTypes: any[] | undefined): string { // Convert to new entity type (legacy sometimes had capital letter and spaces) const siteType = (siteTypes ? siteTypes[0] : undefined) || "water_point" return siteType.toLowerCase().replace(new RegExp(" ", "g"), "_") } /** Answer component for site (entity) questions. */ const SiteAnswerComponent = forwardRef( function SiteAnswerComponent(props, ref) { const { value, onValueChange, siteTypes, onNextOrComments, entityFilter, formCtx } = props const [text, setText] = useState(value?.code || "") const [reloadKey, setReloadKey] = useState(0) const inputRef = useRef(null) useImperativeHandle(ref, () => ({ focus() { inputRef.current?.focus() } })) useEffect(() => { setText(value?.code ?? "") }, [value?.code]) const entityType = getEntityType(siteTypes) const isAsset = ["water_asset", "sanitation_asset"].includes((siteTypes || ["water_point"])[0]) const handleKeyDown = (ev: React.KeyboardEvent) => { if (onNextOrComments != null) { // When pressing ENTER or TAB if (ev.key === "Enter" || ev.key === "Tab") { onNextOrComments(ev) // It's important to prevent the default behavior when handling tabs (or else the tab is applied after the focus change) ev.preventDefault() } } } const handleSelectClick = () => { formCtx.selectEntity!({ entityType, entityFilter, callback: (entityId: any) => { console.log(`Issue584:${new Date().toISOString()}: after selectEntity ${entityType} ${entityId}`) // Get entity formCtx.getEntityById(entityType, entityId, (entity: any) => { console.log( `Issue584:${new Date().toISOString()}: after getEntityById ${entityType} ${entityId} ${JSON.stringify( entity )}` ) if (!entity) { alert(formCtx.T("Site has been deleted. Please select another site.")) return } if (!entity.code) { alert( formCtx.T( "Unable to select that site as it does not have an mWater ID. Please synchronize first with the server." ) ) return } onValueChange({ code: entity.code }) }) } }) } const handleEdit = useCallback((entity: any) => { formCtx.editEntity!(entityType, entity._id, () => { setReloadKey((k) => k + 1) }) }, [formCtx, entityType]) const handleCanEdit = useCallback((entity: any) => { return formCtx.canEditEntity ? formCtx.canEditEntity(entityType, entity) : false }, [formCtx, entityType]) const handleChange = (ev: React.ChangeEvent) => { setText(ev.target.value) } const handleBlur = (ev: React.FocusEvent) => { if (ev.target.value) { onValueChange({ code: ev.target.value.substring(0, 100) }) } else { onValueChange(null) } } return (

) } ) export default SiteAnswerComponent