1
1
import React , { useEffect , useState } from 'react' ;
2
2
import { connect , useSelector } from 'react-redux' ;
3
- import { useLocation } from 'react-router-dom' ;
4
3
import {
5
4
getActivities ,
6
5
getMyActivities ,
@@ -21,46 +20,69 @@ import LoadingPage from '../loading/LoadingPage';
21
20
const useStyles = makeStyles ( styles ) ;
22
21
23
22
function Activities ( props ) {
24
- const location = useLocation ( ) ;
25
23
const classes = useStyles ( ) ;
26
24
const [ loading , setLoading ] = useState ( true ) ;
27
25
const { activities } = useSelector ( state => state ) ;
28
- let [ activityList , setActivityList ] = useState ( [ ] ) ;
26
+ let [ activityList , setActivityList ] = useState ( {
27
+ published : [ ] ,
28
+ unPublishedActivities : [ ] ,
29
+ userActivities : [ ]
30
+ } ) ;
29
31
const [ tab , setTab ] = useState ( "published" ) ;
30
32
const commonClasses = makeStyles ( DefaultStyles ) ( ) ;
31
33
const { t } = useTranslation ( ) ;
32
34
33
35
useEffect ( ( ) => {
34
- setActivityList ( activities . all_activities ) ;
36
+ setActivityList ( activities ) ;
35
37
} , [ 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 ( ( ) => {
50
40
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 )
55
54
}
56
- setActivityList ( activities . all_activities ) ;
55
+ getActivityList ( )
57
56
setLoading ( false ) ;
58
- } , [ location ] ) ;
57
+ } , [ ] ) ;
59
58
60
59
const handleTabChange = ( event , newTab ) => {
61
60
setTab ( newTab )
62
61
}
63
62
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
+
64
86
if ( loading ) {
65
87
return < LoadingPage /> ;
66
88
} else {
@@ -72,39 +94,46 @@ function Activities(props) {
72
94
< Typography className = { commonClasses . title1 } >
73
95
{ t ( 'activities.title' ) }
74
96
</ 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 } />
107
126
) ) }
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
+ }
108
137
</ Grid >
109
138
</ div >
110
139
) ;
0 commit comments