-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from daithihearn/feature/admin-game-stats
Adding switched for games stats
- Loading branch information
Showing
3 changed files
with
117 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useCallback, useEffect, useState } from "react" | ||
import { | ||
Dropdown, | ||
DropdownItem, | ||
DropdownMenu, | ||
DropdownToggle, | ||
} from "reactstrap" | ||
import { useAppDispatch, useAppSelector } from "../../caches/hooks" | ||
import { getMyProfile } from "../../caches/MyProfileSlice" | ||
import { getPlayerProfiles } from "../../caches/PlayerProfilesSlice" | ||
import { PlayerProfile } from "../../model/Player" | ||
import StatsService from "../../services/StatsService" | ||
|
||
const PlayerSwitcher: React.FC = () => { | ||
const dispatch = useAppDispatch() | ||
const myProfile = useAppSelector(getMyProfile) | ||
const players = useAppSelector(getPlayerProfiles) | ||
const [showDropdown, setShowDropdown] = useState(false) | ||
const [currentPlayer, setCurrentPlayer] = useState<PlayerProfile>() | ||
|
||
const toggleDropdown = useCallback( | ||
() => setShowDropdown(!showDropdown), | ||
[showDropdown], | ||
) | ||
|
||
useEffect(() => { | ||
const me = players.find(p => p.id === myProfile.id) | ||
if (me) setCurrentPlayer(me) | ||
}, [myProfile]) | ||
|
||
useEffect(() => { | ||
dispatch(StatsService.gameStatsForPlayer(currentPlayer?.id)) | ||
}, [currentPlayer]) | ||
|
||
return ( | ||
<Dropdown isOpen={showDropdown} toggle={toggleDropdown}> | ||
<DropdownToggle data-toggle="dropdown" tag="span"> | ||
<img | ||
alt={currentPlayer ? currentPlayer.name : ""} | ||
src={currentPlayer ? currentPlayer.picture : ""} | ||
className="avatar clickable" | ||
onClick={() => setShowDropdown(true)} | ||
/> | ||
</DropdownToggle> | ||
<DropdownMenu> | ||
{players.map(p => { | ||
return ( | ||
<DropdownItem | ||
key={p.id} | ||
onClick={() => setCurrentPlayer(p)}> | ||
{p.name} | ||
</DropdownItem> | ||
) | ||
})} | ||
</DropdownMenu> | ||
</Dropdown> | ||
) | ||
} | ||
|
||
export default PlayerSwitcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters