E.W.O. Next / All files / src/client/Game CharacterPage.tsx

0% Statements 0/27
0% Branches 0/8
0% Functions 0/6
0% Lines 0/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                                                                                                                                                                                               
/**
 * @module Client.Game
 */
 
import React from 'react';
import { connect } from 'react-redux';
import { Dispatch } from 'redux';
import { selectCharacter } from '../actions';
import { IStateFrontend } from '../reducers';
import { Actions } from './Actions';
import { Infos } from './Infos';
import { States } from './States';
import { Views } from './Views';
 
export interface CharacterPageProps {
  /**
   * The current selected character, by matricule
   */
  selectedCharacter: number;
 
  /**
   * Flag if the page is loaded
   */
  loaded: boolean;
 
  /**
   * Flag if the page is curently loading
   */
  loading: boolean;
 
  /**
   * Error message
   */
  error?: string;
}
 
export interface CharacterPageComponentProps extends CharacterPageProps {
  /**
   * Action for selecting the character
   * @param mat CHaracter matricule
   */
  selectCharacter(mat: number);
}
 
export class CharacterPageComponent extends React.Component<CharacterPageComponentProps> {
  public render() {
 
    const { loaded, loading, error } = this.props;
 
    if (loading) {
      return <i className="fas fa-spinner">Loading...</i>;
    }
 
    if (error) {
      return <div className="alert alert-danger" role="alert">{error}</div>;
    }
 
    if (!loaded) {
      return <div>Vous n'ĂȘtes pas connectĂ©</div>;
    }
 
    return (
      <div className="Game">
        <Infos />
        <Actions />
        <States />
        <Views />
      </div>
    );
  }
 
  public componentDidMount() {
    this.props.selectCharacter(this.props.selectedCharacter);
  }
 
  public shouldComponentUpdate(nextProps: CharacterPageComponentProps) {
    if (nextProps.selectedCharacter !== this.props.selectedCharacter) {
      this.props.selectCharacter(nextProps.selectedCharacter);
      return false;
    }
    return true;
  }
}
 
const mapStateToProps = (state: IStateFrontend) => ({
  loaded: state.loaded,
  loading: state.loading,
  error: state.error,
});
 
const mapDispatchToProps = (dispatch: Dispatch<any>) => ({
  selectCharacter: (mat) => dispatch(selectCharacter(mat)),
});
 
export const CharacterPage = connect(mapStateToProps, mapDispatchToProps)(CharacterPageComponent) as React.StatelessComponent<CharacterPageProps>;