2022-04-05 02:19:08 -04:00
import React from "react" ;
2022-04-05 13:16:09 -04:00
import { Button , InputGroup , FormControl } from "react-bootstrap" ;
import "../styles/Dashboard.css" ;
2022-04-05 14:52:19 -05:00
import { apiClient } from "../utils/httpClients.js" ;
2022-04-05 14:51:15 -04:00
import MatchInfoCardDisplay from "../components/MatchInfoCardDisplay" ;
2022-04-05 14:52:19 -05:00
import { needUser } from "../utils/routing.js" ;
export default class Dashboard extends React . Component {
2022-04-05 02:19:08 -04:00
constructor ( props ) {
super ( props ) ;
2022-04-05 13:16:09 -04:00
this . state = {
displayedMatches : [ ] ,
2022-04-05 14:51:15 -04:00
displayedSports : [ ] ,
2022-04-05 14:52:19 -05:00
displayedEquipment : [ ] ,
user : null
2022-04-05 13:16:09 -04:00
} ;
2022-04-05 14:51:15 -04:00
this . getFirstName ( ) ;
}
async componentDidMount ( ) {
2022-04-05 14:52:19 -05:00
this . setState ( { user : await needUser ( ) } ) ; // needUser says this page needs a user, and therefore, if there isn't a user, get them to login first. It returns the authenticated user.
this . setState ( { displayedMatches : await this . latestMatches ( ) } ) ;
2022-04-05 13:16:09 -04:00
}
2022-04-05 14:51:15 -04:00
async latestMatches ( ) {
let recentMatchesRes = await apiClient . get ( "/match/recent/15" ) ;
if ( recentMatchesRes . status === 200 ) {
this . setState ( { displayedMatches : recentMatchesRes . data . recent } ) ;
}
}
async availableMatches ( ) {
let availableMatchesRes = await apiClient . get ( "/sports" ) ;
if ( availableMatchesRes . status === 200 ) {
this . setState ( { displayedSports : availableMatchesRes . data . recent } ) ;
}
}
async availableEquipment ( ) {
let availableEquipmentRes = await apiClient . get ( "/rentals" ) ;
if ( availableEquipmentRes . status === 200 ) {
this . setState ( { displayedEquipment : availableEquipmentRes . data . recent } ) ;
}
}
2022-04-05 14:52:19 -05:00
async getFirstName ( ) {
2022-04-05 14:51:15 -04:00
// let result = await apiClient.post("/user/login", {"email": "johndoe@gmail.com", "password": "csc309h1"}).then(apiClient.get("/user"));
2022-04-05 13:16:09 -04:00
let user = await apiClient . get ( "/user" ) ;
2022-04-05 14:51:15 -04:00
let tags = document . getElementsByTagName ( "h1" ) ;
tags [ 0 ] . innerHTML = user . firstName ;
2022-04-05 02:19:08 -04:00
}
render ( ) {
2022-04-05 14:52:19 -05:00
return (
2022-04-05 13:16:09 -04:00
< React . Fragment >
2022-04-05 14:51:15 -04:00
< h1 > < / h 1 >
2022-04-05 13:16:09 -04:00
< InputGroup className = "w-50" >
< FormControl
placeholder = "Search for Matches"
aria - label = "Search Bar"
aria - describedby = "basic-addon2"
/ >
< Button variant = "outline-secondary" id = "button-addon2" >
2022-04-05 14:52:19 -05:00
Search
2022-04-05 13:16:09 -04:00
< / B u t t o n >
< / I n p u t G r o u p >
2022-04-05 14:51:15 -04:00
< div className = "p-4" >
< h2 > Available Matches < / h 2 >
< MatchInfoCardDisplay recommendedmatches = { this . state . displayedMatches } / >
< / d i v >
< div className = "p-4" >
< h2 > Available Sports < / h 2 >
< MatchInfoCardDisplay recommendedmatches = { this . state . displayedSports } / >
< / d i v >
< div className = "p-4" >
< h2 > Available Equipment < / h 2 >
< MatchInfoCardDisplay recommendedmatches = { this . state . displayedEquipment } / >
< / d i v >
2022-04-05 13:16:09 -04:00
< / R e a c t . F r a g m e n t >
) ;
2022-04-05 02:19:08 -04:00
}
}