1- import React , { useState } from 'react' ;
1+ import React from 'react'
22import { Stack } from "@fluentui/react"
3+ import { AppStateContext } from '../../state/AppProvider'
34import { sectionGenerate , SectionGenerateRequest } from '../../api' ;
45import { Section } from '../../api/models'
56import { Spinner } from "@fluentui/react" ;
@@ -10,9 +11,7 @@ import { Textarea, makeStyles, Text, Popover, PopoverSurface, PopoverTrigger, Bu
1011
1112
1213interface SectionCardProps {
13- section : Section ;
14- onValueChange : ( key : number , value : string ) => void ;
15- index : number ;
14+ sectionIdx : number
1615}
1716
1817const useStyles = makeStyles ( {
@@ -83,34 +82,41 @@ const useStyles = makeStyles({
8382
8483} ) ;
8584
86- const SectionCard : React . FC < SectionCardProps > = ( { section , onValueChange , index } ) => {
85+ const SectionCard = ( { sectionIdx } : SectionCardProps ) => {
8786 const classes = useStyles ( )
8887 const [ isLoading , setIsLoading ] = React . useState ( false )
8988 const [ isPopoverOpen , setIsPopoverOpen ] = React . useState ( false )
90- const [ textareaValue , setTextareaValue ] = useState ( section . content ) ;
89+ const appStateContext = React . useContext ( AppStateContext )
90+
91+ if ( ! appStateContext ) { throw new Error ( 'useAppState must be used within a AppStateProvider' ) }
92+
93+ const section = appStateContext . state . draftedDocument ?. sections [ sectionIdx ]
94+ if ( ! section ) { throw new Error ( 'Section not found' ) }
9195
9296 const sectionTitle = section . title
9397 const sectionDescription = section . description
9498 const sectionContent = section . content
9599
96- const handleChange = ( event : React . ChangeEvent < HTMLTextAreaElement > ) => {
97- onValueChange ( index , event . target . value ) ;
98- setTextareaValue ( event . target . value ) ;
99- } ;
100-
101100 const handleOpenChange : PopoverProps [ "onOpenChange" ] = ( e , data ) => setIsPopoverOpen ( data . open || false ) ;
102101
103102 async function fetchSectionContent ( sectionTitle : string , sectionDescription : string ) {
104103 setIsLoading ( true )
105104 const sectionGenerateRequest : SectionGenerateRequest = { sectionTitle, sectionDescription }
105+
106106 const response = await sectionGenerate ( sectionGenerateRequest )
107107 const responseBody = await response . json ( )
108- setTextareaValue ( responseBody . section_content )
108+
109+ const updatedSection : Section = { title : sectionTitle , description : sectionDescription , content : responseBody . section_content }
110+ appStateContext ?. dispatch ( { type : 'UPDATE_SECTION' , payload : { sectionIdx : sectionIdx , section : updatedSection } } )
111+
109112 setIsLoading ( false )
110113 }
111114
115+ if ( sectionContent === '' && ! isLoading ) { fetchSectionContent ( sectionTitle , sectionDescription ) }
116+
112117 return (
113118 < Stack
119+ // add margin bottom unless it's the last section
114120 style = { { marginBottom : '1rem' } }
115121 >
116122 < Stack horizontal horizontalAlign = "space-between" verticalAlign = "center" style = { { marginBottom : '1rem' } } >
@@ -171,8 +177,12 @@ const SectionCard: React.FC<SectionCardProps> = ({ section, onValueChange, index
171177 appearance = "outline"
172178 size = "large"
173179 defaultValue = { sectionContent }
174- value = { textareaValue }
175- onChange = { handleChange }
180+
181+ onChange = { ( e , data ) => {
182+ const updatedSection : Section = { title : sectionTitle , description : sectionDescription , content : data . value || '' }
183+ appStateContext ?. dispatch ( { type : 'UPDATE_SECTION' , payload : { sectionIdx : sectionIdx , section : updatedSection } } )
184+ } }
185+
176186 textarea = { { className : classes . sectionContentTextarea } }
177187 style = { { width : '100%' , height : '100%' } }
178188 />
@@ -183,4 +193,4 @@ const SectionCard: React.FC<SectionCardProps> = ({ section, onValueChange, index
183193 )
184194}
185195
186- export default SectionCard
196+ export default SectionCard
0 commit comments