Skip to content

Commit

Permalink
feat(tournaments-frontend): List tournaments page
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstojda committed Aug 8, 2023
1 parent ea2efe8 commit 2ca7624
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 8 deletions.
6 changes: 5 additions & 1 deletion web/app/src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {AuthApi, Configuration, ErrorResponse, LeaguesApi, LocationsApi, UserLogin, UsersApi} from "./generated";
import {AuthApi, Configuration, ErrorResponse, LeaguesApi, LocationsApi, UserLogin, UsersApi, TournamentsApi} from "./generated";
import {AxiosError} from "axios";

export const TOKEN_KEY = 'token';
Expand Down Expand Up @@ -113,6 +113,10 @@ export class Api {
return new LocationsApi(this.configuration());
}

public tournamentsApi() {
return new TournamentsApi(this.configuration());
}

public parseError(e: AxiosError): ErrorResponse {
if (e.isAxiosError && e.response && e.response.data)
return e.response.data as ErrorResponse
Expand Down
2 changes: 2 additions & 0 deletions web/app/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import theme from "./theme"
import AuthTest from "./pages/AuthTest";
import SignUpPage from "./pages/SignUp";
import LeagueListPage from "./pages/Leagues";
import TournamentsListPage from "./pages/Tournaments";

const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
Expand All @@ -28,6 +29,7 @@ function Router() {
<BrowserRouter basename={'/app'}>
<Routes>
<Route path="/" element={<Navigate to={'/leagues'}/>}/>
<Route path={"/tournaments"} element={<TournamentsListPage/>} />
<Route path="/leagues" element={<LeagueListPage/>}/>
<Route path={"/leagues/create"} element={<LeagueListPage createFormOpen={true} />}/>
<Route path={"/login"} element={<Login/>}/>
Expand Down
19 changes: 12 additions & 7 deletions web/app/src/pages/Leagues/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Button,
Card,
CardBody,
CardHeader,
CardHeader, Flex,
Grid,
GridItem,
Heading,
Expand All @@ -16,7 +16,8 @@ import {
ModalContent,
ModalHeader,
ModalOverlay,
Spacer, Text,
Spacer,
Text,
} from "@chakra-ui/react";
import {useNavigate} from "react-router-dom";
import {AxiosError} from "axios";
Expand Down Expand Up @@ -77,12 +78,16 @@ export default function LeaguesPage(props: LeagueListPageProps) {
{leagues.length > 0 && leagues.map((league) => (
<GridItem key={league.id}>
<Card data-testid="league-card" direction={'row'}>
<CardHeader>
<Heading lineHeight={'2em'} mt={'0'} mb={'0'} verticalAlign={'middle'} size={'md'}>{league.name}</Heading>
<CardHeader display={'flex'} alignItems={"center"} justifyContent={"flex-end"}>
<Flex flexDirection="column" textAlign={'right'}>
<Heading size={'md'}>{league.name}</Heading>
</Flex>
</CardHeader>
<CardBody textAlign={'right'}>
<Text fontSize={'0.8em'}>{league.location.name}</Text>
<Text fontSize={'0.65em'}>{league.location.address}</Text>
<CardBody display="flex" alignItems="center" justifyContent="flex-end">
<Flex flexDirection="column" textAlign={'right'}>
<Text fontSize={'0.8em'}>{league.location!.name}</Text>
<Text fontSize={'0.65em'}>{league.location!.address}</Text>
</Flex>
</CardBody>
</Card>
</GridItem>
Expand Down
120 changes: 120 additions & 0 deletions web/app/src/pages/Tournaments/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import {
Box,
Button,
Card,
CardBody,
CardHeader,
Flex,
Grid,
GridItem,
Heading,
HStack,
Spacer,
Text
} from "@chakra-ui/react";
import Footer from "../../layouts/footer";
import {Api, Tournament, useAuth} from "../../api";
import {useNavigate} from "react-router-dom";
import Alert, {AlertData} from "../../components/Alert";
import {useEffect, useState} from "react";
import {AxiosError} from "axios";

const api: Api = new Api();

type TournamentListPageProps = {
createFormOpen?: boolean
}

export default function TournamentsListPage(props: TournamentListPageProps) {
const auth = useAuth({});
const navigate = useNavigate();
const [tournaments, setTournaments] = useState<Tournament[]>([])
const [alert, setAlert] = useState<AlertData>()

useEffect(() => {
api.tournamentsApi().tournamentsGet().then((getLeaguesResponse) => {
setTournaments(getLeaguesResponse.data.tournaments)
}).catch((e: AxiosError) => {
try {
const err = api.parseError(e)
console.error(err)
setAlert({
status: 'error',
title: "Failed to list leagues",
detail: err.detail
})
} catch (parseError) {
setAlert({
status: 'error',
title: "Failed to list leagues",
detail: 'Unknown error'
})
}
})
}, [props.createFormOpen])

function renderList() {
if (alert) {
return (
<Alert alert={alert}/>
)
}

if (!tournaments || tournaments.length === 0) {
return (
<p>No leagues found, try creating one!</p>
)
}
return (
<Grid gap={4}>
{tournaments.length > 0 && tournaments.map((tournament) => (
<GridItem key={tournament.id}>
<Card data-testid="tournament-card" direction={'row'}>
<CardHeader display="flex" alignItems="center" justifyContent="flex-end">
<Flex flexDirection="column" textAlign={'right'}>
<Heading size={'md'}>{tournament.name}</Heading>
{tournament.league &&
<Text fontSize={'0.8em'}>{tournament.league.name}</Text>
}
</Flex>
</CardHeader>
{tournament.location &&
<CardBody display="flex" alignItems="center" justifyContent="flex-end">
<Flex flexDirection="column" textAlign={'right'}>
<Text fontSize={'0.8em'}>{tournament.location.name}</Text>
<Text fontSize={'0.65em'}>{tournament.location.address}</Text>
</Flex>
</CardBody>
}
</Card>
</GridItem>
))}
</Grid>
)
}

return (
<Box fontSize="xl" w={['90%', '85%', '80%']} maxW={800} mx="auto">
<Box pb={10}>
<HStack py={5}>
<Heading
textAlign={'center'}
fontSize={'4xl'}
fontWeight={'bold'}>
Leagues
</Heading>
<Spacer/>
{auth?.user &&
<Button data-testid={'create-league'} size={'sm'} onClick={() => {
navigate('/leagues/create')
}}>
Create League
</Button>
}
</HStack>
{renderList()}
<Footer/>
</Box>
</Box>
)
}

0 comments on commit 2ca7624

Please sign in to comment.