front: isCurrentUser()
This commit is contained in:
parent
8a0401ec59
commit
6c46188d38
@ -0,0 +1,32 @@
|
|||||||
|
package djmil.cordacheckers.api;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import djmil.cordacheckers.cordaclient.CordaClient;
|
||||||
|
import djmil.cordacheckers.user.HoldingIdentityResolver;
|
||||||
|
import djmil.cordacheckers.user.User;
|
||||||
|
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("api/user")
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
CordaClient cordaClient;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
HoldingIdentityResolver holdingIdentityResolver;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity<User> getUserInfo(
|
||||||
|
@AuthenticationPrincipal User player
|
||||||
|
) {
|
||||||
|
return ResponseEntity.ok(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,3 +3,7 @@
|
|||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tr.username {
|
||||||
|
background-color:aliceblue;
|
||||||
|
}
|
||||||
|
@ -20,7 +20,7 @@ export default function Leaderboard() {
|
|||||||
const tableRows = Object.keys(data.leaderboard).map(playerName => {
|
const tableRows = Object.keys(data.leaderboard).map(playerName => {
|
||||||
var rank = data.leaderboard[playerName];
|
var rank = data.leaderboard[playerName];
|
||||||
|
|
||||||
return <tr key={playerName}>
|
return <tr key={playerName} className={data.isCurrentUser(playerName) && 'username'}>
|
||||||
<td>{playerName}</td>
|
<td>{playerName}</td>
|
||||||
<td>{rank.gamesPlayed}</td>
|
<td>{rank.gamesPlayed}</td>
|
||||||
<td>{rank.gamesWon}</td>
|
<td>{rank.gamesWon}</td>
|
||||||
|
@ -11,7 +11,7 @@ import { useState, useCallback, useEffect, } from "react"
|
|||||||
|
|
||||||
export default function Poll(url, interval_sec, offlineMode) {
|
export default function Poll(url, interval_sec, offlineMode) {
|
||||||
const [dataCache, setDataCache] = useState(null)
|
const [dataCache, setDataCache] = useState(null)
|
||||||
const [fetching, setFetching] = useState(false)
|
const [fetching , setFetching ] = useState(false)
|
||||||
const [timeoutID, setTimeoutID] = useState(null)
|
const [timeoutID, setTimeoutID] = useState(null)
|
||||||
|
|
||||||
const fecthData = useCallback(() => {
|
const fecthData = useCallback(() => {
|
||||||
@ -35,7 +35,7 @@ export default function Poll(url, interval_sec, offlineMode) {
|
|||||||
clearTimeout(timeoutID) // cancel already scheduled fetch
|
clearTimeout(timeoutID) // cancel already scheduled fetch
|
||||||
setTimeoutID(null) // & stop interval fetching
|
setTimeoutID(null) // & stop interval fetching
|
||||||
}
|
}
|
||||||
else if (timeoutID === null) {
|
else if (timeoutID === null && typeof interval_sec === 'number') {
|
||||||
const timeoutID = setTimeout(fecthData, interval_sec * 1000)
|
const timeoutID = setTimeout(fecthData, interval_sec * 1000)
|
||||||
setTimeoutID(timeoutID)
|
setTimeoutID(timeoutID)
|
||||||
console.log("Fetch '" +url +"' scheduled in " +interval_sec +" sec")
|
console.log("Fetch '" +url +"' scheduled in " +interval_sec +" sec")
|
||||||
|
@ -14,12 +14,17 @@ export const AppDataProvider = ({ children }) => {
|
|||||||
|
|
||||||
const [games, gamesFetching ] = Poll('/api/gamestate' , 30, data.offlineMode)
|
const [games, gamesFetching ] = Poll('/api/gamestate' , 30, data.offlineMode)
|
||||||
const [leaderboard, leaderboardFetching ] = Poll('/api/leaderboard', 60, data.offlineMode)
|
const [leaderboard, leaderboardFetching ] = Poll('/api/leaderboard', 60, data.offlineMode)
|
||||||
|
const [user] = Poll('api/user') // once
|
||||||
|
|
||||||
data.games = games
|
data.games = games
|
||||||
data.gamesFetching = gamesFetching
|
data.gamesFetching = gamesFetching
|
||||||
|
|
||||||
data.leaderboard = leaderboard
|
data.leaderboard = leaderboard
|
||||||
data.leaderboardFetching = leaderboardFetching
|
data.leaderboardFetching = leaderboardFetching
|
||||||
|
|
||||||
|
data.isCurrentUser = (otherUsername) => {
|
||||||
|
return user?.username && ciEquals(user.username, otherUsername) ? true : null
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppData.Provider value={[data, dispatchData]}>
|
<AppData.Provider value={[data, dispatchData]}>
|
||||||
@ -27,3 +32,9 @@ export const AppDataProvider = ({ children }) => {
|
|||||||
</AppData.Provider>
|
</AppData.Provider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function ciEquals(a, b) {
|
||||||
|
return typeof a === 'string' && typeof b === 'string'
|
||||||
|
? a.localeCompare(b, undefined, { sensitivity: 'accent' }) === 0
|
||||||
|
: a === b;
|
||||||
|
}
|
@ -19,5 +19,7 @@ export const initialState = {
|
|||||||
leaderboard: null,
|
leaderboard: null,
|
||||||
leaderboardFetching: false,
|
leaderboardFetching: false,
|
||||||
|
|
||||||
|
isCurrentUser: () => false,
|
||||||
|
|
||||||
offlineMode: false
|
offlineMode: false
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user