Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
news-articles.js 5.24 KiB
Newer Older
peter_rabbit's avatar
peter_rabbit committed
"use strict";

peter_rabbit's avatar
peter_rabbit committed
const { articles_url } = require("../../constants");
peter_rabbit's avatar
peter_rabbit committed
const objectHtmlRenderer = require("../lib/object-html-renderer");
peter_rabbit's avatar
peter_rabbit committed
const ImageCarousel = require("../generic-components/image-carousel");
const { loadArticles, getArticleDate, getArticleBody } = require("../lib/article-utils");
peter_rabbit's avatar
peter_rabbit committed

peter_rabbit's avatar
peter_rabbit committed
class NewsArticles {
peter_rabbit's avatar
peter_rabbit committed
    constructor() {
        this.id = performance.now().toString();
        this.state = {
            loading: true,
            articles: [],
            showArticleIndex: -1,
        };
        this.loadArticles();
    }

    loadArticles() {
peter_rabbit's avatar
peter_rabbit committed
        loadArticles(`${articles_url}news`).then(articles => {
peter_rabbit's avatar
peter_rabbit committed
            this.state.articles = articles;
            this.state.showArticleIndex = this.state.articles.length - 1;
            this.refresh();
        });
peter_rabbit's avatar
peter_rabbit committed
    }

    refresh() {
        objectHtmlRenderer.subRender(this.render(), document.getElementById(this.id), {
            mode: "replace",
        });
    }

    renderArticle(articleData) {
        return {
            tag: "article",
peter_rabbit's avatar
peter_rabbit committed
            class: `${articleData.images ? "grid-2" : "grid-1"}`,
            typeof: "Article",
peter_rabbit's avatar
peter_rabbit committed
            contents: [
                {
                    tag: "div",
                    class: "date",
                    contents: [
                        {
                            tag: "time",
                            property: "datePublished",
                            contents: getArticleDate(articleData.date),
                        },
                    ],
peter_rabbit's avatar
peter_rabbit committed
                },
                {
                    tag: "div",
                    class: "title",
                    contents: [
                        {
                            tag: "h3",
                            contents: articleData.title,
                            property: "headline",
peter_rabbit's avatar
peter_rabbit committed
                        },
                    ],
                },
                {
                    tag: "div",
                    class: "subtitle",
                    contents: [
                        {
                            tag: "strong",
                            contents: articleData.subtitle,
                            property: "alternativeHeadline",
peter_rabbit's avatar
peter_rabbit committed
                        },
                    ],
                },
                {
                    tag: "div",
                    class: "body",
                    contents: [
                        {
                            tag: "p",
peter_rabbit's avatar
peter_rabbit committed
                            contents: getArticleBody(articleData.body),
                            property: "articleBody",
peter_rabbit's avatar
peter_rabbit committed
                        },
                    ],
                },
peter_rabbit's avatar
peter_rabbit committed
                articleData.images &&
                    new ImageCarousel({
                        images: articleData.images.map(img => `${articleData.path}/images/${img}`),
                    }).render(),
peter_rabbit's avatar
peter_rabbit committed
            ],
        };
    }

    renderArticlePlaceholder() {
        return {
            tag: "article",
            class: "article-placeholder",
peter_rabbit's avatar
peter_rabbit committed
            contents: [
                { tag: "div", class: "date" },
                { tag: "div", class: "title" },
                { tag: "div", class: "subtitle" },
                { tag: "div", class: "body" },
                { tag: "div", class: "image-carousel" },
peter_rabbit's avatar
peter_rabbit committed
            ],
        };
    }

    handleChangeArticle(dir) {
        let { showArticleIndex, articles } = this.state;
        showArticleIndex =
            dir === "prev"
                ? showArticleIndex - 1 >= 0
                    ? showArticleIndex - 1
                    : 0
                : showArticleIndex + 1 <= articles.length - 1
                ? showArticleIndex + 1
                : articles.length - 1;
        this.state.showArticleIndex = showArticleIndex;
        this.refresh();
    }

    render() {
        const { articles, showArticleIndex } = this.state,
            showNext = showArticleIndex < articles.length - 1,
            showPrev = showArticleIndex > 0;
        return {
            tag: "div",
            id: this.id,
            class: "articles-displayer page-contents-center",
            contents:
                articles.length > 0
                    ? [
                          this.renderArticle(articles[showArticleIndex]),
                          {
peter_rabbit's avatar
peter_rabbit committed
                              tag: "div",
                              class: "prev-next-buttons",
                              contents: [
                                  {
                                      tag: "button",
                                      class: `prev-btn ${!showPrev ? "disabled" : "active"}`,
                                      contents: "Précédent",
                                      onclick: this.handleChangeArticle.bind(this, "prev"),
                                  },
                                  {
                                      tag: "button",
                                      class: `next-btn ${!showNext ? "disabled" : "active"}`,
                                      contents: "Suivant",
                                      onclick: this.handleChangeArticle.bind(this, "next"),
                                  },
                              ],
peter_rabbit's avatar
peter_rabbit committed
                          },
                      ]
peter_rabbit's avatar
peter_rabbit committed
                    : [this.renderArticlePlaceholder()],
peter_rabbit's avatar
peter_rabbit committed
module.exports = NewsArticles;