@@ -138,32 +138,72 @@ const SectionCard = ({ sectionIdx }: SectionCardProps) => {
138138 setCharCount ( sectionContent . length )
139139 } , [ location ] )
140140
141+
142+ useEffect ( ( ) => {
143+ if ( appStateContext . state ?. failedSections . length > 0 && appStateContext . state ?. failedSections [ 0 ] . title === sectionTitle && isLoading && ! appStateContext . state . isFailedReqInitiated ) {
144+ console . log ( "appStateContext.state?.failedSections" , appStateContext . state ?. failedSections ) ;
145+ const tempItem = {
146+ title : sectionTitle ,
147+ description : sectionDescription ,
148+ content : sectionContent
149+ }
150+ appStateContext ?. dispatch ( { type : 'REMOVED_FAILED_SECTION' , payload : { section : tempItem } } )
151+ appStateContext ?. dispatch ( { type : 'UPDATE_SECTION_API_REQ_STATUS' , payload : true } )
152+ fetchSectionContent ( sectionTitle , sectionDescription , 'failed' ) ;
153+ }
154+ } , [ appStateContext . state . failedSections ] ) ;
155+
141156 const handleOpenChange : PopoverProps [ 'onOpenChange' ] = ( e , data ) => setIsPopoverOpen ( data . open || false )
142157
143- async function fetchSectionContent ( sectionTitle : string , sectionDescription : string ) {
158+ async function fetchSectionContent ( sectionTitle : string , sectionDescription : string , isReqFrom = '' ) {
144159 setIsLoading ( true )
145160 const sectionGenerateRequest : SectionGenerateRequest = { sectionTitle, sectionDescription }
146161
147162 const response = await sectionGenerate ( sectionGenerateRequest )
148163 const responseBody = await response . json ( )
149164
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 || ''
165+ if ( responseBody ?. error ?. includes ( "429" ) ) {
166+ console . log ( "retriggerd !!!" )
167+ const failedSectionItems = {
168+ title : sectionTitle ,
169+ description : sectionDescription ,
170+ content : sectionContent
171+ }
172+ appStateContext ?. dispatch ( { type : 'ADD_FAILED_SECTION' , payload : failedSectionItems } )
173+ if ( isReqFrom == 'failed' )
174+ appStateContext ?. dispatch ( { type : 'UPDATE_SECTION_API_REQ_STATUS' , payload : false } )
175+
176+ setTimeout ( ( ) => {
177+ } , 5000 )
178+
179+ } else {
180+ const updatedSection : Section = {
181+ title : sectionTitle ,
182+ description : sectionDescription ,
183+ content : responseBody . section_content
184+ }
185+ appStateContext ?. dispatch ( { type : 'UPDATE_SECTION' , payload : { sectionIdx : sectionIdx , section : updatedSection } } )
186+ let content = updatedSection . content || ''
187+
188+ // limit the character count to 2000
189+ if ( content . length > sectionCharacterLimit ) {
190+ content = content . slice ( 0 , sectionCharacterLimit )
191+ }
192+
193+ setCharCount ( content . length )
194+ setIsLoading ( false )
195+
196+ appStateContext ?. dispatch ( { type : 'REMOVED_FAILED_SECTION' , payload : { section : updatedSection } } )
157197
158- // limit the character count to 2000
159- if ( content . length > sectionCharacterLimit ) {
160- content = content . slice ( 0 , sectionCharacterLimit )
198+ if ( isReqFrom == 'failed' )
199+ appStateContext ?. dispatch ( { type : 'UPDATE_SECTION_API_REQ_STATUS' , payload : false } )
161200 }
162201
163- setCharCount ( content . length )
164- setIsLoading ( false )
202+
165203 }
166204
205+
206+
167207 useEffect ( ( ) => {
168208 if ( sectionContent === '' && ! isLoading && ! isManuallyCleared ) {
169209 fetchSectionContent ( sectionTitle , sectionDescription )
0 commit comments