@@ -138,32 +138,79 @@ const SectionCard = ({ sectionIdx }: SectionCardProps) => {
138138 setCharCount ( sectionContent . length )
139139 } , [ location ] )
140140
141+
142+ useEffect ( ( ) => {
143+ // if (appStateContext.state.failedSections.some((item) => item.title === sectionTitle) && isLoading) {
144+ if ( appStateContext . state ?. failedSections . length > 0 && appStateContext . state ?. failedSections [ 0 ] . title === sectionTitle && isLoading && ! appStateContext . state . isFailedReqInitiated ) {
145+ console . log ( "appStateContext.state?.failedSections" , appStateContext . state ?. failedSections ) ;
146+ const tempItem = {
147+ title : sectionTitle ,
148+ description : sectionDescription ,
149+ content : sectionContent
150+ }
151+ //setTimeout(()=>{
152+ appStateContext ?. dispatch ( { type : 'REMOVED_FAILED_SECTION' , payload : { section : tempItem } } )
153+ appStateContext ?. dispatch ( { type : 'UPDATE_SECTION_API_REQ_STATUS' , payload : true } )
154+ fetchSectionContent ( sectionTitle , sectionDescription , 'failed' ) ;
155+ // },10000)
156+
157+ }
158+ } , [ appStateContext . state . failedSections ] ) ;
159+
141160 const handleOpenChange : PopoverProps [ 'onOpenChange' ] = ( e , data ) => setIsPopoverOpen ( data . open || false )
142161
143- async function fetchSectionContent ( sectionTitle : string , sectionDescription : string ) {
162+ async function fetchSectionContent ( sectionTitle : string , sectionDescription : string , isReqFrom = '' ) {
144163 setIsLoading ( true )
145164 const sectionGenerateRequest : SectionGenerateRequest = { sectionTitle, sectionDescription }
146165
147166 const response = await sectionGenerate ( sectionGenerateRequest )
148167 const responseBody = await response . json ( )
149168
150- const updatedSection : Section = {
151- title : sectionTitle ,
152- description : sectionDescription ,
153- content : responseBody . section_content
154- }
155- appStateContext ?. dispatch ( { type : 'UPDATE_SECTION' , payload : { sectionIdx : sectionIdx , section : updatedSection } } )
156- let content = updatedSection . content || ''
169+ if ( responseBody ?. error ?. includes ( "429" ) ) {
170+ console . log ( "retriggerd !!!" )
171+ const failedSectionItems = {
172+ title : sectionTitle ,
173+ description : sectionDescription ,
174+ content : sectionContent
175+ }
176+ appStateContext ?. dispatch ( { type : 'ADD_FAILED_SECTION' , payload : failedSectionItems } )
177+ if ( isReqFrom == 'failed' )
178+ appStateContext ?. dispatch ( { type : 'UPDATE_SECTION_API_REQ_STATUS' , payload : false } )
179+
180+ setTimeout ( ( ) => {
181+ // fetchSectionContent(sectionTitle,sectionDescription)
182+ } , 5000 )
183+
184+ } else {
185+ const updatedSection : Section = {
186+ title : sectionTitle ,
187+ description : sectionDescription ,
188+ content : responseBody . section_content
189+ }
190+ appStateContext ?. dispatch ( { type : 'UPDATE_SECTION' , payload : { sectionIdx : sectionIdx , section : updatedSection } } )
191+ let content = updatedSection . content || ''
192+
193+ // limit the character count to 2000
194+ if ( content . length > sectionCharacterLimit ) {
195+ content = content . slice ( 0 , sectionCharacterLimit )
196+ }
197+
198+ setCharCount ( content . length )
199+ setIsLoading ( false )
157200
158- // limit the character count to 2000
159- if ( content . length > sectionCharacterLimit ) {
160- content = content . slice ( 0 , sectionCharacterLimit )
201+ //appStateContext?.dispatch({ type: 'REMOVED_FAILED_SECTION', payload: updatedSection })
202+
203+ appStateContext ?. dispatch ( { type : 'REMOVED_FAILED_SECTION' , payload : { section : updatedSection } } )
204+
205+ if ( isReqFrom == 'failed' )
206+ appStateContext ?. dispatch ( { type : 'UPDATE_SECTION_API_REQ_STATUS' , payload : false } )
161207 }
162208
163- setCharCount ( content . length )
164- setIsLoading ( false )
209+
165210 }
166211
212+
213+
167214 useEffect ( ( ) => {
168215 if ( sectionContent === '' && ! isLoading && ! isManuallyCleared ) {
169216 fetchSectionContent ( sectionTitle , sectionDescription )
0 commit comments