import React, { useEffect, useState } from 'react'; import { CustomSelectControl } from "../ui/custom-select-control"; import { CheckboxControl } from "../ui/checkbox-control"; import type { CustomSelectOption } from "../ui/custom-select-control"; import { Controller, useFieldArray, useFormContext } from 'react-hook-form'; import { InputControl } from '../ui/input-control'; import { Divider } from '../ui/divider'; import { Heading } from '../ui/heading'; import { Flex, FlexItem } from '../ui/flex'; import { Text } from '../ui/text'; import { Spacer } from '../ui/spacer'; import { PlusCircle, Trash2 } from 'lucide-react'; import { IValueSet, useValueSetServiceContext } from '../../services'; import { FhirCodeableConcept, FhirCode, FhirPeriod, FhirDuration } from '../index'; interface FhirTimingProps { context: string; label?: string; disabled?: boolean; } const FhirTiming: React.FC = ({ context, label, disabled = false }) => { const { control, watch, setValue } = useFormContext(); const { valueSetUrls, fetchValueSet, unitsOfTime, daysOfWeek } = useValueSetServiceContext(); const [timingAbbreviation, setTimingAbbreviation] = useState([]); const [eventTiming, setEventTiming] = useState([]); const spacerMargin = '16px'; const eventsFieldArray = useFieldArray({ control, name: `${context}.event` }); useEffect(() => { const loadValueSets = async () => { try { const [timingAbbreviationValueSet, eventTimingValueSet] = await Promise.all([ fetchValueSet(valueSetUrls.timingAbbreviation), fetchValueSet(valueSetUrls.eventTiming) ]); setTimingAbbreviation(timingAbbreviationValueSet); setEventTiming(eventTimingValueSet); } catch (error) { console.error('Error loading value sets for FhirTiming:', error); } }; loadValueSets(); }, [fetchValueSet, valueSetUrls]); const addEvent = () => { eventsFieldArray.append(''); }; const removeEvent = (index: number) => { eventsFieldArray.remove(index); }; const repeatType = watch(`${context}.repeat.repeatType`) || 'frequency'; const selectedDaysOfWeek = watch(`${context}.repeat.dayOfWeek`) || []; return ( <> {label && ( <> {label} )} {/* timingType */} {/* (*/} {/* {*/} {/* field.onChange(newValue.selectedItem.key);*/} {/* }}*/} {/* disabled={disabled}/>*/} {/* )}/>*/} {/**/} {/*{watch(`${context}.timingType`) === 'code' && (*/} {true && ( <> {/* code */} )} {watch(`${context}.timingType`) === 'timing' && ( <> {/* Events (dateTime) */} Events Specific dates/times when the event occurs {eventsFieldArray.fields.length === 0 && ( )} {eventsFieldArray.fields.map((field, index) => ( ( )}/> ))} {eventsFieldArray.fields.length > 0 && ( )} {/* Repeat */} Repeat Pattern {/* Repeat Type */} ( { field.onChange(newValue.selectedItem.key); }} disabled={disabled}/> )}/> {/* frequency timing */} {repeatType === 'frequency' && ( <> ( )}/> ( )}/> ( )}/> ( ( { name: option.display, key: option.code } ))} value={field.value} onChange={(newValue: { selectedItem: CustomSelectOption }) => { field.onChange(newValue.selectedItem.key); }} disabled={disabled}/> )}/> )} {/* boundsPeriod */} {repeatType === 'period' && ( )} {/* boundsDuration */} {repeatType === 'duration' && ( )} {/* count */} ( )}/> {/* countMax */} ( )}/> {/* Day of Week */} Days of Week {daysOfWeek.map((day) => ( { const currentDays = [...selectedDaysOfWeek]; if (checked) { if (!currentDays.includes(day.code)) { currentDays.push(day.code); } } else { const index = currentDays.indexOf(day.code); if (index !== -1) { currentDays.splice(index, 1); } } // A person's edit: marked dirty, so an editor knows the form has changes. setValue(`${context}.repeat.dayOfWeek`, currentDays, { shouldDirty: true }); }} disabled={disabled}/> ))} {/* Time of Day */} Time of Day { const timeValues = Array.isArray(field.value) ? field.value : []; return ( <> {timeValues.map((time, index) => ( { const updatedTimes = [...timeValues]; updatedTimes[index] = newTime; field.onChange(updatedTimes); }} disabled={disabled}/> ))} ); }} /> {/* When (time of day codes) */} When (Time of Day Codes) {/* when */} {/* Offset */} ( )}/> )} ); }; export default FhirTiming;