diff --git a/includes/api.php b/includes/api.php index a03b823..250c111 100644 --- a/includes/api.php +++ b/includes/api.php @@ -51,34 +51,31 @@ add_action('rest_api_init', function () { 'permission_callback' => '__return_true', )); + // * BUILD BROCHURES ARCHIVE ROWS + register_rest_route('homegrade-datas/v1/build', '/brochures-archive-rows', array( + 'methods' => 'GET', + 'callback' => 'build_brochure_archive_rows', + 'permission_callback' => '__return_true', + + )); + // * BUILD FICHE INFOS ARCHIVE ROWS + register_rest_route('homegrade-datas/v1/build', '/fiche-info-archive-rows', array( + 'methods' => 'GET', + 'callback' => 'build_fiches_info_archive_rows', + 'permission_callback' => '__return_true', + + )); + // * BUILD WEBINAIRE ARCHIVE ROWS + // register_rest_route('homegrade-datas/v1/build', '/webinaire-archive-rows', array( + // 'methods' => 'GET', + // 'callback' => 'build_webinaire_archive_rows', + // 'permission_callback' => '__return_true', + + // )); }); - -// function build_card($request) -// { -// $cardId = $request['cardId']; - - -// $test = get_template_part( -// 'template-components/cards/card-news', -// null, -// array( -// 'card_variant' => 'activite', -// 'post_ID' => $cardId, -// 'post_title' => get_the_title($cardId), -// // 'post_thumbnail' => $post_thumbnail, -// // 'news_type' => $news_type, -// ) -// ); -// $response = new WP_REST_Response($test); -// $response->set_status(200); - -// return $response; -// } - - function get_news($request) { $args = array( @@ -92,12 +89,6 @@ function get_news($request) return $response; } - - -/* ---------------- - NEWS BY TYPE ID ------------------*/ - function get_news_posts_per_type_id($request) { @@ -119,6 +110,31 @@ function get_news_posts_per_type_id($request) return $response; } +function get_questions_posts_per_thematique_id($request) +{ + $excluded_ids = $request->get_param('excluded_ids'); + $excludedArray = explode(',', $excluded_ids); + + $thematiqueId = $request['thematiqueId']; + $args = array( + "post_type" => "questions", + "posts_per_page" => -1, + "post__not_in" => $excludedArray, + 'tax_query' => array( + array( + 'taxonomy' => 'thematiques', + 'field' => 'term_id', + 'terms' => $thematiqueId + ) + ) + ); + $postDatas = get_posts($args); + $response = new WP_REST_Response($postDatas); + $response->set_status(200); + + return $response; +} + function build_news_posts_feed_all($request) { @@ -212,26 +228,77 @@ function build_news_posts_feed_per_type_id($request) return $response; } -function get_questions_posts_per_thematique_id($request) +function build_brochure_archive_rows() { - $excluded_ids = $request->get_param('excluded_ids'); - $excludedArray = explode(',', $excluded_ids); - - $thematiqueId = $request['thematiqueId']; $args = array( - "post_type" => "questions", - "posts_per_page" => -1, - "post__not_in" => $excludedArray, - 'tax_query' => array( - array( - 'taxonomy' => 'thematiques', - 'field' => 'term_id', - 'terms' => $thematiqueId - ) + 'post_type' => 'brochures', + 'posts_per_page' => -1, + 'post_status' => 'publish', + 'order' => 'DESC', + 'orderby' => 'date', + 'meta_key' => 'brochure_pdf', + 'meta_value' => '', + 'meta_compare' => '!=', + // ICI METAQUERY FOR LE PDF + ); + $brochuresPosts = new WP_Query($args); + + ob_start(); + get_template_part( + 'template-components/archives/brochure-rows', + null, + array( + 'brochuresPosts' => $brochuresPosts, ) ); - $postDatas = get_posts($args); - $response = new WP_REST_Response($postDatas); + $html_template = ob_get_clean(); + + + + $response_data = array( + 'posts' => $brochuresPosts->posts, + 'html_template' => $html_template, + 'total_posts_found' => count($brochuresPosts->posts), + ); + + $response = new WP_REST_Response($response_data); + $response->set_status(200); + + return $response; +} +function build_fiches_info_archive_rows() +{ + $args = array( + 'post_type' => 'fiches-infos', + 'posts_per_page' => -1, + 'post_status' => 'publish', + 'order' => 'DESC', + 'orderby' => 'date', + 'meta_key' => 'brochure_pdf', + 'meta_value' => '', + 'meta_compare' => '!=', + + ); + $ficheInfoPosts = new WP_Query($args); + + ob_start(); + get_template_part( + 'template-components/archives/fiche-info-rows', + null, + array( + 'fichesInfosPosts' => $ficheInfoPosts, + ) + ); + $html_template = ob_get_clean(); + + + $response_data = array( + 'posts' => $ficheInfoPosts->posts, + 'html_template' => $html_template, + 'total_posts_found' => count($ficheInfoPosts->posts), + ); + + $response = new WP_REST_Response($response_data); $response->set_status(200); return $response; diff --git a/resources/css/components/filters-toolbar.css b/resources/css/components/filters-toolbar.css index d9357a9..412e4c1 100644 --- a/resources/css/components/filters-toolbar.css +++ b/resources/css/components/filters-toolbar.css @@ -15,6 +15,10 @@ li { @apply h-auto; } + + &__posts-results-indications { + @apply flex justify-between items-center; + } &__posts-results-amount { @apply pb-4; @@ -49,6 +53,10 @@ @apply block; } } + + &__action-button--load-all { + @apply underline; + } &__action-button--active { @apply text-secondary; outline: transparent; diff --git a/resources/css/pages/template-archive-publications.css b/resources/css/pages/template-archive-publications.css index f1b8699..a104fa1 100644 --- a/resources/css/pages/template-archive-publications.css +++ b/resources/css/pages/template-archive-publications.css @@ -1,4 +1,7 @@ .template-archives--publications { + section[isLoading] { + @apply opacity-30; + } .archives-publications-header { @apply bg-primary max-w-screen-xl text-white text-center mx-auto; } diff --git a/resources/js/app.js b/resources/js/app.js index 0be0cff..a8e041a 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -4,6 +4,7 @@ import singleConseil from './single-conseil'; import SchemaBulletPointsInit from './schema-bullet-points'; import taxonomyThematiqueFaqInit from './taxonomy-thematique-(faq)'; import archiveNewsInit from './archive-template-news'; +import filterPublicationsInit from './archive-template-publications'; // window.addEventListener('load', function () {}); window.addEventListener('DOMContentLoaded', (event) => { @@ -11,6 +12,10 @@ window.addEventListener('DOMContentLoaded', (event) => { SchemaBulletPointsInit(); singleConseil(); editorInit(); + taxonomyThematiqueFaqInit(); + // NEWS archiveNewsInit(); + // PUBLICATIONS + filterPublicationsInit(); }); diff --git a/resources/js/archive-template-publications.js b/resources/js/archive-template-publications.js new file mode 100644 index 0000000..2d5d18e --- /dev/null +++ b/resources/js/archive-template-publications.js @@ -0,0 +1,161 @@ +function updateSearchResultsIndications(publicationType, e) { + if (!publicationType) return; + + const searchResultsComponent = document.querySelector('.filters-toolbar__posts-results-amount'); + const currentActiveButton = e.target; + let returnedElementsAmount; + + switch (publicationType) { + case 'brochures': + returnedElementsAmount = document.querySelectorAll( + '.brochures-archives .publications-grid__row' + ).length; + searchResultsComponent.classList.add('filter-active'); + searchResultsComponent.classList.remove('filter-disable'); + searchResultsComponent.querySelector('.results-filter-name').innerHTML = + 'Brochures Thématiques'; + + break; + + case 'fiches-infos': + returnedElementsAmount = document.querySelectorAll( + '.fiches-infos-archives .publications-grid__row' + ).length; + searchResultsComponent.classList.add('filter-active'); + searchResultsComponent.classList.remove('filter-disable'); + searchResultsComponent.querySelector('.results-filter-name').innerHTML = 'Fiches infos'; + break; + + case 'webinaires': + returnedElementsAmount = document.querySelectorAll( + '.videos-webinaires-archives .publications-grid__row' + ).length; + searchResultsComponent.classList.add('filter-active'); + searchResultsComponent.classList.remove('filter-disable'); + searchResultsComponent.querySelector('.results-filter-name').innerHTML = + 'Vidéos et Webinaires'; + break; + case 'all': + const brochureElementsAmount = document.querySelectorAll( + '.fiches-infos-archives .publications-grid__row' + ).length; + const webinairesElementsAmount = document.querySelectorAll( + '.videos-webinaires-archives .publications-grid__row' + ).length; + const fichesInfosElementsAmount = document.querySelectorAll( + '.fiches-infos-archives .publications-grid__row' + ).length; + returnedElementsAmount = + fichesInfosElementsAmount + webinairesElementsAmount + brochureElementsAmount; + + searchResultsComponent.classList.add('filter-disable'); + searchResultsComponent.classList.remove('filter-active'); + break; + default: + searchResultsComponent.classList.add('filter-disable'); + searchResultsComponent.classList.remove('filter-active'); + return; + } + + const postAmountSpan = searchResultsComponent.querySelector('.results-amount'); + postAmountSpan.innerHTML = returnedElementsAmount; +} +function toggleActiveFilterButton(e) { + const filterButtons = document.querySelectorAll('.filters-toolbar__action-button'); + + filterButtons.forEach((button) => { + button.classList.remove('filters-toolbar__action-button--active'); + }); + + const activeFilterButton = e.target.getAttribute('data-publication-type') + ? e.target + : e.target.parentElement.getAttribute('data-publication-type') + ? e.target.parentElement + : null; + activeFilterButton.classList.add('filters-toolbar__action-button--active'); +} + +async function hydrateBrochureArchiveGrid() { + const response = await fetch(`/wp-json/homegrade-datas/v1/build/brochures-archive-rows`); + brochuresDatas = await response.json(); + + const brochureRows = document.querySelector('.brochures-archives #brochures-rows'); + brochureRows.innerHTML = brochuresDatas.html_template; +} + +async function hydrateFicheInfoArchiveGrid() { + const response = await fetch(`/wp-json/homegrade-datas/v1/build/fiche-info-archive-rows`); + ficheInfosDatas = await response.json(); + + const ficheInfoRows = document.querySelector('.fiches-infos-archives #fiche-infos-rows'); + ficheInfoRows.innerHTML = ficheInfosDatas.html_template; +} +async function hydrateAll() { + await hydrateBrochureArchiveGrid(); + await hydrateFicheInfoArchiveGrid(); +} +async function filterPublications(publicationType, e) { + const sectionFichesInfos = document.querySelector('.fiches-infos-archives'); + const sectionBrochures = document.querySelector('.brochures-archives'); + const sectionWebinaires = document.querySelector('.videos-webinaires-archives'); + + sectionFichesInfos.removeAttribute('hidden'); + sectionWebinaires.removeAttribute('hidden'); + sectionBrochures.removeAttribute('hidden'); + + switch (publicationType) { + case 'all': + sectionBrochures.setAttribute('isLoading', true); + sectionFichesInfos.setAttribute('isLoading', true); + await hydrateAll(); + sectionBrochures.removeAttribute('isLoading'); + sectionFichesInfos.removeAttribute('isLoading'); + break; + + case 'brochures': + sectionFichesInfos.setAttribute('hidden', true); + sectionWebinaires.setAttribute('hidden', true); + + sectionBrochures.setAttribute('isLoading', true); + await hydrateBrochureArchiveGrid(); + sectionBrochures.removeAttribute('isLoading'); + break; + + case 'fiches-infos': + sectionBrochures.setAttribute('hidden', true); + sectionWebinaires.setAttribute('hidden', true); + + sectionFichesInfos.setAttribute('isLoading', true); + await hydrateFicheInfoArchiveGrid(); + sectionFichesInfos.removeAttribute('isLoading'); + break; + + case 'webinaires': + sectionFichesInfos.setAttribute('hidden', true); + sectionBrochures.setAttribute('hidden', true); + sectionWebinaires.setAttribute('isLoading', true); + setTimeout(() => { + sectionWebinaires.removeAttribute('isLoading'); + }, 300); + break; + } + + updateSearchResultsIndications(publicationType, e); + toggleActiveFilterButton(e); +} + +export default function filterPublicationsInit() { + const filterButtons = document.querySelectorAll('.filters-toolbar__action-button'); + const loadAllbutton = document.querySelector('.filters-toolbar__action-button--load-all'); + + filterButtons.forEach((button) => { + const publicationType = button.getAttribute('data-publication-type'); + button.addEventListener('click', (e) => { + filterPublications(publicationType, e); + }); + }); + loadAllbutton.addEventListener('click', (e) => { + const publicationType = loadAllbutton.getAttribute('data-publication-type'); + filterPublications(publicationType, e); + }); +} diff --git a/template-components/archives/brochure-rows.php b/template-components/archives/brochure-rows.php new file mode 100644 index 0000000..0644772 --- /dev/null +++ b/template-components/archives/brochure-rows.php @@ -0,0 +1,31 @@ + + +have_posts()) : while ($brochuresPosts->have_posts()) : $brochuresPosts->the_post(); ?> + + +
  • + + + +

    +

    name ?>

    + + +

    + +
    + +
    +
  • + + + \ No newline at end of file diff --git a/template-components/archives/fiche-info-rows.php b/template-components/archives/fiche-info-rows.php new file mode 100644 index 0000000..4761014 --- /dev/null +++ b/template-components/archives/fiche-info-rows.php @@ -0,0 +1,33 @@ + + +have_posts()) : while ($fichesInfosPosts->have_posts()) : $fichesInfosPosts->the_post(); ?> + + term_id)['icon'] ?? null; + ?> +
  • + + + + +

    +

    name ?>

    + +

    + + +
    + +
    +
  • + + \ No newline at end of file diff --git a/template-components/archives/webinaire-rows.php b/template-components/archives/webinaire-rows.php new file mode 100644 index 0000000..02313ba --- /dev/null +++ b/template-components/archives/webinaire-rows.php @@ -0,0 +1,26 @@ + + + +
  • + + + +

    + +

    name ?>

    + + +

    + +

    + + +
    + +
    +
  • + + \ No newline at end of file diff --git a/template-publications.php b/template-publications.php index a1d9855..134b6bf 100644 --- a/template-publications.php +++ b/template-publications.php @@ -66,42 +66,46 @@ $total_posts_found = $brochuresPosts->found_posts + $fichesInfosPosts->found_pos -
    -

    - - - - - - +

    +
    +

    + + + + + + + + + + + + + +

    + + +
    - - - - - - -