From a4d28d8d429b2d6ffcba9598cddc72488945da18 Mon Sep 17 00:00:00 2001 From: Habu Date: Mon, 30 Apr 2018 11:46:39 +0900 Subject: [PATCH] =?utf8?q?[cleanup]TSLint=E3=81=AE=E6=8C=87=E6=91=98?= =?utf8?q?=E3=81=AB=E3=82=88=E3=82=8B=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- score/src/popurality_ranking.tsx | 294 ++++++++++++++++++++++++--------------- 1 file changed, 184 insertions(+), 110 deletions(-) diff --git a/score/src/popurality_ranking.tsx b/score/src/popurality_ranking.tsx index e6aa257..7e177b8 100644 --- a/score/src/popurality_ranking.tsx +++ b/score/src/popurality_ranking.tsx @@ -27,6 +27,19 @@ const columnOrder = [ "max_score", ]; +const basicTables = [ + "race", + "class", + "personality", +]; + +const realmTables = [ + "realm1", + "realm2", +]; + +const allTables = basicTables.concat(realmTables); + /** * 表示テーブル選択ボタンコンポーネントプロパティ */ @@ -49,19 +62,22 @@ class TableSelectButton extends React.Component { } public render() { - if (this.props.selected) { - return ( + const { selected, name, onClick } = this.props; + + const button = (selected) ? + ( - {name2j[this.props.name]} + {name2j[name]} - ); - } else { - return ( - this.props.onClick()}> - {name2j[this.props.name]} + ) + : + ( + + {name2j[name]} ); - } + + return {button}; } } @@ -75,7 +91,7 @@ interface ITableSelectProps { * 選択テーブル切替時に呼び出されるコールバック関数 * @param name 新たに選択されたテーブル名称 */ - onSelectChange: (name: string) => void; + onSelectedTableChange: (name: string) => void; } /** @@ -83,21 +99,40 @@ interface ITableSelectProps { */ // tslint:disable-next-line:max-classes-per-file class TableSelector extends React.Component { + private onSelectedTableChange: { [key: string]: () => void } = {}; + + constructor(props: ITableSelectProps) { + super(props); + + allTables.forEach((t) => + this.onSelectedTableChange[t] = () => this.props.onSelectedTableChange(t), + ); + } + public render() { return (
- [ {this.renderSelectButton("race")} | {this.renderSelectButton("class")} | - {this.renderSelectButton("personality")} ] - [ {this.renderSelectButton("realm1")} | {this.renderSelectButton("realm2")} ] + [{this.renderSelectBussons(basicTables)}] + [{this.renderSelectBussons(realmTables)}]
); } + private renderSelectBussons(tables: string[]) { + return ( + tables.map((t) => this.renderSelectButton(t)). + map((t, i) => {t}{i === tables.length - 1 ? "" : "|"}) + ); + } + private renderSelectButton(name: string) { - return this.props.onSelectChange(name)} />; + return ( + + ); } } @@ -122,22 +157,25 @@ interface ITableHeaderProps { * テーブルヘッダコンポーネント */ function TableHeader(props: ITableHeaderProps) { + const tableHeaderColumns = columnOrder.map((col) => { + let className = "sort"; + if (props.sortKeyColumn === col) { + className += " "; + className += (props.sortOrder === SortOrder.Ascend) ? "ascend" : "descend"; + } + const onClick = () => props.onClick(col); + return ( + + {column2j[col]} + + ); + }); + return ( {props.tableName} - {columnOrder.map((col) => { - let className = "sort"; - if (props.sortKeyColumn === col) { - className += " "; - className += (props.sortOrder === SortOrder.Ascend) ? "ascend" : "descend"; - } - return ( - props.onClick(col)}> - {column2j[col]} - - ); - })} + {tableHeaderColumns} ); @@ -159,15 +197,17 @@ interface ITableDataRowProps { * テーブル行データコンポーネント */ function TableDataRow(props: ITableDataRowProps) { + const dataColumns = columnOrder.map((col) => + {props.row_data[col]}); + return ( - + {props.rowName} - {columnOrder.map((col) => - {props.row_data[col]})} + {dataColumns} ); } @@ -215,6 +255,16 @@ class PopuralityTable extends React.Component ( + + )); return ( - - {data.map((row) => - )} - + {dataRows}
); } @@ -263,19 +314,18 @@ class PopuralityTable extends React.Component ( + + )); + return ( - - {data.map((row) => - )} - + {dataRows}
); } @@ -328,33 +379,37 @@ interface IRankingTablesProps { // tslint:disable-next-line:max-classes-per-file class RankingTables extends React.Component { public shouldComponentUpdate(nextProps: IRankingTablesProps) { - return (this.props.data === null && nextProps.data !== null) || + return (Object.keys(this.props.data).length === 0 && Object.keys(nextProps.data).length > 0) || (this.props.selectedTableName !== nextProps.selectedTableName); } public render() { - const tables = Object.keys(name2j).map((name) => { - if (!name.startsWith("realm")) { - return ( -
- -
- ); - } else { - const realmTables = this.props.data[name].map((i) => { - return ; - }); - return
{realmTables}
; - } + const { data, selectedTableName } = this.props; + + const tableElements = basicTables.map((name) => ( +
+ +
), + ); + + const realmTableElements = realmTables.map((name) => { + const classTables = data[name].map((i) => ( + ), + ); + + return
{classTables}
; }); - return
{tables}
; + + return
{tableElements}{realmTableElements}
; } } @@ -362,8 +417,8 @@ class RankingTables extends React.Component { * 人気データ表示コンポーネントプロパティ */ interface IPopuralityRankingProps { - /** 人気データ取得URL */ - dataUrl: string; + /** データ取得URL */ + url: string; } /** @@ -372,10 +427,12 @@ interface IPopuralityRankingProps { interface IPopuralityRankingState { /** 表示選択中テーブル名称 */ selectedTableName: string; + /** データ取得中かどうか */ + isLoading: boolean; /** データ内容 */ data: { [col: string]: any[] }; /** データ表示状態を示すメッセージ */ - stateMessage: string; + error: string; } /** @@ -390,27 +447,30 @@ class PopuralityRanking extends React.Component { - if (!response.ok) { - throw Error(`${response.status} ${response.statusText}`); - } - return response.json(); - }).then((data) => { - data.realm1 = this.group_by_class(data.realm1); - data.realm2 = this.group_by_class(data.realm2); - this.setState({ - data, - stateMessage: "Success", - }); - }).catch((err) => { - this.setState({ stateMessage: err.message }); - }); + this.setState({ isLoading: true }); + fetch(this.props.url) + .then((response) => { + if (!response.ok) { + throw Error(`${response.status} ${response.statusText}`); + } + return response.json(); + }) + .then((data) => { + // 魔法領域のデータを職業毎にグループ化する + data.realm1 = this.group_by_class(data.realm1); + data.realm2 = this.group_by_class(data.realm2); + this.setState({ data, isLoading: false }); + }) + .catch((err) => this.setState({ error: err.message, isLoading: false })); } public componentDidUpdate() { @@ -420,19 +480,26 @@ class PopuralityRanking extends React.Component{this.state.stateMessage}; + const { data, error, isLoading, selectedTableName } = this.state; + if (error !== "") { + return

{error}

; + } + if (isLoading) { + return

Loading...

; + } + if (Object.keys(data).length === 0) { + return

; } return (

); @@ -461,10 +528,17 @@ class PopuralityRanking extends React.Component { @@ -478,6 +552,6 @@ class PopuralityRanking extends React.Component, + , document.getElementById("content"), ); -- 2.11.0