@@ -6,51 +6,61 @@ import Loading from '../loading';
66
77Chart . register ( CategoryScale , LinearScale , PointElement , LineElement , Title , Tooltip , Legend ) ;
88
9- const ArticleViewsGraph = ( { article } ) => {
9+ const ArticleViewsGraph = ( { articles } ) => {
1010 const [ viewData , setViewData ] = useState ( { } ) ;
1111 const [ loading , setLoading ] = useState ( true ) ;
12+
1213 useEffect ( ( ) => {
13- const fetchViews = async ( ) => {
14+ const fetchAllViews = async ( ) => {
1415 try {
15- const response = await fetchPageViewsCount ( article ) ;
16- const views = response . items ;
17- const dates = views . map ( ( view ) => {
18- const year = view . timestamp . substring ( 0 , 4 ) ;
19- const month = view . timestamp . substring ( 4 , 6 ) ;
20- const day = view . timestamp . substring ( 6 , 8 ) ;
21-
22- const dateString = `${ year } -${ month } -${ day } ` ;
23- const date = new Date ( dateString ) ;
24- return date . toLocaleDateString ( ) ;
16+ const allDataPromises = articles . map ( async ( article ) => {
17+ const response = await fetchPageViewsCount ( article ) ;
18+ const views = response . items ;
19+ const dates = views . map ( ( view ) => {
20+ const year = view . timestamp . substring ( 0 , 4 ) ;
21+ const month = view . timestamp . substring ( 4 , 6 ) ;
22+ const day = view . timestamp . substring ( 6 , 8 ) ;
23+
24+ const dateString = `${ year } -${ month } -${ day } ` ;
25+ const date = new Date ( dateString ) ;
26+ return date . toLocaleDateString ( ) ;
27+ } ) ;
28+ const counts = views . map ( ( view ) => view . views ) ;
29+ return { article : article . article , dates, counts } ;
2530 } ) ;
26- const counts = views . map ( ( view ) => view . views ) ;
31+
32+ const allData = await Promise . all ( allDataPromises ) ;
33+
34+ const combinedDates = [ ...new Set ( allData . flatMap ( ( data ) => data . dates ) ) ] . sort ( ( a , b ) => new Date ( a ) - new Date ( b ) ) ;
35+ const datasets = allData . map ( ( data , index ) => ( {
36+ label : decodeURIComponent ( data . article ) ,
37+ data : combinedDates . map ( ( date ) => {
38+ const viewIndex = data . dates . indexOf ( date ) ;
39+ return viewIndex !== - 1 ? data . counts [ viewIndex ] : 0 ;
40+ } ) ,
41+ fill : false ,
42+ backgroundColor : `rgba(${ 75 + index * 50 } , 192, 192, 0.6)` ,
43+ borderColor : `rgba(${ 75 + index * 50 } , 192, 192, 1)` ,
44+ } ) ) ;
2745
2846 setViewData ( {
29- labels : dates ,
30- datasets : [
31- {
32- label : 'Views' ,
33- data : counts ,
34- fill : false ,
35- backgroundColor : 'rgba(75, 192, 192, 0.6)' ,
36- borderColor : 'rgba(75, 192, 192, 1)' ,
37- } ,
38- ] ,
47+ labels : combinedDates ,
48+ datasets,
3949 } ) ;
50+
4051 setLoading ( false ) ;
4152 } catch ( error ) {
4253 setLoading ( true ) ;
43-
4454 return ;
4555 }
4656 } ;
4757
48- fetchViews ( ) ;
49- } , [ article ] ) ;
58+ fetchAllViews ( ) ;
59+ } , [ articles ] ) ;
5060
5161 return (
52- < div className = 'graph w-3/4 flex flex-col items-center' >
53- < h2 > Views for { decodeURIComponent ( article . article ) } </ h2 >
62+ < div className = 'graph w-full flex flex-col items-center' >
63+ < h2 className = 'font-bold text-xl self-start' > Views for Multiple Articles </ h2 >
5464 { loading ? < Loading /> : viewData . labels ? < Line data = { viewData } /> : < p > No data available.</ p > }
5565 </ div >
5666 ) ;
0 commit comments