From c90f35fff602fed16a11721ca6ef23cda9ef54e6 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Fri, 25 Sep 2020 17:57:46 -0700 Subject: [PATCH] Profiles and GridReps --- src/components/App/index.css | 12 +++ src/components/GridRep/index.css | 51 ++++++++++++ src/components/GridRep/index.tsx | 71 +++++++++++++++++ src/components/GridRepCollection/index.css | 6 ++ src/components/GridRepCollection/index.tsx | 15 ++++ src/components/Main/index.tsx | 2 +- src/components/WeaponGrid/index.css | 12 --- src/components/WeaponGrid/index.tsx | 71 +++++++---------- src/routes/Party/index.tsx | 49 +++++++++--- src/routes/Profile/index.tsx | 92 ++++++++++++++++++++-- src/types/GridWeapon.d.ts | 6 ++ 11 files changed, 311 insertions(+), 76 deletions(-) create mode 100644 src/components/GridRep/index.css create mode 100644 src/components/GridRep/index.tsx create mode 100644 src/components/GridRepCollection/index.css create mode 100644 src/components/GridRepCollection/index.tsx create mode 100644 src/types/GridWeapon.d.ts diff --git a/src/components/App/index.css b/src/components/App/index.css index 28994fc5..f62e3165 100644 --- a/src/components/App/index.css +++ b/src/components/App/index.css @@ -20,4 +20,16 @@ h1 { font-size: 18px; font-weight: 500; text-align: center; +} + +#NotFound { + display: flex; + flex-direction: column; + align-items: center; + margin-top: 40px; +} + +#NotFound h2 { + color: #444; + font-weight: 500; } \ No newline at end of file diff --git a/src/components/GridRep/index.css b/src/components/GridRep/index.css new file mode 100644 index 00000000..0e8c6ee0 --- /dev/null +++ b/src/components/GridRep/index.css @@ -0,0 +1,51 @@ +.GridRep { + border: 2px solid transparent; + border-radius: 6px; + display: flex; + flex-direction: row; + flex-shrink: 0; + margin: 0 8px 8px 0; + padding: 4px; + height: 148px; + width: 311px; +} + +.GridRep:hover { + border: 2px solid #61B3FF; + cursor: pointer; +} + +.GridRep .weapon { + background: white; + border-radius: 4px; +} + +.GridRep .grid_mainhand { + flex-shrink: 0; + height: 136px; + width: 65px; + margin-right: 8px; +} + +.GridRep .grid_weapons { + list-style: none; + padding: 0; +} + +.GridRep .grid_weapon { + float: left; + margin: 0 8px 8px 0; + height: 40px; + width: 70px; +} + +.GridRep .grid_weapon:nth-child(3n+3) { + margin-right: 0; +} + +.GridRep .weapon img[src*="jpg"] { + border-radius: 4px; + height: 100%; + width: 100%; +} + diff --git a/src/components/GridRep/index.tsx b/src/components/GridRep/index.tsx new file mode 100644 index 00000000..6352e5c2 --- /dev/null +++ b/src/components/GridRep/index.tsx @@ -0,0 +1,71 @@ +import React, { useEffect, useState } from 'react' + +import './index.css' + +import mainhandImages from '../../images/mainhand/*.jpg' +import gridImages from '../../images/grid/*.jpg' + +interface Props { + shortcode: string + grid: GridWeapon[] + onClick: (shortcode: string) => void +} + +const GridRep = (props: Props) => { + const numWeapons: number = 9 + + const [mainhand, setMainhand] = useState() + const [weapons, setWeapons] = useState({}) + + useEffect(() => { + configure() + }, []) + + function configure() { + const newWeapons = Array(numWeapons) + + for (const [key, value] of Object.entries(props.grid)) { + if (value.position == -1) + setMainhand(value.weapon) + else if (!value.mainhand && value.position != null) + newWeapons[value.position] = value.weapon + } + + setWeapons(newWeapons) + } + + function navigate() { + props.onClick(props.shortcode) + } + + return ( +
+
+ +
+ +
    + { + Array.from(Array(numWeapons)).map((x, i) => { + console.log() + return ( +
  • + +
  • + ) + }) + } +
+
+ ) +} + +export default GridRep diff --git a/src/components/GridRepCollection/index.css b/src/components/GridRepCollection/index.css new file mode 100644 index 00000000..a1c7150f --- /dev/null +++ b/src/components/GridRepCollection/index.css @@ -0,0 +1,6 @@ +.GridRepCollection { + display: flex; + flex-wrap: wrap; + justify-content: center; + padding: 0; +} \ No newline at end of file diff --git a/src/components/GridRepCollection/index.tsx b/src/components/GridRepCollection/index.tsx new file mode 100644 index 00000000..fdceedc0 --- /dev/null +++ b/src/components/GridRepCollection/index.tsx @@ -0,0 +1,15 @@ +import React from 'react' + +import './index.css' + +interface Props {} + +const GridRepCollection: React.FC = ({ children }) => { + return ( +
    + {children} +
+ ) +} + +export default GridRepCollection diff --git a/src/components/Main/index.tsx b/src/components/Main/index.tsx index be349c66..52fcaccb 100644 --- a/src/components/Main/index.tsx +++ b/src/components/Main/index.tsx @@ -19,7 +19,7 @@ const Main = () => ( - + ) diff --git a/src/components/WeaponGrid/index.css b/src/components/WeaponGrid/index.css index 206835fc..ad176b84 100644 --- a/src/components/WeaponGrid/index.css +++ b/src/components/WeaponGrid/index.css @@ -25,16 +25,4 @@ #grid_weapons > li { list-style: none; -} - -#NotFound { - display: flex; - flex-direction: column; - align-items: center; - margin-top: 40px; -} - -#NotFound h2 { - color: #444; - font-weight: 500; } \ No newline at end of file diff --git a/src/components/WeaponGrid/index.tsx b/src/components/WeaponGrid/index.tsx index 657598ea..060ac0d3 100644 --- a/src/components/WeaponGrid/index.tsx +++ b/src/components/WeaponGrid/index.tsx @@ -90,49 +90,36 @@ const WeaponGrid = (props: Props) => { api.endpoints.weapons.create(body) } - function renderGrid() { - return ( -
- - -
    - { - Array.from(Array(numWeapons)).map((x, i) => { - return ( -
  • - -
  • - ) - }) - } -
