11import React , { useEffect , useState } from 'react' ;
22import { connect , useSelector } from 'react-redux' ;
3- import { useLocation } from 'react-router-dom' ;
43import {
54 getActivities ,
65 getMyActivities ,
@@ -21,46 +20,69 @@ import LoadingPage from '../loading/LoadingPage';
2120const useStyles = makeStyles ( styles ) ;
2221
2322function Activities ( props ) {
24- const location = useLocation ( ) ;
2523 const classes = useStyles ( ) ;
2624 const [ loading , setLoading ] = useState ( true ) ;
2725 const { activities } = useSelector ( state => state ) ;
28- let [ activityList , setActivityList ] = useState ( [ ] ) ;
26+ let [ activityList , setActivityList ] = useState ( {
27+ published : [ ] ,
28+ unPublishedActivities : [ ] ,
29+ userActivities : [ ]
30+ } ) ;
2931 const [ tab , setTab ] = useState ( "published" ) ;
3032 const commonClasses = makeStyles ( DefaultStyles ) ( ) ;
3133 const { t } = useTranslation ( ) ;
3234
3335 useEffect ( ( ) => {
34- setActivityList ( activities . all_activities ) ;
36+ setActivityList ( activities ) ;
3537 } , [ activities ] ) ;
36-
37- const flagMap = {
38- staff : ( ) =>
39- props . getUnPublishedActivities ( {
40- t : props . t ,
41- token : props . auth . token ,
42- } ) ,
43- educator : ( ) =>
44- props . getMyActivities ( {
45- t : props . t ,
46- token : props . auth . token ,
47- } ) ,
48- } ;
49- useEffect ( async ( ) => {
38+
39+ useEffect ( ( ) => {
5040 setLoading ( true ) ;
51- if ( location . state ?. flag && flagMap [ location . state . flag ] ) {
52- await flagMap [ location . state . flag ] ( ) ;
53- } else {
54- await props . getActivities ( props . t ) ;
41+ async function getActivityList ( ) {
42+ if ( props . auth ?. tags . includes ( 'staff' ) ) {
43+ await props . getUnPublishedActivities ( {
44+ t : props . t ,
45+ token : props . auth . token ,
46+ } )
47+ } else if ( props . auth ?. tags . includes ( 'educator' ) ) {
48+ await props . getMyActivities ( {
49+ t : props . t ,
50+ token : props . auth . token ,
51+ } )
52+ }
53+ await props . getActivities ( props . t )
5554 }
56- setActivityList ( activities . all_activities ) ;
55+ getActivityList ( )
5756 setLoading ( false ) ;
58- } , [ location ] ) ;
57+ } , [ ] ) ;
5958
6059 const handleTabChange = ( event , newTab ) => {
6160 setTab ( newTab )
6261 }
6362
63+ const ActivityCard = ( { activity } ) => {
64+ return (
65+ < Grid
66+ key = { activity . id }
67+ item
68+ xs = { 12 }
69+ sm = { 6 }
70+ lg = { 4 }
71+ align = "center"
72+ className = { classes . activityBoxContainer }
73+ >
74+ < Activity
75+ key = { activity . id }
76+ activity = { activity }
77+ auth = { props . auth }
78+ activityToggleSave = { props . activityToggleSave }
79+ t = { props . t }
80+ navigate = { props . navigate }
81+ />
82+ </ Grid >
83+ )
84+ }
85+
6486 if ( loading ) {
6587 return < LoadingPage /> ;
6688 } else {
@@ -72,39 +94,46 @@ function Activities(props) {
7294 < Typography className = { commonClasses . title1 } >
7395 { t ( 'activities.title' ) }
7496 </ Typography >
75- < Tabs
76- value = { tab }
77- onChange = { handleTabChange }
78- aria-label = { t ( 'activities.tabs.ariaLabel' ) }
79- indicatorColor = "primary"
80- variant = "fullWidth"
81- className = { classes . tabs }
82- >
83- < Tab value = "published" label = { `${ t ( 'activities.tabs.published' ) } (${ activityList . length } )` } className = { classes . tab } />
84- < Tab value = "unpublished" label = { `${ t ( 'activities.tabs.unpublished' ) } (${ activityList . length } )` } className = { classes . tab } />
85- </ Tabs >
86- < Grid container spacing = { 3 } >
87- { activityList &&
88- activityList . map ( ( activity , index ) => (
89- < Grid
90- key = { `activityContainer-${ index } ` }
91- item
92- xs = { 12 }
93- sm = { 6 }
94- lg = { 4 }
95- align = "center"
96- className = { classes . activityBoxContainer }
97- >
98- < Activity
99- key = { `activity-${ index } ` }
100- activity = { activity }
101- auth = { props . auth }
102- activityToggleSave = { props . activityToggleSave }
103- t = { props . t }
104- navigate = { props . navigate }
105- />
106- </ Grid >
97+ { ( props . auth ?. tags . includes ( 'staff' ) || props . auth ?. tags . includes ( 'educator' ) ) && (
98+ < Tabs
99+ value = { tab }
100+ onChange = { handleTabChange }
101+ aria-label = { t ( 'activities.tabs.ariaLabel' ) }
102+ indicatorColor = "primary"
103+ variant = "fullWidth"
104+ className = { classes . tabs }
105+ >
106+ < Tab
107+ value = "published"
108+ label = { `${ t ( 'activities.tabs.published' ) } (${ activityList . published ?. length } )` }
109+ className = { classes . tab }
110+ />
111+ < Tab
112+ value = "unpublished"
113+ label = {
114+ `${ t ( 'activities.tabs.unpublished' ) } (${ props . auth ?. tags . includes ( 'staff' ) ?
115+ activityList . unPublishedActivities ?. length :
116+ activityList . userActivities . filter ( activity => ! activity . publish ) . length } )
117+ ` }
118+ className = { classes . tab }
119+ />
120+ </ Tabs >
121+ ) }
122+ < Grid container spacing = { 3 } className = { classes . activitiesContainer } >
123+ { activityList . published && tab === "published" &&
124+ activityList . published . map ( ( activity ) => (
125+ < ActivityCard activity = { activity } key = { activity . id } />
107126 ) ) }
127+ { activityList . unPublishedActivities && tab === "unpublished" && props . auth ?. tags . includes ( 'staff' ) &&
128+ activityList . unPublishedActivities . map ( activity => (
129+ < ActivityCard activity = { activity } key = { activity . id } />
130+ ) )
131+ }
132+ { activityList . userActivities && tab === "unpublished" && props . auth ?. tags . includes ( 'educator' ) &&
133+ activityList . userActivities . filter ( activity => ! activity . publish ) . map ( activity => (
134+ < ActivityCard activity = { activity } key = { activity . id } />
135+ ) )
136+ }
108137 </ Grid >
109138 </ div >
110139 ) ;
0 commit comments