1- import React from "react" ;
1+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2+ import React , { useEffect , useState } from "react" ;
23import SwipeableViews from "react-swipeable-views" ;
34import { makeStyles , useTheme } from "@material-ui/core/styles" ;
45import AppBar from "@material-ui/core/AppBar" ;
@@ -9,6 +10,9 @@ import MeetingCard from "./meetingStuff/MeetingCard";
910import "./MeetingTabs.css" ;
1011import PollCard from "./pollStuff/PollCard" ;
1112import CreateButton from "./CreateButton" ;
13+ import useUser from "src/customHooks/useUser" ;
14+ import useMeetingData from "src/customHooks/useMeetingData" ;
15+ import usePollData from "src/customHooks/usePollData" ;
1216
1317interface TabPanelProps {
1418 children ?: React . ReactNode ;
@@ -58,10 +62,64 @@ const useStyles = makeStyles(() => ({
5862 } ,
5963} ) ) ;
6064
65+ type clubType = { authority : "admin" | "member" ; _id : string ; name : string } ;
66+ export type pollType = {
67+ voter : string [ ] ;
68+ _id : string ;
69+ question : string ;
70+ club : string ;
71+ createdAt : string ;
72+ updatedAt : string ;
73+ options : {
74+ votes : number ;
75+ _id : string ;
76+ name : string ;
77+ } [ ] ;
78+ } ;
79+ export type meetingType = {
80+ _id : string ;
81+ title : string ;
82+ club : string ;
83+ datetime : string ;
84+ description : string ;
85+ registered : {
86+ userId : number ;
87+ _id : string ;
88+ name : string ;
89+ email : string ;
90+ } [ ] ;
91+ } ;
92+
93+ // Returns an array of all clubs in which he is admin
94+ const getAdminClubs = ( arr : clubType [ ] ) => {
95+ const res = [ ] ;
96+ for ( let i = 0 ; i < arr . length ; i ++ ) {
97+ const e = arr [ i ] ;
98+ if ( e . authority === "admin" ) {
99+ res . push ( e . name ) ;
100+ }
101+ }
102+ return res ;
103+ } ;
104+
61105const MeetingTabs : React . FC = ( ) => {
62106 const classes = useStyles ( ) ;
63107 const theme = useTheme ( ) ;
64108 const [ value , setValue ] = React . useState ( 0 ) ;
109+ const { userData } = useUser ( ) ;
110+ const { meetingData } = useMeetingData ( ) ;
111+ const { pollData } = usePollData ( ) ;
112+ const [ meetings , setMeetings ] = useState < meetingType [ ] | undefined > ( ) ;
113+ const [ polls , setPolls ] = useState < pollType [ ] | undefined > ( ) ;
114+
115+ const [ adminOfClubs , setAdminsOfClubs ] = React . useState < string [ ] > ( [ ] ) ;
116+
117+ useEffect ( ( ) => {
118+ if ( userData && userData . done )
119+ setAdminsOfClubs ( getAdminClubs ( userData . data . clubs ) ) ;
120+ if ( meetingData ) setMeetings ( meetingData . data ) ;
121+ if ( pollData ) setPolls ( pollData . data ) ;
122+ } , [ userData , meetingData , pollData ] ) ;
65123
66124 // eslint-disable-next-line @typescript-eslint/ban-types
67125 const handleChange = ( event : React . ChangeEvent < { } > , newValue : number ) => {
@@ -72,6 +130,10 @@ const MeetingTabs: React.FC = () => {
72130 setValue ( index ) ;
73131 } ;
74132
133+ if ( ! adminOfClubs || ! meetings || ! polls ) {
134+ return < div > Loading..</ div > ;
135+ }
136+
75137 return (
76138 < div className = { classes . root } >
77139 < AppBar className = { classes . tabbar } position = "static" color = "default" >
@@ -92,18 +154,25 @@ const MeetingTabs: React.FC = () => {
92154 onChangeIndex = { handleChangeIndex }
93155 >
94156 < TabPanel value = { value } index = { 0 } dir = { theme . direction } >
95- < MeetingCard />
96- < MeetingCard />
97- < MeetingCard />
98- < MeetingCard />
99- < MeetingCard />
100- < MeetingCard />
157+ { meetings ! . map ( ( e , key ) => (
158+ < MeetingCard
159+ key = { key }
160+ meetingData = { e }
161+ isAdmin = { adminOfClubs . includes ( e . club ) }
162+ />
163+ ) ) }
101164 </ TabPanel >
102165 < TabPanel value = { value } index = { 1 } dir = { theme . direction } >
103- < PollCard />
166+ { polls ! . map ( ( e , key ) => (
167+ < PollCard
168+ key = { key }
169+ pollData = { e }
170+ isAdmin = { adminOfClubs . includes ( e . club ) }
171+ />
172+ ) ) }
104173 </ TabPanel >
105174 </ SwipeableViews >
106- < CreateButton formType = { value } />
175+ { adminOfClubs . length > 0 ? < CreateButton formType = { value } /> : null }
107176 </ div >
108177 ) ;
109178} ;
0 commit comments