-
- ) - } + return ( +
+ - function renderGridNotFound() { - return ( -
-

There's no grid here.

- -
- ) - } - - return (!props.exists || props.found) ? renderGrid() : renderGridNotFound() +
    + { + Array.from(Array(numWeapons)).map((x, i) => { + return ( +
  • + +
  • + ) + }) + } +
+
+ ) } export default WeaponGrid diff --git a/src/routes/Party/index.tsx b/src/routes/Party/index.tsx index ddb6cf88..ddcd1446 100644 --- a/src/routes/Party/index.tsx +++ b/src/routes/Party/index.tsx @@ -4,6 +4,7 @@ import { RouteComponentProps, withRouter } from 'react-router-dom' import api from '~utils/api' import WeaponGrid from '~components/WeaponGrid' +import Button from '~components/Button' interface Props { hash: string @@ -29,6 +30,7 @@ interface GridWeapon { const Party: React.FC = ({ match }, state: State) => { const [found, setFound] = useState(false) + const [loading, setLoading] = useState(true) const [editable, setEditable] = useState(false) const [grid, setGrid] = useState({}) const [mainhand, setMainhand] = useState() @@ -61,6 +63,7 @@ const Party: React.FC = ({ match }, state: State) => { }) setFound(true) + setLoading(false) setGrid(weapons) setPartyId(party.id) }) @@ -68,6 +71,7 @@ const Party: React.FC = ({ match }, state: State) => { if (error.response != null) { if (error.response.status == 404) { setFound(false) + setLoading(false) } } else { console.error(error) @@ -75,19 +79,38 @@ const Party: React.FC = ({ match }, state: State) => { }) } - return ( -
- -
- ) + function render() { + return ( +
+ +
+ ) + } + + function renderNotFound() { + return ( +
+

There's no grid here.

+ +
+ ) + } + + if (!found && !loading) { + return renderNotFound() + } else if (found && !loading) { + return render() + } else { + return (
) + } } export default diff --git a/src/routes/Profile/index.tsx b/src/routes/Profile/index.tsx index 1a41768a..d4643155 100644 --- a/src/routes/Profile/index.tsx +++ b/src/routes/Profile/index.tsx @@ -3,6 +3,10 @@ import { withCookies, useCookies } from 'react-cookie' import { RouteComponentProps, withRouter } from 'react-router-dom' import api from '~utils/api' +import GridRep from '~components/GridRep' +import GridRepCollection from '~components/GridRepCollection' +import { composeInitialProps } from 'react-i18next' + interface Props { username: string } @@ -13,21 +17,31 @@ interface User { granblueId: number } +interface Party { + id: string + shortcode: string + grid: GridWeapon[] +} + interface ProfileProps extends RouteComponentProps {} -const Profile: React.FC = ({ match }) => { +const Profile: React.FC = ({ history, match }) => { const [cookies, setCookie] = useCookies(['user']) + + const [found, setFound] = useState(false) + const [loading, setLoading] = useState(true) + const [parties, setParties] = useState([]) const [user, setUser] = useState({ id: '', username: '', granblueId: 0 }) - const [parties, setParties] = useState([]) const username = match.params.username || '' useEffect(() => { - fetchProfile(username) + console.log(`Fetching profile for ${username}...`) + fetchProfile(username) }, []) async function fetchProfile(username: string) { @@ -40,13 +54,75 @@ const Profile: React.FC = ({ match }) => { }) setParties(response.data.user.parties) }) + .then(() => { + setFound(true) + setLoading(false) + }) + .catch(error => { + if (error.response != null) { + if (error.response.status == 404) { + setFound(false) + } + } else { + console.error(error) + } + }) } - return ( -
-

{user.username}

-
- ) + function render() { + const content = (parties && parties.length > 0) ? renderGrids() : renderNoGrids() + return ( +
+

{user.username}

+ {content} +
+ ) + } + + function navigate(shortcode: string) { + history.push(`/p/${shortcode}`) + } + + function renderGrids() { + return ( + + { + parties.map((party, i) => { + return + }) + } + + ) + } + + function renderNoGrids() { + return ( +
+

This user has no grids.

+
+ ) + } + + function renderNotFound() { + return ( +
+

That user doesn't exist.

+
+ ) + } + + if (!found && !loading) { + return renderNotFound() + } else if (found && !loading) { + return render() + } else { + return (
) + } } export default diff --git a/src/types/GridWeapon.d.ts b/src/types/GridWeapon.d.ts new file mode 100644 index 00000000..2c329833 --- /dev/null +++ b/src/types/GridWeapon.d.ts @@ -0,0 +1,6 @@ +interface GridWeapon { + id: string + mainhand: boolean + position: number | null + weapon: Weapon +} \ No newline at end of file