import React from 'react';
import { withRouter, Link } from 'react-router-dom';
import { connect } from 'react-redux';
import queryString from 'query-string';
//actions
import { storeProfile, clearProfile } from '../../actions/profile.js';
//panels
import CommonLinks from '../panels/common_links.jsx';
import AttackButton from '../panels/attack_button.jsx';
import Markdown from '../panels/markdown.jsx';
import BadgeText from '../panels/badge_text.jsx';
class Profile extends React.Component {
constructor(props) {
super(props);
this.state = {
params: queryString.parse(props.location.search),
warning: '', //TODO: unified warning?
};
this.sendRequest('/profilerequest', {username: this.state.params.username ? this.state.params.username : this.props.account.username});
}
componentWillUnmount() {
this.props.clearProfile();
}
render() {
let warningStyle = {
display: this.state.warning.length > 0 ? 'flex' : 'none'
};
//side panel stuff
let SidePanel;
if (this.props.account.id) {
if (this.props.account.username === this.props.profile.username) {
SidePanel = this.MyProfileSidePanel.bind(this);
} else {
SidePanel = this.NotMyProfileSidePanel.bind(this);
}
} else { //logged out
SidePanel = this.LoggedOutSidePanel.bind(this);
}
//main panel
let MainPanel;
if (this.props.account.id) {
//logged in
if (this.props.account.username === this.props.profile.username) {
MainPanel = this.MyProfileMainPanel.bind(this);
} else {
if (this.props.profile.username) {
MainPanel = this.NotMyProfileMainPanel.bind(this);
} else {
MainPanel = this.ProfileNotFoundMainPanel.bind(this);
}
}
} else {
//not logged in
if (this.props.profile.username) {
MainPanel = this.LoggedOutMainPanel.bind(this);
} else {
MainPanel = this.ProfileNotFoundMainPanel.bind(this);
}
}
//finally
return (
);
}
//gameplay functions
sendRequest(url, args = {}) { //send a unified request, using my credentials
//build the XHR
let xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
let json = JSON.parse(xhr.responseText);
this.props.storeProfile(
json.username,
json.gold,
json.recruits,
json.soldiers,
json.spies,
json.scientists,
json.activeBadge,
json.activeBadgeFilename
);
}
else if (xhr.status === 400) {
this.setWarning(xhr.responseText);
}
}
};
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.send(JSON.stringify({
id: this.props.account.id,
token: this.props.account.token,
...args
}));
}
//panel functions
MyProfileSidePanel() {
return (
);
}
MyProfileMainPanel() {
return (
Your Kingdom
Username:
{this.props.profile.username}
Gold:
{this.props.profile.gold}
(+1 gold for each recruit)
Recruits:
{this.props.profile.recruits}
Soldiers:
{this.props.profile.soldiers}
Scientists:
{this.props.profile.scientists}
Spies:
{this.props.profile.spies}
);
}
NotMyProfileSidePanel() {
//return the side panel
return (
{
e.preventDefault();
this.sendRequest('/profilerequest', {username: this.props.account.username});
this.setWarning('');
this.props.history.push('/profile');
}} />
);
}
NotMyProfileMainPanel() {
return (
{this.props.profile.username}'s Kingdom
Username:
{this.props.profile.username}
Gold:
{this.props.profile.gold}
Recruits:
{this.props.profile.recruits}
json.soldiers}
>Attack
Soldiers:
{this.props.profile.soldiers}
json.spies}
>Send Spies
Scientists:
{this.props.profile.scientists}
Spies:
{this.props.profile.spies}
);
}
LoggedOutSidePanel() {
return (
);
}
LoggedOutMainPanel() {
return (
{this.props.profile.username}'s Kingdom
Username:
{this.props.profile.username}
Gold:
{this.props.profile.gold}
Recruits:
{this.props.profile.recruits}
Soldiers:
{this.props.profile.soldiers}
Scientists:
{this.props.profile.scientists}
Spies:
{this.props.profile.spies}
);
}
ProfileNotFoundMainPanel() {
return (
);
}
setWarning(s) {
this.setState({ warning: s });
}
};
const mapStoreToProps = (store) => {
return {
account: store.account,
profile: store.profile,
};
};
const mapDispatchToProps = (dispatch) => {
return {
storeProfile: (username, gold, recruits, soldiers, spies, scientists, activeBadge, activeBadgeFilename) => dispatch(storeProfile(username, gold, recruits, soldiers, spies, scientists, activeBadge, activeBadgeFilename)),
clearProfile: () => dispatch(clearProfile())
};
};
Profile = connect(mapStoreToProps, mapDispatchToProps)(Profile);
export default withRouter(Profile);