templates/bundles/SyliusAdminBundle/Script/_statistics.html.twig line 1

Open in your IDE?
  1. <script>
  2.     class StatisticsComponent {
  3.         constructor(wrapper) {
  4.             if (!wrapper) return;
  5.             this.weekInMilliseconds = 604800000;
  6.             this.wrapper = wrapper;
  7.             this.chart = null;
  8.             this.chartCanvas = this.wrapper.querySelector('#stats-graph');
  9.             this.summaryBoxes = this.wrapper.querySelectorAll('[data-stats-summary]');
  10.             this.buttons = this.wrapper.querySelectorAll('[data-stats-button]');
  11.             this.loader = this.wrapper.querySelector('.stats-loader');
  12.             this.DateObjectFactory = new DateObjectFactory();
  13.             this.init();
  14.         }
  15.         init() {
  16.             const defaultInterval = 'month';
  17.             const productsInputEl = document.querySelector('#products');
  18.             productsInputEl.addEventListener('change', this.fetchData.bind(this));
  19.             const doctorInputEl = document.querySelector('#doctor');
  20.             
  21.             if (doctorInputEl) {
  22.                 doctorInputEl.addEventListener('change', this.fetchData.bind(this));
  23.             }
  24.             this.buttons.forEach((button) => {
  25.                 button.addEventListener('click', this.fetchData.bind(this));
  26.                 if (button.getAttribute('data-stats-button') === defaultInterval) {
  27.                     button.classList.add('active');
  28.                 }
  29.             });
  30.             this.initializeNavButtons(defaultInterval);
  31.             const labels = this.chartCanvas.getAttribute('data-labels') || '[]';
  32.             const values = this.chartCanvas.getAttribute('data-values') || '[]';
  33.             const currency = this.chartCanvas.getAttribute('data-currency') || '';
  34.             this.chart = drawChart(this.chartCanvas, JSON.parse(labels), JSON.parse(values), { prefix: currency });
  35.         }
  36.         initializeNavButtons(defaultInterval) {
  37.             this.prevButton = document.getElementById('navigation-prev');
  38.             this.nextButton = document.getElementById('navigation-next');
  39.             this.prevButton.addEventListener('click', this.fetchData.bind(this));
  40.             this.nextButton.addEventListener('click', this.fetchData.bind(this));
  41.             const DateObject = this.DateObjectFactory.createDateObject(defaultInterval, new Date());
  42.             this.updateNavButtons(
  43.                 defaultInterval,
  44.                 DateObject.prevDate,
  45.                 DateObject.nextDate,
  46.                 DateObject.maxGraphDate,
  47.             );
  48.         }
  49.         fetchData(e) {
  50.             let date = new Date();
  51.             if (e.target.getAttribute('date')) {
  52.                 date = new Date(e.target.getAttribute('date'));
  53.             }
  54.             const activeButtonEl = document.querySelector('[data-stats-button].active');
  55.             const interval = e.target.getAttribute('data-stats-button') || e.target.getAttribute('interval') || activeButtonEl.getAttribute('data-stats-button');
  56.             const DateObject = this.DateObjectFactory.createDateObject(interval, date);
  57.             this.updateNavButtons(interval,
  58.                 DateObject.prevDate,
  59.                 DateObject.nextDate,
  60.                 DateObject.maxGraphDate);
  61.             const productsInputEl = document.querySelector('#products');
  62.             const doctorInputEl = document.querySelector('#doctor');
  63.             const url = `${e.target.getAttribute('data-stats-url')
  64.             }&interval=${DateObject.interval
  65.             }&startDate=${this.formatDate(DateObject.startDate)
  66.             }&endDate=${this.formatDate(DateObject.endDate)
  67.             }&productIds=[${productsInputEl.value
  68.             }]&doctorAsCustomerId=${doctorInputEl?.value}`;
  69.             if (url) {
  70.                 this.toggleLoadingState(true);
  71.                 $.ajax({
  72.                     type: 'GET',
  73.                     url,
  74.                     dataType: 'json',
  75.                     accept: 'application/json',
  76.                 }).done((response) => {
  77.                     this.updateSummaryValues(response.statistics);
  78.                     this.updateButtonsState(e.target);
  79.                     this.updateGraph(response.sales_summary);
  80.                 }).always(() => {
  81.                     this.toggleLoadingState(false);
  82.                 });
  83.             }
  84.         }
  85.         updateSummaryValues(data) {
  86.             this.summaryBoxes.forEach((box) => {
  87.                 const name = box.getAttribute('data-stats-summary');
  88.                 if (name in data) {
  89.                     box.innerHTML = data[name];
  90.                 }
  91.             });
  92.         }
  93.         updateGraph(data) {
  94.             this.chart.data.labels = data.intervals;
  95.             this.chart.data.datasets[0].data = data.sales;
  96.             this.chart.update();
  97.         }
  98.         updateButtonsState(activeButton) {
  99.             const activeButtonEl = document.querySelector('[data-stats-button].active');
  100.             let interval = activeButton.getAttribute('data-stats-button') ? activeButton.getAttribute('data-stats-button')
  101.                 : activeButton.getAttribute('interval') ? activeButton.getAttribute('interval') : activeButtonEl.getAttribute('data-stats-button');
  102.             this.buttons.forEach((button) => {
  103.                 button.classList.remove('active');
  104.                 if (button.getAttribute('data-stats-button') === interval) {
  105.                     button.classList.add('active');
  106.                 }
  107.             });
  108.         }
  109.         toggleLoadingState(loading) {
  110.             if (loading) {
  111.                 this.loader.classList.add('active');
  112.             } else {
  113.                 this.loader.classList.remove('active');
  114.             }
  115.         }
  116.         formatDate(date) {
  117.             let month = `${(date.getMonth() + 1)}`;
  118.             let day = `${date.getDate()}`;
  119.             const year = `${date.getFullYear()}`;
  120.             if (month.length < 2) month = `0${month}`;
  121.             if (day.length < 2) day = `0${day}`;
  122.             return [year, month, day].join('-');
  123.         }
  124.         setInterval(element, interval) {
  125.             element.setAttribute('interval', interval);
  126.         }
  127.         updateNavButtons(interval, prevDate, nextDate, maxGraphDate) {
  128.             this.nextButton.disabled = false;
  129.             this.nextButton.style.visibility = 'visible';
  130.             if (nextDate > maxGraphDate) {
  131.                 this.nextButton.disabled = true;
  132.                 this.nextButton.style.visibility = 'hidden';
  133.             }
  134.             this.prevButton.setAttribute('interval', interval);
  135.             this.nextButton.setAttribute('interval', interval);
  136.             this.prevButton.setAttribute('date', this.formatDate(prevDate));
  137.             this.nextButton.setAttribute('date', this.formatDate(nextDate));
  138.         }
  139.     }
  140.     const dashboardStatistics = new StatisticsComponent(document.querySelector('.custom-stats'));
  141. </script>