Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Champs page#68 #71

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
10 changes: 9 additions & 1 deletion src/App/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Container } from '@material-ui/core'
import { FC } from 'react'
import { BrowserRouter, Route, Switch } from 'react-router-dom'
import { ChampInfo } from '../ChampInfo/ChampInfo'
import { ComparePage } from '../ComparePage/ComparePage'
import { ConfigPage } from '../ConfigPage/ConfigPage'
import { DisplayChampsPage } from '../DisplayChampsPage/DisplayChampsPage'
import { DisplayUsersPage } from '../DisplayUsersPage/DisplayUsersPage'
import { Footer } from '../Footer/Footer'
import { GlobalContextProvider } from '../GlobalContext'
Expand All @@ -20,7 +22,7 @@ const App: FC = () => {
<GlobalContextProvider>
<BrowserRouter>
<Nav />
<Container maxWidth="sm" className="app">
<Container maxWidth={false} className="app">
{/* Content outside of <Switch> renders on every page */}
<Switch>
<Route path="/" exact>
Expand All @@ -47,6 +49,12 @@ const App: FC = () => {
<Route path="/compare" exact>
<ComparePage />
</Route>
<Route path="/champs" exact>
<DisplayChampsPage />
</Route>
<Route path="/champInfo/:champId" exact>
<ChampInfo />
</Route>
<Route path="/">
<PageNotFoundPage />
</Route>
Expand Down
54 changes: 54 additions & 0 deletions src/ChampDisplay/ChampDisplay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Container, Link, makeStyles, Theme } from '@material-ui/core'
import React, { FC } from 'react'
import { theme } from '../theme'
import { IChampDisplay } from '../types/IChampDisplay'
import { Link as RouterLink } from 'react-router-dom'

interface ChampDisplayProps {
champInfo: IChampDisplay
}

const useStyles = makeStyles({
champDisplayContainer: (theme: Theme) => ({
display: 'flex',
flexDirection: 'column',
textAlign: 'center',
justifyContent: 'center',
marginTop: theme.spacing(3),
color: 'white',
}),
})

export const ChampDisplay: FC<ChampDisplayProps> = ({ champInfo: champ }) => {
const classes = useStyles(theme)
const imgUrl = `http://ddragon.leagueoflegends.com/cdn/${champ.version}/img/sprite/${champ.image.sprite}`

const newTitle = champ.title[0]
.toUpperCase()
.concat(champ.title.substring(1))
return (
<Container className={classes.champDisplayContainer}>
<Link
component={RouterLink}
to={`/champInfo/${champ.id}`}
rel="noopener noreferrer"
role="link"
>
<Container
style={{
width: 48,
height: 48,
backgroundPositionX: -champ.image.x,
backgroundPositionY: -champ.image.y,
backgroundImage: `url(${imgUrl})`,
transform: 'scale(1.5) translate(0px, -10px)',
}}
>
<div />
</Container>
</Link>
<Container>{champ.name}</Container>
<Container>{newTitle}</Container>
</Container>
)
}
47 changes: 47 additions & 0 deletions src/ChampInfo/ChampInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Container, makeStyles, Theme } from '@material-ui/core'
import React, { FC, useCallback, useContext, useEffect, useState } from 'react'
import { useParams } from 'react-router-dom'
import { GlobalContext } from '../GlobalContext'
import { theme } from '../theme'
import { IChampInfo } from '../types/IChampInfo'

interface IChampRouteProps {
champId: string
}

const useStyles = makeStyles({
champInfoContainer: (theme: Theme) => ({
display: 'flex',
flexDirection: 'column',
textAlign: 'center',
justifyContent: 'center',
color: 'white',
}),
})

