| 1 |
// Rest API load content in Tab Curriculum - Nhamdv. |
| 2 |
import { addQueryArgs } from '@wordpress/url'; |
| 3 |
//import apiFetch from '@wordpress/api-fetch'; |
| 4 |
import scrollToItemCurrent from './scrolltoitem'; |
| 5 |
import { searchCourseContent } from './components/search'; |
| 6 |
|
| 7 |
export default function courseCurriculumSkeleton( courseID = '' ) { |
| 8 |
let idItemViewing = 0; |
| 9 |
const elItemViewing = document.querySelector( '.viewing-course-item' ); |
| 10 |
if ( elItemViewing ) { |
| 11 |
const regex = new RegExp( '^viewing-course-item-([0-9].*)' ); |
| 12 |
const classList = elItemViewing.classList; |
| 13 |
|
| 14 |
classList.forEach( function( className ) { |
| 15 |
const compare = regex.exec( className ); |
| 16 |
|
| 17 |
if ( compare ) { |
| 18 |
idItemViewing = compare[ 1 ]; |
| 19 |
return false; |
| 20 |
} |
| 21 |
} ); |
| 22 |
} |
| 23 |
|
| 24 |
let isLoadingItems = false; |
| 25 |
let isLoadingSections = false; |
| 26 |
const Sekeleton = () => { |
| 27 |
const elementCurriculum = document.querySelector( |
| 28 |
'.learnpress-course-curriculum' |
| 29 |
); |
| 30 |
|
| 31 |
if ( ! elementCurriculum ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
getResponse( elementCurriculum ); |
| 36 |
}; |
| 37 |
|
| 38 |
const getResponse = async ( ele ) => { |
| 39 |
const skeleton = ele.querySelector( '.lp-skeleton-animation' ); |
| 40 |
const itemID = ele.dataset.id; |
| 41 |
const sectionID = ele.dataset.section; |
| 42 |
|
| 43 |
try { |
| 44 |
const page = 1; |
| 45 |
let url = lpData.lp_rest_url + 'lp/v1/lazy-load/course-curriculum/'; |
| 46 |
url = addQueryArgs( url, { |
| 47 |
courseId: courseID || lpGlobalSettings.post_id || '', |
| 48 |
page, |
| 49 |
sectionID: sectionID || '', |
| 50 |
idItemViewing, |
| 51 |
} ); |
| 52 |
let paramsFetch = {}; |
| 53 |
if ( 0 !== parseInt( lpData.user_id ) ) { |
| 54 |
paramsFetch = { |
| 55 |
headers: { |
| 56 |
'X-WP-Nonce': lpData.nonce, |
| 57 |
}, |
| 58 |
}; |
| 59 |
} |
| 60 |
|
| 61 |
let response = await fetch( url, { method: 'GET', ...paramsFetch } ); |
| 62 |
response = await response.json(); |
| 63 |
|
| 64 |
const { data, status, message } = response; |
| 65 |
const section_ids = data.section_ids; |
| 66 |
|
| 67 |
if ( status === 'error' ) { |
| 68 |
throw new Error( message || 'Error' ); |
| 69 |
} |
| 70 |
|
| 71 |
const returnData = data.content; |
| 72 |
|
| 73 |
if ( sectionID ) { |
| 74 |
if ( section_ids && ! section_ids.includes( sectionID ) ) { |
| 75 |
const response2 = await getResponsive( |
| 76 |
'', |
| 77 |
page + 1, |
| 78 |
sectionID |
| 79 |
); |
| 80 |
|
| 81 |
if ( response2 ) { |
| 82 |
const { data2, pages2, page2 } = response2; |
| 83 |
|
| 84 |
await parseContentItems( { |
| 85 |
ele, |
| 86 |
returnData, |
| 87 |
sectionID, |
| 88 |
itemID, |
| 89 |
data2, |
| 90 |
pages2, |
| 91 |
page2, |
| 92 |
} ); |
| 93 |
} |
| 94 |
} else { |
| 95 |
await parseContentItems( { |
| 96 |
ele, |
| 97 |
returnData, |
| 98 |
sectionID, |
| 99 |
itemID, |
| 100 |
} ); |
| 101 |
} |
| 102 |
} else { |
| 103 |
returnData && ele.insertAdjacentHTML( 'beforeend', returnData ); |
| 104 |
} |
| 105 |
} catch ( error ) { |
| 106 |
ele.insertAdjacentHTML( |
| 107 |
'beforeend', |
| 108 |
`<div class="lp-ajax-message error" style="display:block">${ |
| 109 |
error.message || |
| 110 |
'Error: Query lp/v1/lazy-load/course-curriculum' |
| 111 |
}</div>` |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
skeleton && skeleton.remove(); |
| 116 |
|
| 117 |
searchCourseContent(); |
| 118 |
}; |
| 119 |
|
| 120 |
const parseContentItems = async ( { |
| 121 |
ele, |
| 122 |
returnData, |
| 123 |
sectionID, |
| 124 |
itemID, |
| 125 |
data2, |
| 126 |
pages2, |
| 127 |
page2, |
| 128 |
} ) => { |
| 129 |
const parser = new DOMParser(); |
| 130 |
const doc = parser.parseFromString( returnData, 'text/html' ); |
| 131 |
|
| 132 |
if ( data2 ) { |
| 133 |
const sections = doc.querySelector( '.curriculum-sections' ); |
| 134 |
|
| 135 |
const loadMoreBtn = doc.querySelector( '.curriculum-more__button' ); |
| 136 |
|
| 137 |
if ( loadMoreBtn ) { |
| 138 |
if ( pages2 <= page2 ) { |
| 139 |
loadMoreBtn.remove(); |
| 140 |
} else { |
| 141 |
loadMoreBtn.dataset.page = page2; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
sections.insertAdjacentHTML( 'beforeend', data2 ); |
| 146 |
} |
| 147 |
|
| 148 |
const section = doc.querySelector( `[data-section-id="${ sectionID }"]` ); |
| 149 |
|
| 150 |
if ( section ) { |
| 151 |
const items = section.querySelectorAll( '.course-item' ); |
| 152 |
const item_ids = [ ...items ].map( ( item ) => item.dataset.id ); |
| 153 |
const sectionContent = section.querySelector( '.section-content' ); |
| 154 |
const itemLoadMore = section.querySelector( |
| 155 |
'.section-item__loadmore' |
| 156 |
); |
| 157 |
|
| 158 |
if ( itemID && ! item_ids.includes( itemID ) ) { |
| 159 |
const responseItem = await getResponsiveItem( |
| 160 |
'', |
| 161 |
2, |
| 162 |
sectionID, |
| 163 |
itemID |
| 164 |
); |
| 165 |
|
| 166 |
const { data3, pages3, paged3, page } = responseItem; |
| 167 |
|
| 168 |
if ( pages3 <= paged3 || pages3 <= page ) { |
| 169 |
itemLoadMore.remove(); |
| 170 |
} else { |
| 171 |
itemLoadMore.dataset.page = page; |
| 172 |
} |
| 173 |
|
| 174 |
if ( data3 && sectionContent ) { |
| 175 |
sectionContent.insertAdjacentHTML( 'beforeend', data3 ); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
ele.insertAdjacentHTML( 'beforeend', doc.body.innerHTML ); |
| 181 |
|
| 182 |
scrollToItemCurrent.init(); |
| 183 |
}; |
| 184 |
|
| 185 |
const getResponsiveItem = async ( returnData, paged, sectionID, itemID ) => { |
| 186 |
let url = lpData.lp_rest_url + 'lp/v1/lazy-load/course-curriculum-items/'; |
| 187 |
url = addQueryArgs( url, { |
| 188 |
sectionId: sectionID || '', |
| 189 |
page: paged, |
| 190 |
} ); |
| 191 |
let paramsFetch = {}; |
| 192 |
if ( 0 !== parseInt( lpData.user_id ) ) { |
| 193 |
paramsFetch = { |
| 194 |
headers: { |
| 195 |
'X-WP-Nonce': lpData.nonce, |
| 196 |
}, |
| 197 |
}; |
| 198 |
} |
| 199 |
|
| 200 |
let response = await fetch( url, { method: 'GET', ...paramsFetch } ); |
| 201 |
response = await response.json(); |
| 202 |
|
| 203 |
const { data, status, pages, message } = response; |
| 204 |
|
| 205 |
const { page } = data; |
| 206 |
|
| 207 |
let item_ids; |
| 208 |
|
| 209 |
if ( status === 'success' ) { |
| 210 |
const dataTmp = data.content; |
| 211 |
item_ids = data.item_ids; |
| 212 |
|
| 213 |
returnData += dataTmp; |
| 214 |
|
| 215 |
if ( sectionID && item_ids && itemID && ! item_ids.includes( itemID ) ) { |
| 216 |
return getResponsiveItem( |
| 217 |
returnData, |
| 218 |
paged + 1, |
| 219 |
sectionID, |
| 220 |
itemID |
| 221 |
); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
isLoadingItems = false; |
| 226 |
|
| 227 |
return { |
| 228 |
data3: returnData, |
| 229 |
pages3: pages || data.pages, |
| 230 |
status3: status, |
| 231 |
message3: message, |
| 232 |
page: page || 0, |
| 233 |
}; |
| 234 |
}; |
| 235 |
|
| 236 |
const getResponsive = async ( returnData, page, sectionID ) => { |
| 237 |
let url = lpData.lp_rest_url + 'lp/v1/lazy-load/course-curriculum/'; |
| 238 |
url = addQueryArgs( url, { |
| 239 |
courseId: courseID || lpGlobalSettings.post_id || '', |
| 240 |
page, |
| 241 |
sectionID: sectionID || '', |
| 242 |
loadMore: true, |
| 243 |
} ); |
| 244 |
let paramsFetch = {}; |
| 245 |
if ( 0 !== parseInt( lpData.user_id ) ) { |
| 246 |
paramsFetch = { |
| 247 |
headers: { |
| 248 |
'X-WP-Nonce': lpData.nonce, |
| 249 |
}, |
| 250 |
}; |
| 251 |
} |
| 252 |
|
| 253 |
let response = await fetch( url, { method: 'GET', ...paramsFetch } ); |
| 254 |
response = await response.json(); |
| 255 |
|
| 256 |
const { data, status, message } = response; |
| 257 |
|
| 258 |
const returnDataTmp = data.content; |
| 259 |
const section_ids = data.section_ids; |
| 260 |
const pages = data.pages; |
| 261 |
|
| 262 |
if ( status === 'success' ) { |
| 263 |
returnData += returnDataTmp; |
| 264 |
|
| 265 |
if ( |
| 266 |
sectionID && |
| 267 |
section_ids && |
| 268 |
section_ids.length > 0 && |
| 269 |
! section_ids.includes( sectionID ) |
| 270 |
) { |
| 271 |
return getResponsive( returnData, page + 1, sectionID ); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
isLoadingSections = false; |
| 276 |
|
| 277 |
return { |
| 278 |
data2: returnData, |
| 279 |
pages2: pages || data.pages, |
| 280 |
page2: page, |
| 281 |
status2: status, |
| 282 |
message2: message, |
| 283 |
}; |
| 284 |
}; |
| 285 |
|
| 286 |
Sekeleton(); |
| 287 |
|
| 288 |
document.addEventListener( 'click', ( e ) => { |
| 289 |
const sectionBtns = document.querySelectorAll( |
| 290 |
'.section-item__loadmore' |
| 291 |
); |
| 292 |
|
| 293 |
[ ...sectionBtns ].map( async ( sectionBtn ) => { |
| 294 |
if ( sectionBtn.contains( e.target ) && ! isLoadingItems ) { |
| 295 |
isLoadingItems = true; |
| 296 |
const sectionItem = sectionBtn.parentNode; |
| 297 |
const sectionId = sectionItem.getAttribute( 'data-section-id' ); |
| 298 |
const sectionContent = |
| 299 |
sectionItem.querySelector( '.section-content' ); |
| 300 |
|
| 301 |
const paged = parseInt( sectionBtn.dataset.page ); |
| 302 |
|
| 303 |
sectionBtn.classList.add( 'loading' ); |
| 304 |
|
| 305 |
try { |
| 306 |
const response = await getResponsiveItem( |
| 307 |
'', |
| 308 |
paged + 1, |
| 309 |
sectionId, |
| 310 |
'' |
| 311 |
); |
| 312 |
|
| 313 |
const { data3, pages3, status3, message3 } = response; |
| 314 |
|
| 315 |
if ( status3 === 'error' ) { |
| 316 |
throw new Error( message3 || 'Error' ); |
| 317 |
} |
| 318 |
|
| 319 |
if ( pages3 <= paged + 1 ) { |
| 320 |
sectionBtn.remove(); |
| 321 |
} else { |
| 322 |
sectionBtn.dataset.page = paged + 1; |
| 323 |
} |
| 324 |
|
| 325 |
sectionContent.insertAdjacentHTML( 'beforeend', data3 ); |
| 326 |
} catch ( e ) { |
| 327 |
sectionContent.insertAdjacentHTML( |
| 328 |
'beforeend', |
| 329 |
`<div class="lp-ajax-message error" style="display:block">${ |
| 330 |
e.message || |
| 331 |
'Error: Query lp/v1/lazy-load/course-curriculum' |
| 332 |
}</div>` |
| 333 |
); |
| 334 |
} |
| 335 |
|
| 336 |
sectionBtn.classList.remove( 'loading' ); |
| 337 |
|
| 338 |
searchCourseContent(); |
| 339 |
} |
| 340 |
} ); |
| 341 |
|
| 342 |
// Load more Sections |
| 343 |
const moreSections = document.querySelectorAll( |
| 344 |
'.curriculum-more__button' |
| 345 |
); |
| 346 |
|
| 347 |
[ ...moreSections ].map( async ( moreSection ) => { |
| 348 |
if ( moreSection.contains( e.target ) && ! isLoadingSections ) { |
| 349 |
isLoadingSections = true; |
| 350 |
const paged = parseInt( moreSection.dataset.page ); |
| 351 |
|
| 352 |
const sections = |
| 353 |
moreSection.parentNode.parentNode.querySelector( |
| 354 |
'.curriculum-sections' |
| 355 |
); |
| 356 |
|
| 357 |
if ( paged && sections ) { |
| 358 |
moreSection.classList.add( 'loading' ); |
| 359 |
|
| 360 |
try { |
| 361 |
const response2 = await getResponsive( |
| 362 |
'', |
| 363 |
paged + 1, |
| 364 |
'' |
| 365 |
); |
| 366 |
|
| 367 |
const { data2, pages2, status2, message2 } = response2; |
| 368 |
|
| 369 |
if ( status2 === 'error' ) { |
| 370 |
throw new Error( message2 || 'Error' ); |
| 371 |
} |
| 372 |
|
| 373 |
if ( pages2 <= paged + 1 ) { |
| 374 |
moreSection.remove(); |
| 375 |
} else { |
| 376 |
moreSection.dataset.page = paged + 1; |
| 377 |
} |
| 378 |
|
| 379 |
sections.insertAdjacentHTML( 'beforeend', data2 ); |
| 380 |
} catch ( e ) { |
| 381 |
sections.insertAdjacentHTML( |
| 382 |
'beforeend', |
| 383 |
`<div class="lp-ajax-message error" style="display:block">${ |
| 384 |
e.message || |
| 385 |
'Error: Query lp/v1/lazy-load/course-curriculum' |
| 386 |
}</div>` |
| 387 |
); |
| 388 |
} |
| 389 |
|
| 390 |
moreSection.classList.remove( 'loading' ); |
| 391 |
|
| 392 |
searchCourseContent(); |
| 393 |
} |
| 394 |
} |
| 395 |
} ); |
| 396 |
|
| 397 |
// Show/Hide accordion |
| 398 |
if ( document.querySelector( '.learnpress-course-curriculum' ) ) { |
| 399 |
const sections = document.querySelectorAll( '.section' ); |
| 400 |
|
| 401 |
[ ...sections ].map( ( section ) => { |
| 402 |
if ( section.contains( e.target ) ) { |
| 403 |
const toggle = section.querySelector( '.section-left' ); |
| 404 |
|
| 405 |
toggle.contains( e.target ) && |
| 406 |
section.classList.toggle( 'closed' ); |
| 407 |
} |
| 408 |
} ); |
| 409 |
} |
| 410 |
} ); |
| 411 |
} |
| 412 |
|