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 { CharacterService, UserService } from '../../services'; import { CharacterModel } from '../../model'; import './user-account.scss'; interface Props extends RouteComponentProps { userStore?: UserStore; } interface State { characters: {[realm: string]: CharacterModel[]}; selectedRealm?: string; selectedCharIndex: number; selectedAvatarIndex: number; insufficientScope?: boolean; noCharacters: boolean; } @inject('userStore') @observer export class UserAccount extends React.Component { constructor(props: Props) { super(props); this.state = { characters: {}, noCharacters: false, selectedCharIndex: 0, selectedAvatarIndex: 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; if (res.characters) { if (res.characters.length === 0) { this.setState({ noCharacters: true }); return; } const characters = groupBy(res.characters, 'realm'); this.setState({ characters, selectedRealm: res.characters[0].realm, insufficientScope: false, }); } else { this.setState({ insufficientScope: true }); } } catch (e) { console.error(e); } } onRealmSelect(event: any) { this.setState({ selectedRealm: event.target.value, selectedCharIndex: 0, selectedAvatarIndex: 0, }); } onCharSelect(event: any) { this.setState({ selectedCharIndex: event.target.value as any, selectedAvatarIndex: 0, }); } onAvatarSelect(selectedAvatarIndex: number) { this.setState({ selectedAvatarIndex }); } renderDropDowns() { if (!this.selectedCharacter()) { return
; } return (

Set your default character

this.onSave()}>Save
{this.selectedCharacter().avatarList!.map((val, index) => { const avatarClass = this.state.selectedAvatarIndex === index ? 'avatar-list__item--selected' : ''; return (
this.onAvatarSelect(index)}>
); })}
); } private async onSave() { const { name, guild, realm } = this.selectedCharacter(); const selectedAvatar = this.selectedCharacter().avatarList![this.state.selectedAvatarIndex].title; const charClass = CharacterService.getClass(this.selectedCharacter().class); const data = { character_name: name, character_class: charClass.name, character_guild: guild, character_realm: realm, character_avatar: selectedAvatar, }; await UserService.saveCharacter(data); } private logout() { this.props.userStore!.resetUser(); window.location.pathname = '/'; } private renderScopeError() { return (

To set your default character we need access to your WoW profile.

); } render() { if (this.state.noCharacters) { return
You have no WoW characters in your account.
; } // user must be logged in to view this page if (!this.props.userStore!.user) { return
; } const { battletag, character_name, character_class, character_guild, character_realm, character_avatar } = this.props.userStore!.user!; const { insufficientScope } = this.state; return (
{character_avatar && }
{battletag &&
Battletag: {battletag}
} {character_name &&
Character: {character_name}
} {character_class &&
Class: {character_class}
} {character_guild &&
Guild: {character_guild}
} {character_realm &&
Realm: {character_realm}
}
{insufficientScope === true ? this.renderScopeError() : this.renderDropDowns()}
); } }