Lift SearchModal out of CharacterUnit
This commit is contained in:
parent
e95f45caa6
commit
907d9f05e4
3 changed files with 38 additions and 30 deletions
|
|
@ -1,5 +1,8 @@
|
||||||
import React, { useState } from 'react'
|
import React, { useState } from 'react'
|
||||||
|
import { useModal as useModal } from '~utils/useModal'
|
||||||
|
|
||||||
import CharacterUnit from '~components/CharacterUnit'
|
import CharacterUnit from '~components/CharacterUnit'
|
||||||
|
import SearchModal from '~components/SearchModal'
|
||||||
|
|
||||||
import './index.scss'
|
import './index.scss'
|
||||||
|
|
||||||
|
|
@ -19,12 +22,31 @@ interface Props {
|
||||||
}
|
}
|
||||||
|
|
||||||
const CharacterGrid = (props: Props) => {
|
const CharacterGrid = (props: Props) => {
|
||||||
|
const { open, openModal, closeModal } = useModal()
|
||||||
|
const [searchPosition, setSearchPosition] = useState(0)
|
||||||
|
|
||||||
const numCharacters: number = 5
|
const numCharacters: number = 5
|
||||||
|
|
||||||
|
function isCharacter(object: Character | Weapon | Summon): object is Character {
|
||||||
|
// There aren't really any unique fields here
|
||||||
|
return (object as Character).gender !== undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
function openSearchModal(position: number) {
|
||||||
|
setSearchPosition(position)
|
||||||
|
openModal()
|
||||||
|
}
|
||||||
|
|
||||||
function receiveCharacter(character: Character, position: number) {
|
function receiveCharacter(character: Character, position: number) {
|
||||||
props.onSelect(GridType.Character, character, position)
|
props.onSelect(GridType.Character, character, position)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sendData(object: Character | Weapon | Summon, position: number) {
|
||||||
|
if (isCharacter(object)) {
|
||||||
|
receiveCharacter(object, position)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="CharacterGrid">
|
<div className="CharacterGrid">
|
||||||
<ul id="grid_characters">
|
<ul id="grid_characters">
|
||||||
|
|
@ -33,8 +55,8 @@ const CharacterGrid = (props: Props) => {
|
||||||
return (
|
return (
|
||||||
<li key={`grid_unit_${i}`} >
|
<li key={`grid_unit_${i}`} >
|
||||||
<CharacterUnit
|
<CharacterUnit
|
||||||
|
onClick={() => { openSearchModal(i) }}
|
||||||
editable={props.editable}
|
editable={props.editable}
|
||||||
onReceiveData={receiveCharacter}
|
|
||||||
position={i}
|
position={i}
|
||||||
character={props.grid[i]}
|
character={props.grid[i]}
|
||||||
/>
|
/>
|
||||||
|
|
@ -42,6 +64,16 @@ const CharacterGrid = (props: Props) => {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
{open ? (
|
||||||
|
<SearchModal
|
||||||
|
grid={props.grid}
|
||||||
|
close={closeModal}
|
||||||
|
send={sendData}
|
||||||
|
fromPosition={searchPosition}
|
||||||
|
object="characters"
|
||||||
|
placeholderText="Search for a character..."
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import classnames from 'classnames'
|
import classnames from 'classnames'
|
||||||
import { useModal as useModal } from '~utils/useModal'
|
|
||||||
|
|
||||||
import SearchModal from '~components/SearchModal'
|
|
||||||
import UncapIndicator from '~components/UncapIndicator'
|
import UncapIndicator from '~components/UncapIndicator'
|
||||||
|
|
||||||
import PlusIcon from '~public/icons/plus.svg'
|
import PlusIcon from '~public/icons/plus.svg'
|
||||||
|
|
@ -10,7 +8,7 @@ import PlusIcon from '~public/icons/plus.svg'
|
||||||
import './index.scss'
|
import './index.scss'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
onReceiveData: (character: Character, position: number) => void
|
onClick: () => void
|
||||||
character: Character | undefined
|
character: Character | undefined
|
||||||
position: number
|
position: number
|
||||||
editable: boolean
|
editable: boolean
|
||||||
|
|
@ -18,9 +16,6 @@ interface Props {
|
||||||
|
|
||||||
const CharacterUnit = (props: Props) => {
|
const CharacterUnit = (props: Props) => {
|
||||||
const [imageUrl, setImageUrl] = useState('')
|
const [imageUrl, setImageUrl] = useState('')
|
||||||
const { open, openModal, closeModal } = useModal()
|
|
||||||
|
|
||||||
const openModalIfEditable = (props.editable) ? openModal : () => {}
|
|
||||||
|
|
||||||
const classes = classnames({
|
const classes = classnames({
|
||||||
CharacterUnit: true,
|
CharacterUnit: true,
|
||||||
|
|
@ -40,27 +35,16 @@ const CharacterUnit = (props: Props) => {
|
||||||
|
|
||||||
if (props.character) {
|
if (props.character) {
|
||||||
const character = props.character!
|
const character = props.character!
|
||||||
imgSrc = `/images/chara-main/${character.granblue_id}.jpg`
|
imgSrc = `/images/chara-main/${character.granblue_id}_01.jpg`
|
||||||
}
|
}
|
||||||
|
|
||||||
setImageUrl(imgSrc)
|
setImageUrl(imgSrc)
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendData(object: Character | Weapon | Summon, position: number) {
|
|
||||||
if (isCharacter(object)) {
|
|
||||||
props.onReceiveData(object, position)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function isCharacter(object: Character | Weapon | Summon): object is Character {
|
|
||||||
// There aren't really any unique fields here
|
|
||||||
return (object as Character).gender !== undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<div className={classes} onClick={openModalIfEditable}>
|
<div className={classes}>
|
||||||
<div className="CharacterImage">
|
<div className="CharacterImage" onClick={props.onClick}>
|
||||||
<img alt={character?.name.en} className="grid_image" src={imageUrl} />
|
<img alt={character?.name.en} className="grid_image" src={imageUrl} />
|
||||||
{ (props.editable) ? <span className='icon'><PlusIcon /></span> : '' }
|
{ (props.editable) ? <span className='icon'><PlusIcon /></span> : '' }
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -71,15 +55,6 @@ const CharacterUnit = (props: Props) => {
|
||||||
/>
|
/>
|
||||||
<h3 className="CharacterName">{character?.name.en}</h3>
|
<h3 className="CharacterName">{character?.name.en}</h3>
|
||||||
</div>
|
</div>
|
||||||
{open ? (
|
|
||||||
<SearchModal
|
|
||||||
close={closeModal}
|
|
||||||
send={sendData}
|
|
||||||
fromPosition={props.position}
|
|
||||||
object="characters"
|
|
||||||
placeholderText="Search for a character..."
|
|
||||||
/>
|
|
||||||
) : null}
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import PlusIcon from '~public/icons/plus.svg'
|
||||||
interface Props {
|
interface Props {
|
||||||
close: () => void
|
close: () => void
|
||||||
send: (object: Character | Weapon | Summon, position: number) => any
|
send: (object: Character | Weapon | Summon, position: number) => any
|
||||||
|
grid: GridArray<Character|Weapon|Summon>
|
||||||
placeholderText: string
|
placeholderText: string
|
||||||
fromPosition: number
|
fromPosition: number
|
||||||
object: 'weapons' | 'characters' | 'summons'
|
object: 'weapons' | 'characters' | 'summons'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue