| 1 |
|
| 2 |
// Rest API load content in Tab Curriculum - Nhamdv. |
| 3 |
import { addQueryArgs } from '@wordpress/url'; |
| 4 |
import apiFetch from '@wordpress/api-fetch'; |
| 5 |
import scrollToItemCurrent from './scrolltoitem'; |
| 6 |
import { searchCourseContent } from './components/search'; |
| 7 |
|
| 8 |
export default function courseCurriculumSkeleton( courseID = '' ) { |
| 9 |
let isLoadingItems = false; |
| 10 |
let isLoadingSections = false; |
| 11 |
const Sekeleton = () => { |
| 12 |
const elementCurriculum = document.querySelector( '.learnpress-course-curriculum' ); |
| 13 |
|
| 14 |
if ( ! elementCurriculum ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
getResponse( elementCurriculum ); |
| 19 |
}; |
| 20 |
|
| 21 |
const getResponse = async ( ele ) => { |
| 22 |
const skeleton = ele.querySelector( '.lp-skeleton-animation' ); |
| 23 |
const itemID = ele.dataset.id; |
| 24 |
const sectionID = ele.dataset.section; |
| 25 |
|
| 26 |
try { |
| 27 |
const page = 1; |
| 28 |
const response = await apiFetch( { |
| 29 |
path: addQueryArgs( 'lp/v1/lazy-load/course-curriculum', { |
| 30 |
courseId: courseID || lpGlobalSettings.post_id || '', |
| 31 |
page, |
| 32 |
sectionID: sectionID || '', |
| 33 |
} ), |
| 34 |
method: 'GET', |
| 35 |
} ); |
| 36 |
|
| 37 |
const { data, status, message } = response; |
| 38 |
let section_ids = data.section_ids; |
| 39 |
|
| 40 |
if ( status === 'error' ) { |
| 41 |
throw new Error( message || 'Error' ); |
| 42 |
} |
| 43 |
|
| 44 |
let returnData = data.content; |
| 45 |
if ( undefined === returnData ) { // For old Eduma <= 4.6.0 |
| 46 |
returnData = data; |
| 47 |
section_ids = response.section_ids; |
| 48 |
} |
| 49 |
|
| 50 |
if ( sectionID ) { |
| 51 |
if ( section_ids && ! section_ids.includes( sectionID ) ) { |
| 52 |
const response2 = await getResponsive( '', page + 1, sectionID ); |
| 53 |
|
| 54 |
if ( response2 ) { |
| 55 |
const { data2, pages2, page2 } = response2; |
| 56 |
|
| 57 |
await parseContentItems( { ele, returnData, sectionID, itemID, data2, pages2, page2 } ); |
| 58 |
} |
| 59 |
} else { |
| 60 |
await parseContentItems( { ele, returnData, sectionID, itemID } ); |
| 61 |
} |
| 62 |
} else { |
| 63 |
returnData && ele.insertAdjacentHTML( 'beforeend', returnData ); |
| 64 |
} |
| 65 |
} catch ( error ) { |
| 66 |
ele.insertAdjacentHTML( 'beforeend', `<div class="lp-ajax-message error" style="display:block">${ error.message || 'Error: Query lp/v1/lazy-load/course-curriculum' }</div>` ); |
| 67 |
} |
| 68 |
|
| 69 |
skeleton && skeleton.remove(); |
| 70 |
|
| 71 |
searchCourseContent(); |
| 72 |
}; |
| 73 |
|
| 74 |
const parseContentItems = async ( { ele, returnData, sectionID, itemID, data2, pages2, page2 } ) => { |
| 75 |
const parser = new DOMParser(); |
| 76 |
const doc = parser.parseFromString( returnData, 'text/html' ); |
| 77 |
|
| 78 |
if ( data2 ) { |
| 79 |
const sections = doc.querySelector( '.curriculum-sections' ); |
| 80 |
|
| 81 |
const loadMoreBtn = doc.querySelector( '.curriculum-more__button' ); |
| 82 |
|
| 83 |
if ( loadMoreBtn ) { |
| 84 |
if ( pages2 <= page2 ) { |
| 85 |
loadMoreBtn.remove(); |
| 86 |
} else { |
| 87 |
loadMoreBtn.dataset.page = page2; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
sections.insertAdjacentHTML( 'beforeend', data2 ); |
| 92 |
} |
| 93 |
|
| 94 |
const section = doc.querySelector( `[data-section-id="${ sectionID }"]` ); |
| 95 |
|
| 96 |
if ( section ) { |
| 97 |
const items = section.querySelectorAll( '.course-item' ); |
| 98 |
const item_ids = [ ...items ].map( ( item ) => item.dataset.id ); |
| 99 |
const sectionContent = section.querySelector( '.section-content' ); |
| 100 |
const itemLoadMore = section.querySelector( '.section-item__loadmore' ); |
| 101 |
|
| 102 |
if ( itemID && ! item_ids.includes( itemID ) ) { |
| 103 |
const responseItem = await getResponsiveItem( '', 2, sectionID, itemID ); |
| 104 |
|
| 105 |
const { data3, pages3, paged3, page } = responseItem; |
| 106 |
|
| 107 |
if ( pages3 <= paged3 || pages3 <= page ) { |
| 108 |
itemLoadMore.remove(); |
| 109 |
} else { |
| 110 |
itemLoadMore.dataset.page = page; |
| 111 |
} |
| 112 |
|
| 113 |
if ( data3 && sectionContent ) { |
| 114 |
sectionContent.insertAdjacentHTML( 'beforeend', data3 ); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
ele.insertAdjacentHTML( 'beforeend', doc.body.innerHTML ); |
| 120 |
|
| 121 |
scrollToItemCurrent.init(); |
| 122 |
}; |
| 123 |
|
| 124 |
const getResponsiveItem = async ( returnData, paged, sectionID, itemID ) => { |
| 125 |
const response = await apiFetch( { |
| 126 |
path: addQueryArgs( 'lp/v1/lazy-load/course-curriculum-items', { |
| 127 |
sectionId: sectionID || '', |
| 128 |
page: paged, |
| 129 |
} ), |
| 130 |
method: 'GET', |
| 131 |
} ); |
| 132 |
|
| 133 |
const { data, status, pages, message } = response; |
| 134 |
|
| 135 |
const { page } = data; |
| 136 |
|
| 137 |
let item_ids; |
| 138 |
|
| 139 |
if ( status === 'success' ) { |
| 140 |
let dataTmp = data.content; |
| 141 |
item_ids = data.item_ids; |
| 142 |
|
| 143 |
if ( undefined === dataTmp ) { // For old Eduma <= 4.6.0 |
| 144 |
dataTmp = data; |
| 145 |
item_ids = response.item_ids; |
| 146 |
} |
| 147 |
|
| 148 |
returnData += dataTmp; |
| 149 |
|
| 150 |
if ( sectionID && item_ids && itemID && ! item_ids.includes( itemID ) ) { |
| 151 |
return getResponsiveItem( returnData, paged + 1, sectionID, itemID ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
isLoadingItems = false; |
| 156 |
|
| 157 |
return { data3: returnData, pages3: pages || data.pages, status3: status, message3: message, page: page || 0 }; |
| 158 |
}; |
| 159 |
|
| 160 |
const getResponsive = async ( returnData, page, sectionID ) => { |
| 161 |
const response = await apiFetch( { |
| 162 |
path: addQueryArgs( 'lp/v1/lazy-load/course-curriculum', { |
| 163 |
courseId: courseID || lpGlobalSettings.post_id || '', |
| 164 |
page, |
| 165 |
sectionID: sectionID || '', |
| 166 |
loadMore: true, |
| 167 |
} ), |
| 168 |
method: 'GET', |
| 169 |
} ); |
| 170 |
|
| 171 |
const { data, status, message } = response; |
| 172 |
|
| 173 |
let returnDataTmp = data.content; |
| 174 |
let section_ids = data.section_ids; |
| 175 |
let pages = data.pages; |
| 176 |
if ( undefined === returnDataTmp ) { // For old Eduma <= 4.6.0 |
| 177 |
returnDataTmp = data; |
| 178 |
section_ids = response.section_ids; |
| 179 |
pages = response.pages; |
| 180 |
} |
| 181 |
|
| 182 |
if ( status === 'success' ) { |
| 183 |
returnData += returnDataTmp; |
| 184 |
|
| 185 |
if ( sectionID && section_ids && section_ids.length > 0 && ! section_ids.includes( sectionID ) ) { |
| 186 |
return getResponsive( returnData, page + 1, sectionID ); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
isLoadingSections = false; |
| 191 |
|
| 192 |
return { data2: returnData, pages2: pages || data.pages, page2: page, status2: status, message2: message }; |
| 193 |
}; |
| 194 |
|
| 195 |
Sekeleton(); |
| 196 |
|
| 197 |
document.addEventListener( 'click', ( e ) => { |
| 198 |
const sectionBtns = document.querySelectorAll( '.section-item__loadmore' ); |
| 199 |
|
| 200 |
[ ...sectionBtns ].map( async ( sectionBtn ) => { |
| 201 |
if ( sectionBtn.contains( e.target ) && ! isLoadingItems ) { |
| 202 |
isLoadingItems = true; |
| 203 |
const sectionItem = sectionBtn.parentNode; |
| 204 |
const sectionId = sectionItem.getAttribute( 'data-section-id' ); |
| 205 |
const sectionContent = sectionItem.querySelector( '.section-content' ); |
| 206 |
|
| 207 |
const paged = parseInt( sectionBtn.dataset.page ); |
| 208 |
|
| 209 |
sectionBtn.classList.add( 'loading' ); |
| 210 |
|
| 211 |
try { |
| 212 |
const response = await getResponsiveItem( '', paged + 1, sectionId, '' ); |
| 213 |
|
| 214 |
const { data3, pages3, status3, message3 } = response; |
| 215 |
|
| 216 |
if ( status3 === 'error' ) { |
| 217 |
throw new Error( message3 || 'Error' ); |
| 218 |
} |
| 219 |
|
| 220 |
if ( pages3 <= paged + 1 ) { |
| 221 |
sectionBtn.remove(); |
| 222 |
} else { |
| 223 |
sectionBtn.dataset.page = paged + 1; |
| 224 |
} |
| 225 |
|
| 226 |
sectionContent.insertAdjacentHTML( 'beforeend', data3 ); |
| 227 |
} catch ( e ) { |
| 228 |
sectionContent.insertAdjacentHTML( 'beforeend', `<div class="lp-ajax-message error" style="display:block">${ e.message || 'Error: Query lp/v1/lazy-load/course-curriculum' }</div>` ); |
| 229 |
} |
| 230 |
|
| 231 |
sectionBtn.classList.remove( 'loading' ); |
| 232 |
|
| 233 |
searchCourseContent(); |
| 234 |
} |
| 235 |
} ); |
| 236 |
|
| 237 |
// Load more Sections |
| 238 |
const moreSections = document.querySelectorAll( '.curriculum-more__button' ); |
| 239 |
|
| 240 |
[ ...moreSections ].map( async ( moreSection ) => { |
| 241 |
if ( moreSection.contains( e.target ) && ! isLoadingSections ) { |
| 242 |
isLoadingSections = true; |
| 243 |
const paged = parseInt( moreSection.dataset.page ); |
| 244 |
|
| 245 |
const sections = moreSection.parentNode.parentNode.querySelector( '.curriculum-sections' ); |
| 246 |
|
| 247 |
if ( paged && sections ) { |
| 248 |
moreSection.classList.add( 'loading' ); |
| 249 |
|
| 250 |
try { |
| 251 |
const response2 = await getResponsive( '', paged + 1, '' ); |
| 252 |
|
| 253 |
const { data2, pages2, status2, message2 } = response2; |
| 254 |
|
| 255 |
if ( status2 === 'error' ) { |
| 256 |
throw new Error( message2 || 'Error' ); |
| 257 |
} |
| 258 |
|
| 259 |
if ( pages2 <= paged + 1 ) { |
| 260 |
moreSection.remove(); |
| 261 |
} else { |
| 262 |
moreSection.dataset.page = paged + 1; |
| 263 |
} |
| 264 |
|
| 265 |
sections.insertAdjacentHTML( 'beforeend', data2 ); |
| 266 |
} catch ( e ) { |
| 267 |
sections.insertAdjacentHTML( 'beforeend', `<div class="lp-ajax-message error" style="display:block">${ e.message || 'Error: Query lp/v1/lazy-load/course-curriculum' }</div>` ); |
| 268 |
} |
| 269 |
|
| 270 |
moreSection.classList.remove( 'loading' ); |
| 271 |
|
| 272 |
searchCourseContent(); |
| 273 |
} |
| 274 |
} |
| 275 |
} ); |
| 276 |
|
| 277 |
// Show/Hide accordion |
| 278 |
if ( document.querySelector( '.learnpress-course-curriculum' ) ) { |
| 279 |
const sections = document.querySelectorAll( '.section' ); |
| 280 |
|
| 281 |
[ ...sections ].map( ( section ) => { |
| 282 |
if ( section.contains( e.target ) ) { |
| 283 |
const toggle = section.querySelector( '.section-left' ); |
| 284 |
|
| 285 |
toggle.contains( e.target ) && section.classList.toggle( 'closed' ); |
| 286 |
} |
| 287 |
} ); |
| 288 |
} |
| 289 |
} ); |
| 290 |
} |
| 291 |
|