useBsfLearn.js
181 lines
| 1 | import { useState, useEffect, useMemo, useCallback } from 'react'; |
| 2 | import apiFetch from '@wordpress/api-fetch'; |
| 3 | import { toast } from '@bsf/force-ui'; |
| 4 | import { __ } from '@wordpress/i18n'; |
| 5 | |
| 6 | /** |
| 7 | * Custom hook for managing Learn chapters and steps |
| 8 | * |
| 9 | * @param {Object} config - Configuration object |
| 10 | * @param {Array} config.initialChapters - Array of chapter objects with steps |
| 11 | * @param {string} config.saveEndpoint - API endpoint to save progress (optional) |
| 12 | * @returns {Object} - Object containing chapters state and utility functions |
| 13 | */ |
| 14 | const useBsfLearn = ( { |
| 15 | initialChapters = [], |
| 16 | saveEndpoint = null, |
| 17 | } = {} ) => { |
| 18 | const [ chapters, setChapters ] = useState( initialChapters ); |
| 19 | const [ learnHowDialogOpen, setLearnHowDialogOpen ] = useState( false ); |
| 20 | const [ currentLearnHowItem, setCurrentLearnHowItem ] = useState( null ); |
| 21 | |
| 22 | // Update chapters when initialChapters changes (e.g., when API data loads) |
| 23 | useEffect( () => { |
| 24 | if ( initialChapters.length > 0 ) { |
| 25 | setChapters( initialChapters ); |
| 26 | } |
| 27 | }, [ initialChapters ] ); |
| 28 | |
| 29 | /** |
| 30 | * Update completion status of a specific step |
| 31 | */ |
| 32 | const updateStepCompletion = useCallback( |
| 33 | ( chapterId, stepId, completed ) => { |
| 34 | // Optimistically update UI |
| 35 | setChapters( ( prevChapters ) => |
| 36 | prevChapters.map( ( chapter ) => |
| 37 | chapter.id === chapterId |
| 38 | ? { |
| 39 | ...chapter, |
| 40 | steps: chapter.steps.map( ( step ) => |
| 41 | step.id === stepId ? { ...step, completed } : step |
| 42 | ), |
| 43 | } |
| 44 | : chapter |
| 45 | ) |
| 46 | ); |
| 47 | |
| 48 | // Save to API if endpoint is provided |
| 49 | if ( saveEndpoint ) { |
| 50 | apiFetch( { |
| 51 | path: saveEndpoint, |
| 52 | method: 'POST', |
| 53 | data: { |
| 54 | chapterId, |
| 55 | stepId, |
| 56 | completed, |
| 57 | }, |
| 58 | } ).catch( ( error ) => { |
| 59 | // Revert UI state on error |
| 60 | setChapters( ( prevChapters ) => |
| 61 | prevChapters.map( ( chapter ) => |
| 62 | chapter.id === chapterId |
| 63 | ? { |
| 64 | ...chapter, |
| 65 | steps: chapter.steps.map( ( step ) => |
| 66 | step.id === stepId ? { ...step, completed: ! completed } : step |
| 67 | ), |
| 68 | } |
| 69 | : chapter |
| 70 | ) |
| 71 | ); |
| 72 | |
| 73 | toast.error( __( 'Failed to save progress. Please try again.', 'presto-player' ) ); |
| 74 | console.error( 'Failed to save progress:', error ); |
| 75 | } ); |
| 76 | } |
| 77 | }, |
| 78 | [ saveEndpoint ] |
| 79 | ); |
| 80 | |
| 81 | const markStepCompleted = useCallback( |
| 82 | ( chapterId, stepId ) => { |
| 83 | updateStepCompletion( chapterId, stepId, true ); |
| 84 | }, |
| 85 | [ updateStepCompletion ] |
| 86 | ); |
| 87 | |
| 88 | const markStepIncomplete = useCallback( |
| 89 | ( chapterId, stepId ) => { |
| 90 | updateStepCompletion( chapterId, stepId, false ); |
| 91 | }, |
| 92 | [ updateStepCompletion ] |
| 93 | ); |
| 94 | |
| 95 | const resetProgress = useCallback( () => { |
| 96 | setChapters( ( prevChapters ) => |
| 97 | prevChapters.map( ( chapter ) => ( { |
| 98 | ...chapter, |
| 99 | steps: chapter.steps.map( ( step ) => ( { ...step, completed: false } ) ), |
| 100 | } ) ) |
| 101 | ); |
| 102 | }, [] ); |
| 103 | |
| 104 | const firstIncompleteChapterId = useMemo( () => { |
| 105 | const incompleteChapter = chapters.find( |
| 106 | ( chapter ) => |
| 107 | chapter.steps.length !== |
| 108 | chapter.steps.filter( ( step ) => step.completed ).length |
| 109 | ); |
| 110 | return incompleteChapter?.id; |
| 111 | }, [ chapters ] ); |
| 112 | |
| 113 | const progressStats = useMemo( () => { |
| 114 | const totalSteps = chapters.reduce( ( sum, chapter ) => sum + chapter.steps.length, 0 ); |
| 115 | const completedSteps = chapters.reduce( |
| 116 | ( sum, chapter ) => |
| 117 | sum + chapter.steps.filter( ( step ) => step.completed ).length, |
| 118 | 0 |
| 119 | ); |
| 120 | const completionPercentage = |
| 121 | totalSteps > 0 ? Math.round( ( completedSteps / totalSteps ) * 100 ) : 0; |
| 122 | |
| 123 | return { |
| 124 | totalChapters: chapters.length, |
| 125 | totalSteps, |
| 126 | completedSteps, |
| 127 | completionPercentage, |
| 128 | isFullyCompleted: totalSteps > 0 && completedSteps === totalSteps, |
| 129 | }; |
| 130 | }, [ chapters ] ); |
| 131 | |
| 132 | const getChapterStats = useCallback( |
| 133 | ( chapterId ) => { |
| 134 | const chapter = chapters.find( ( c ) => c.id === chapterId ); |
| 135 | if ( ! chapter ) { |
| 136 | return null; |
| 137 | } |
| 138 | |
| 139 | const totalSteps = chapter.steps.length; |
| 140 | const completedSteps = chapter.steps.filter( ( step ) => step.completed ).length; |
| 141 | |
| 142 | return { |
| 143 | totalSteps, |
| 144 | completedSteps, |
| 145 | isCompleted: totalSteps > 0 && completedSteps === totalSteps, |
| 146 | completionPercentage: |
| 147 | totalSteps > 0 ? Math.round( ( completedSteps / totalSteps ) * 100 ) : 0, |
| 148 | }; |
| 149 | }, |
| 150 | [ chapters ] |
| 151 | ); |
| 152 | |
| 153 | const openLearnHowDialog = useCallback( ( item ) => { |
| 154 | setCurrentLearnHowItem( item ); |
| 155 | setLearnHowDialogOpen( true ); |
| 156 | }, [] ); |
| 157 | |
| 158 | const closeLearnHowDialog = useCallback( () => { |
| 159 | setLearnHowDialogOpen( false ); |
| 160 | setCurrentLearnHowItem( null ); |
| 161 | }, [] ); |
| 162 | |
| 163 | return { |
| 164 | chapters, |
| 165 | updateStepCompletion, |
| 166 | markStepCompleted, |
| 167 | markStepIncomplete, |
| 168 | resetProgress, |
| 169 | firstIncompleteChapterId, |
| 170 | progressStats, |
| 171 | getChapterStats, |
| 172 | learnHowDialogOpen, |
| 173 | currentLearnHowItem, |
| 174 | openLearnHowDialog, |
| 175 | closeLearnHowDialog, |
| 176 | setLearnHowDialogOpen, |
| 177 | }; |
| 178 | }; |
| 179 | |
| 180 | export default useBsfLearn; |
| 181 |