1+ import { debounce } from 'lodash'
12import {
23 Dispatch ,
34 FC ,
@@ -38,14 +39,15 @@ import {
3839 UserCertificationProgressStatus ,
3940 userCertificationProgressUpdateAsync ,
4041 UserCertificationUpdateProgressActions ,
42+ useShowSurvey ,
4143} from '../learn-lib'
4244import { getCertificationCompletedPath , getCoursePath , getLessonPathFromModule } from '../learn.routes'
4345
4446import { FccFrame } from './fcc-frame'
4547import { FccSidebar } from './fcc-sidebar'
4648import { TitleNav } from './title-nav'
4749import styles from './FreeCodeCamp.module.scss'
48- import { debounce } from 'lodash '
50+ import { LearnConfig } from '../learn-config '
4951
5052const FreeCodeCamp : FC < { } > = ( ) => {
5153
@@ -66,6 +68,11 @@ const FreeCodeCamp: FC<{}> = () => {
6668 const [ lessonParam , setLessonParam ] : [ string , Dispatch < SetStateAction < string > > ]
6769 = useState ( textFormatGetSafeString ( routeParams . lesson ) )
6870
71+ const [ showSurvey , setShowSurvey ] : [
72+ string ,
73+ Dispatch < SetStateAction < string > >
74+ ] = useShowSurvey ( )
75+
6976 const {
7077 certificationProgress : certificateProgress ,
7178 setCertificateProgress,
@@ -212,6 +219,7 @@ const FreeCodeCamp: FC<{}> = () => {
212219 profile ?. userId ,
213220 ] )
214221
222+ // eslint-disable-next-line react-hooks/exhaustive-deps
215223 const handleFccLessonComplete : ( challengeUuid : string ) => void = useCallback ( debounce ( ( challengeUuid : string ) => {
216224
217225 const currentLesson : { [ key : string ] : string } = {
@@ -234,7 +242,6 @@ const FreeCodeCamp: FC<{}> = () => {
234242 currentLesson ,
235243 )
236244 . then ( ( progress : LearnUserCertificationProgress ) => {
237-
238245 setCertificateProgress ( progress )
239246 handleSurvey ( certWasInProgress , progress )
240247
@@ -274,30 +281,16 @@ const FreeCodeCamp: FC<{}> = () => {
274281
275282 // This is the last lesson to be completed in the first module completed,
276283 // so it's time to trigger the survey
277- const surveyTrigger : string = 'TCA First Module Completed'
278-
279- // If there is only one assessment in a cert (e.g. Data Analysis w/Python),
280- // the cert is also completed, which redirects the user to the cert page.
281- // So the survey needs to be delayed so that it appears on the completed
282- // cert page instead of the current lesson.
283-
284- // NOTE: we can't use the cert's status here bc it doesn't get set to
285- // completed until the UI notices the cert is complete and initiates
286- // the completion. And we have to use >= instead of === because it's
287- // possible TCA data isn't in sync w/the latest FCC curriculum.
288- if ( progress . certificationProgressPercentage >= 100 ) {
289- setTimeout ( async ( ) => {
290- surveyTriggerForUser ( surveyTrigger , profile ?. userId )
291- } , 1000 )
292- } else {
293- surveyTriggerForUser ( surveyTrigger , profile ?. userId )
294- }
284+ // NOTE: We have to add a delay, otherwise the survey closes when the user
285+ // is automatically redirected to the next lesson.
286+ setShowSurvey ( certificationParam )
295287 }
296288
297289 /**
298290 * Handle the navigation away from the last step of the course in the FCC frame
299291 * @returns
300292 */
293+ // eslint-disable-next-line react-hooks/exhaustive-deps
301294 const handleFccLastLessonNavigation : ( ) => void = useCallback ( debounce ( ( ) => {
302295
303296 if ( ! certificateProgress ) {
@@ -461,6 +454,17 @@ const FreeCodeCamp: FC<{}> = () => {
461454 isLoggedIn ,
462455 ] )
463456
457+ useEffect ( ( ) => {
458+ if ( ready && showSurvey === certificationParam ) {
459+ surveyTriggerForUser ( LearnConfig . SURVEY . COMPLETED_FIRST_MODULE , profile ?. userId )
460+ setShowSurvey ( '' )
461+ }
462+ } , [
463+ ready ,
464+ showSurvey ,
465+ certificationParam ,
466+ ] ) ;
467+
464468 return (
465469 < >
466470 < LoadingSpinner hide = { ready } />
0 commit comments