?AlkantarClanX12

Your IP : 216.73.216.177


Current Path : /home/rankinh/saintgilleslesbains/wp-content/themes/pressville/assets/js/
Upload File :
Current File : /home/rankinh/saintgilleslesbains/wp-content/themes/pressville/assets/js/dashboard-phone-number.js

window.addEventListener("load", () => {
    if(document.querySelector("#PhoneNumberClickContainer")){
        const MONTHS = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"]

        const categoryFilter = document.querySelector("#categoryFilter")

        const PhoneNumberByMonth = document.querySelector("#PhoneNumberByMonth")
        const PhoneNumberByCategory = document.querySelector("#PhoneNumberByCategory")

        const PhoneNumberClickList = document.querySelector("#PhoneNumberClickList")

        const pie_config = {
            type: 'pie',
        }

        const render_charts_and_table = () => {
            const phone_number_clicks = JSON.parse(sessionStorage.getItem("phone_number_clicks"))
            console.log(phone_number_clicks)

            const phone_number_clicks_categories = [...new Set(phone_number_clicks.map(({url}) => {
                const url_match = url.match(/\/guide\/([^\/]+)\//)
                const category = url_match ? url_match[1] : null
                return category
            }))]
            categoryFilter.insertAdjacentHTML("beforeend", Array.from(phone_number_clicks_categories.map(category => `<option value="${category}">${category}</option>`)).join(""))

            let phone_number_clicks_formatted = phone_number_clicks.map(({date_create, url}) => {
                const url_match = url.match(/\/guide\/([^\/]+)\//)
                const date_match = date_create.match(/(\d{4})-(\d{2})/)
                const { year, month } = date_match ? { year: date_match[1], month: date_match[2] } : { year: null, month: null }
                const category = url_match ? url_match[1] : null

                return {
                    category,
                    year,
                    month
                }
            })
            sessionStorage.setItem("phone_number_clicks_formatted", JSON.stringify(phone_number_clicks_formatted))

            let phone_number_clicks_formatted_for_table = phone_number_clicks.map(({ID, date_create, url}) => {
                const url_match = url.match(/\/guide\/([^\/]+)\//)
                const date_match = date_create.match(/(\d{4})-(\d{2})/)
                const { year, month } = date_match ? { year: date_match[1], month: date_match[2] } : { year: null, month: null }
                const category = url_match ? url_match[1] : null

                return {
                    ID,
                    url,
                    category,
                    date_create,
                    year,
                    month
                }
            })
            sessionStorage.setItem("phone_number_clicks_formatted_for_table", JSON.stringify(phone_number_clicks_formatted_for_table))

			const count_by_category = phone_number_clicks_formatted.reduce((acc, item) => {
				acc[item.category] = (acc[item.category] || 0) + 1
				return acc
			}, {})
			console.log(count_by_category)
            const phone_number_clicks_by_category_formatted_config = {
				labels: Array.from(new Set(phone_number_clicks_formatted.map(el => el.category))),
				datasets: [
					{
						label: "Nombre de click par catégorie ",
						data: Array.from(Object.values(count_by_category)),
						backgroundColor: ["rgb(255, 99, 132)", "rgb(54, 162, 235)", "rgb(255, 205, 86)"],
						hoverOffset: 4,
					},
				],
			}

            const count_by_month = Array(12).fill(0)
            phone_number_clicks_formatted.forEach(item => {
                const monthIndex = parseInt(item.month, 10) - 1
                count_by_month[monthIndex] += 1
            })
            // const count_by_month = phone_number_clicks_formatted.reduce((acc, item) => {
			// 	acc[item.month] = (acc[item.month] || 0) + 1
			// 	return acc
			// }, {})
			console.log(count_by_month)
            const phone_number_clicks_by_month_formatted_config = {
				labels: MONTHS,
				datasets: [
					{
						label: "Nombre de click par mois ",
						data: Array.from(Object.values(count_by_month)),
						backgroundColor: ["rgb(255, 99, 132)", "rgb(54, 162, 235)", "rgb(255, 205, 86)"],
						hoverOffset: 4,
					},
				],
			}
            
            const PhoneNumberByCategoryChart = new Chart(PhoneNumberByCategory, {
                ...pie_config,
                data: phone_number_clicks_by_category_formatted_config
            })
            
            const PhoneNumberByMonthChart = new Chart(PhoneNumberByMonth, {
                ...pie_config,
                data: phone_number_clicks_by_month_formatted_config
            })

            const PhoneNumberClickList = document.querySelector("#PhoneNumberClickList")
            const PhoneNumberClickListTable = new DataTable(PhoneNumberClickList, {
                language:{
                    url: "//cdn.datatables.net/plug-ins/2.1.8/i18n/fr-FR.json"
                },
                order: {
                    idx: 3,
                    dir: 'desc'
                },
                columns: [
                    { title: 'ID' },
                    { title: 'Url de l\'annonce' },
                    { title: 'Catégorie' },
                    { title: 'Cliqué le' },
                ],
                data: phone_number_clicks_formatted_for_table.map(item => [item.ID, item.url, item.category, item.date_create]),
                columnDefs: [{
                    "targets": 1,
                    "render": function ( data, type, row, meta ) {
                        return '<a href="'+data+'" target="_blank">'+data+'</a>';
                    }
                }]
            })

            const year_filter = document.querySelector(".dashboard-filter select#yearFilter")
            const month_filter = document.querySelector(".dashboard-filter select#monthFilter")
            const category_filter = document.querySelector(".dashboard-filter select#categoryFilter")

            const update_charts = () => {
                const [year_selected, month_selected, category_selected] = [year_filter.value, month_filter.value, category_filter.value]
                
                const phone_number_clicks_formatted = JSON.parse(sessionStorage.getItem("phone_number_clicks_formatted"))
                const phone_number_clicks_formatted_for_month_graph = phone_number_clicks_formatted.filter(el => {
                    return (year_selected != "all" ? el.year == year_selected : true) && 
                        (category_selected != "all" ? el.category == category_selected : true)
                })
                const phone_number_clicks_formatted_for_category_graph = phone_number_clicks_formatted.filter(el => {
                    return (year_selected != "all" ? el.year == year_selected : true) && 
                        (month_selected != "all" ? el.month == month_selected : true)
                })
                console.log(phone_number_clicks_formatted_for_month_graph, phone_number_clicks_formatted_for_category_graph)
            }

            const update_datatable = () => {
                const [year_selected, month_selected, category_selected] = [year_filter.value, month_filter.value, category_filter.value]
                
                const phone_number_clicks_formatted = JSON.parse(sessionStorage.getItem("phone_number_clicks_formatted_for_table"))
                const phone_number_clicks_formatted_for_datatable = phone_number_clicks_formatted.filter(el => {
                    return (year_selected != "all" ? el.year == year_selected : true) && 
                        (category_selected != "all" ? el.category == category_selected : true) && 
                        (month_selected != "all" ? el.month == month_selected : true)
                })
                console.log(phone_number_clicks_formatted_for_datatable)

            }

            const filters = document.querySelectorAll(".dashboard-filter select")
            filters.forEach(filter => {
                filter.addEventListener("change", () => {
                    update_charts()
                    update_datatable()
                })
            })
        }

        if(JSON.parse(sessionStorage.getItem("phone_number_clicks")) == null) {
            fetch("https://saintgilleslesbains.com/wp-json/phone-number/get")
            .then(json => json.json())
            .then(data => {
                sessionStorage.setItem("phone_number_clicks", JSON.stringify(data))
                render_charts_and_table()
            })
        } else {
            render_charts_and_table()
        }

    }
})