export const ChampInfo: FC<any> = () => {
const classes = useStyles(theme)
const props = useParams<IChampRouteProps>()
const { api, setTitle } = useContext(GlobalContext) // for page title and API access
const [champ, setChamp] = useState<IChampInfo>()

const fireGetChampInfo = useCallback(async () => {
setChamp(await api.getChampInfo(props.champId))
}, [api])

useEffect(() => {
setTitle(props.champId)
fireGetChampInfo()
console.log(champ)
}, [setTitle, fireGetChampInfo])

if (!champ) {
return <></>
}

return (
<Container className={classes.champInfoContainer}>
{champ.lore}
</Container>
)
}
4 changes: 2 additions & 2 deletions src/ComparePage/ComparePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ const ComparePage: FC = () => {

return (
// Search Section: Layout; 2 Selects for user lists, 1 Select for # of games
<Container>
<Container maxWidth="md">
{/* Search Section */}
<Container className={classes.searchContainer}>
<Container maxWidth="sm" className={classes.searchContainer}>
<Typography>Games:</Typography>
<Select
onChange={gamesHandleChange}
Expand Down
59 changes: 59 additions & 0 deletions src/DisplayChampsPage/DisplayChampsPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Container, Grid, makeStyles, Theme } from '@material-ui/core'
import React, { FC, useCallback, useContext, useEffect, useState } from 'react'
import { ChampDisplay } from '../ChampDisplay/ChampDisplay'
import { GlobalContext } from '../GlobalContext'
import { theme } from '../theme'

const useStyles = makeStyles({
errorContainer: (theme: Theme) => ({
display: 'flex',
justifyContent: 'center',
marginTop: theme.spacing(3),
}),
champsContainer: (theme: Theme) => ({
marginTop: theme.spacing(3),
}),
})

// get version from https://ddragon.leagueoflegends.com/api/versions.json
// store first value as latest version
// Make Individual champ displays
// Make API method to get all champs based on version
// all champs json http://ddragon.leagueoflegends.com/cdn/12.1.1/data/en_US/champion.json
// add latest version to sprite sheet url ex http://ddragon.leagueoflegends.com/cdn/12.1.1/img/sprite/champion0.png
// Get champ data from http://ddragon.leagueoflegends.com/cdn/12.1.1/data/en_US/champion/Aatrox.json (sub in champ name)
// Get champ sprite sheet image position from champ data

export const DisplayChampsPage: FC = () => {
const classes = useStyles(theme)
const { api, setTitle } = useContext(GlobalContext) // for page title and API access
const [champs, setChamps] = useState<any[]>([])

const fireGetChamps = useCallback(async () => {
setChamps(await api.getAllChamps())
}, [api])

useEffect(() => {
setTitle('Champions')
fireGetChamps()
}, [setTitle, fireGetChamps])
return (
<Container maxWidth={false} className={classes.champsContainer}>
<Grid container spacing={5}>
{champs.map((champ) => (
<Grid
key={champ.id}
item
xs={12}
sm={6}
md={4}
lg={3}
xl={2}
>
<ChampDisplay champInfo={champ} />
</Grid>
))}
</Grid>
</Container>
)
}
2 changes: 2 additions & 0 deletions src/Nav/Nav.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('Nav component', () => {
'/search',
'/users',
'/compare',
'/champs',
]
const expectedLinkTexts = [
'Home',
Expand All @@ -56,6 +57,7 @@ describe('Nav component', () => {
'Search Users',
'All Users',
'Compare Users',
'Champions',
]

beforeEach(() => {
Expand Down
17 changes: 16 additions & 1 deletion src/Nav/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,28 @@ const Nav: FC = () => {
}}
>
<Link
href="/compare"
component={RouterLink}
to="/compare"
rel="noopener noreferrer"
role="link"
>
Compare Users
</Link>
</MenuItem>
<MenuItem
onClick={() => {
handleClose()
}}
>
<Link
component={RouterLink}
to="/champs"
rel="noopener noreferrer"
role="link"
>
Champions
</Link>
</MenuItem>
</Menu>
<Typography
aria-roledescription="title of page"
Expand Down
70 changes: 69 additions & 1 deletion src/services/ApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
HTTP_POST,
NO_CACHE,
} from '../constants'
import { IUser, IUserStats } from '../types'
import { IChampInfo, IUser, IUserStats } from '../types'

/**
* This service encapsulates API communication in a centralized location
Expand Down Expand Up @@ -212,6 +212,74 @@ class ApiService {
})
}

/**
* Use the fetch API over HTTP to hit the get user stats endpoint
*
* @returns Promise that resolves latest version string; otherwise, Promise that resolves to empty string
*/
getLatestDragonVersion(): Promise<string> {
let versionUrl = 'https://ddragon.leagueoflegends.com/api/versions.json'
return fetch(versionUrl, {
cache: NO_CACHE,
method: HTTP_GET,
})
.then((resp) => resp.json())
.then((allVersions) => allVersions[0])
.catch((err) => {
this.handleError(err)

return Promise.resolve('')
})
}

/**
* Use the fetch API over HTTP to hit the get user stats endpoint
*
* @returns Promise that resolves all champs objects; otherwise, Promise that resolves to empty array
*/
async getAllChamps(): Promise<any[]> {
let version = await this.getLatestDragonVersion()
let versionUrl = `http://ddragon.leagueoflegends.com/cdn/${version}/data/en_US/champion.json`
return fetch(versionUrl, {
cache: NO_CACHE,
method: HTTP_GET,
})
.then((resp) => resp.json())
.then((allChamps) => {
return Object.keys(allChamps.data).map((champName) => {
return allChamps.data[champName]
})
})
.catch((err) => {
this.handleError(err)

return Promise.resolve([])
})
}

/**
* Use the fetch API over HTTP to hit the get user stats endpoint
*
* @returns Promise that resolves champ info; otherwise, Promise that resolves to empty object
*/
async getChampInfo(champId: string): Promise<IChampInfo> {
let version = await this.getLatestDragonVersion()
let versionUrl = `http://ddragon.leagueoflegends.com/cdn/${version}/data/en_US/champion/${champId}.json`
return fetch(versionUrl, {
cache: NO_CACHE,
method: HTTP_GET,
})
.then((resp) => resp.json())
.then((champInfo) => {
return champInfo.data[champId]
})
.catch((err) => {
this.handleError(err)

return Promise.resolve({})
})
}

private handleError(err: Error) {
console.error(err)
alert(`There was a problem -\n\n${err.message}`)
Expand Down
14 changes: 14 additions & 0 deletions src/types/IChampDisplay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IChampImage } from './IChampImage'

export interface IChampDisplay {
version: string
id: string
key: string
name: string
title: string
blurb: string
info: []
image: IChampImage
tags: []
partype: string
}
9 changes: 9 additions & 0 deletions src/types/IChampImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface IChampImage {
full: string
sprite: string
group: string
x: number
y: number
w: number
h: number
}
Loading