import React from 'react'; import { inject, observer } from 'mobx-react'; import { RouteComponentProps } from 'react-router-dom'; import { get, groupBy, map } from 'lodash'; import { ContentContainer, Portrait, ScrollToTop } from '../../components'; import { UserStore } from '../../stores/user-store'; import { UserService } from '../../services'; import { AvatarList, CharacterModel } from '../../model'; import './user-account.scss'; interface Props extends RouteComponentProps { userStore?: UserStore; } interface State { characters: {[realm: string]: CharacterModel[]}; selectedRealm?: string; selectedCharIndex: number; } @inject('userStore') @observer export class UserAccount extends React.Component { constructor(props: Props) { super(props); this.state = { characters: {}, selectedCharIndex: 0, }; } componentDidMount() { this.getCharacters(); } private selectedCharacter(): CharacterModel { const { selectedRealm, selectedCharIndex } = this.state; const char = get(this.state, `characters[${selectedRealm}][${selectedCharIndex}]`); return char; } async getCharacters() { try { const res = await UserService.getCharacters() as any; const characters = groupBy(res, 'realm'); this.setState({ characters, selectedRealm: res[0].realm, }); } catch (e) { console.error(e); } } onRealmSelect(event: any) { this.setState({ selectedRealm: event.target.value, selectedCharIndex: 0 }); } onCharSelect(event: any) { this.setState({ selectedCharIndex: event.target.value as any }); } renderDropDowns() { if (!this.selectedCharacter()) { return
; } return (
{AvatarList.map((val, index) => { return (
); })}
); } private async onSave() { const { name, guild, realm } = this.selectedCharacter(); const data = { character_name: name, character_class: 'Rogue', // todo get class from number character_guild: guild, character_realm: realm, character_avatar: 'Avatar', // TODO: }; await UserService.saveCharacter(data); } render() { const { battletag, character_name, character_class, character_guild, character_realm } = this.props.userStore!.user!; return (
{battletag &&
Battletag: {battletag}
} {character_name &&
Character: {character_name}
} {character_class &&
Class: {character_class}
} {character_guild &&
Guild: {character_guild}
} {character_realm &&
Realm: {character_realm}
}

Set a new default character

{this.renderDropDowns()} this.onSave()}>Save
); } }