fix: table do not have unique key (#1512)

This commit is contained in:
Yaodong Yu
2023-02-02 13:53:18 +08:00
committed by GitHub
parent 11b56c340f
commit b34e16b145
4 changed files with 73 additions and 26 deletions

View File

@ -25,11 +25,27 @@ class ManagedAccountTable extends React.Component {
super(props); super(props);
this.state = { this.state = {
classes: props, classes: props,
managedAccounts: [],
}; };
this.state.managedAccounts = this.props.table.map((item, index) => {
item.key = index;
return item;
});
} }
count = this.props.table.length;
updateTable(table) { updateTable(table) {
this.props.onUpdateTable(table); this.setState({
managedAccounts: table,
});
this.props.onUpdateTable([...table].map((item) => {
const newItem = Setting.deepCopy(item);
delete newItem.key;
return newItem;
}));
} }
updateField(table, index, key, value) { updateField(table, index, key, value) {
@ -38,10 +54,12 @@ class ManagedAccountTable extends React.Component {
} }
addRow(table) { addRow(table) {
const row = {application: "", username: "", password: ""}; const row = {key: this.count, application: "", username: "", password: ""};
if (table === undefined || table === null) { if (table === undefined || table === null) {
table = []; table = [];
} }
this.count += 1;
table = Setting.addRow(table, row); table = Setting.addRow(table, row);
this.updateTable(table); this.updateTable(table);
} }
@ -131,7 +149,7 @@ class ManagedAccountTable extends React.Component {
]; ];
return ( return (
<Table scroll={{x: "max-content"}} rowKey="name" columns={columns} dataSource={table} size="middle" bordered pagination={false} <Table scroll={{x: "max-content"}} rowKey="key" columns={columns} dataSource={table} size="middle" bordered pagination={false}
title={() => ( title={() => (
<div> <div>
{this.props.title}&nbsp;&nbsp;&nbsp;&nbsp; {this.props.title}&nbsp;&nbsp;&nbsp;&nbsp;
@ -148,7 +166,7 @@ class ManagedAccountTable extends React.Component {
<Row style={{marginTop: "20px"}} > <Row style={{marginTop: "20px"}} >
<Col span={24}> <Col span={24}>
{ {
this.renderTable(this.props.table) this.renderTable(this.state.managedAccounts)
} }
</Col> </Col>
</Row> </Row>

View File

@ -571,7 +571,7 @@ class UserEditPage extends React.Component {
<Col span={22} > <Col span={22} >
<ManagedAccountTable <ManagedAccountTable
title={i18next.t("user:Managed accounts")} title={i18next.t("user:Managed accounts")}
table={this.state.user.managedAccounts ?? []} table={this.state.user.managedAccounts}
onUpdateTable={(table) => {this.updateUserField("managedAccounts", table);}} onUpdateTable={(table) => {this.updateUserField("managedAccounts", table);}}
applications={this.state.applications} applications={this.state.applications}
/> />

View File

@ -28,9 +28,18 @@ class PolicyTable extends React.Component {
editingIndex: "", editingIndex: "",
oldPolicy: "", oldPolicy: "",
add: false, add: false,
page: 1,
}; };
} }
count = 0;
pageSize = 10;
getIndex(index) {
// Need to be used in all place when modify table. Parameter is the row index in table, need to calculate the index in dataSource.
return index + (this.state.page - 1) * 10;
}
UNSAFE_componentWillMount() { UNSAFE_componentWillMount() {
if (this.props.mode === "edit") { if (this.props.mode === "edit") {
this.synPolicies(); this.synPolicies();
@ -46,8 +55,8 @@ class PolicyTable extends React.Component {
}; };
cancel = (table, index) => { cancel = (table, index) => {
Object.keys(table[index]).forEach((key) => { Object.keys(table[this.getIndex(index)]).forEach((key) => {
table[index][key] = this.state.oldPolicy[key]; table[this.getIndex(index)][key] = this.state.oldPolicy[key];
}); });
this.updateTable(table); this.updateTable(table);
this.setState({editingIndex: "", oldPolicy: ""}); this.setState({editingIndex: "", oldPolicy: ""});
@ -62,23 +71,28 @@ class PolicyTable extends React.Component {
} }
updateField(table, index, key, value) { updateField(table, index, key, value) {
table[index][key] = value; table[this.getIndex(index)][key] = value;
this.updateTable(table); this.updateTable(table);
} }
addRow(table) { addRow(table) {
const row = {Ptype: "p"}; const row = {key: this.count, Ptype: "p"};
if (table === undefined) { if (table === undefined) {
table = []; table = [];
} }
table = Setting.addRow(table, row, "top"); table = Setting.addRow(table, row, "top");
this.count = this.count + 1;
this.updateTable(table); this.updateTable(table);
this.edit(row, 0); this.edit(row, 0);
this.setState({add: true}); this.setState({
page: 1,
add: true,
});
} }
deleteRow(table, i) { deleteRow(table, index) {
table = Setting.deleteRow(table, i); table = Setting.deleteRow(table, this.getIndex(index));
this.updateTable(table); this.updateTable(table);
} }
@ -91,8 +105,14 @@ class PolicyTable extends React.Component {
AdapterBackend.syncPolicies(this.props.owner, this.props.name) AdapterBackend.syncPolicies(this.props.owner, this.props.name)
.then((res) => { .then((res) => {
if (res.status === "ok") { if (res.status === "ok") {
this.setState({policyLists: res.data});
Setting.showMessage("success", i18next.t("adapter:Sync policies successfully")); Setting.showMessage("success", i18next.t("adapter:Sync policies successfully"));
const policyList = res.data;
policyList.map((policy, index) => {
policy.key = index;
});
this.count = policyList.length;
this.setState({policyLists: policyList});
} else { } else {
Setting.showMessage("error", `${i18next.t("adapter:Failed to sync policies")}: ${res.msg}`); Setting.showMessage("error", `${i18next.t("adapter:Failed to sync policies")}: ${res.msg}`);
} }
@ -129,12 +149,12 @@ class PolicyTable extends React.Component {
}); });
} }
deletePolicy(table, i) { deletePolicy(table, index) {
AdapterBackend.RemovePolicy(this.props.owner, this.props.name, table[i]).then(res => { AdapterBackend.RemovePolicy(this.props.owner, this.props.name, table[this.getIndex(index)]).then(res => {
if (res.status === "ok") { if (res.status === "ok") {
table = Setting.deleteRow(table, i);
this.updateTable(table);
Setting.showMessage("success", i18next.t("general:Successfully deleted")); Setting.showMessage("success", i18next.t("general:Successfully deleted"));
this.deleteRow(table, index);
} else { } else {
Setting.showMessage("error", i18next.t("general:Failed to delete")); Setting.showMessage("error", i18next.t("general:Failed to delete"));
} }
@ -279,9 +299,14 @@ class PolicyTable extends React.Component {
return ( return (
<Table <Table
pagination={{ pagination={{
defaultPageSize: 10, defaultPageSize: this.pageSize,
onChange: (page) => this.setState({
page: page,
}),
disabled: this.state.editingIndex !== "",
current: this.state.page,
}} }}
columns={columns} dataSource={table} rowKey="index" size="middle" bordered columns={columns} dataSource={table} rowKey="key" size="middle" bordered
loading={this.state.loading} loading={this.state.loading}
title={() => ( title={() => (
<div> <div>

View File

@ -23,18 +23,19 @@ class PropertyTable extends React.Component {
super(props); super(props);
this.state = { this.state = {
properties: [], properties: [],
count: this.props.properties !== null ? Object.entries(this.props.properties).length : 0,
}; };
// transfer the Object to object[] // transfer the Object to object[]
if (this.props.properties !== null) { if (this.props.properties !== null) {
Object.entries(this.props.properties).map((item, index) => { Object.entries(this.props.properties).map((item, index) => {
this.state.properties.push({key: index, name: item[0], value: item[1]}); this.state.properties.push({key: index, name: item[0], value: item[1]});
}); });
} }
} }
page = 1; page = 1;
pageSize = 10;
count = this.props.properties !== null ? Object.entries(this.props.properties).length : 0;
updateTable(table) { updateTable(table) {
this.setState({properties: table}); this.setState({properties: table});
@ -46,12 +47,12 @@ class PropertyTable extends React.Component {
} }
addRow(table) { addRow(table) {
const row = {key: this.state.count, name: "", value: ""}; const row = {key: this.count, name: "", value: ""};
if (table === undefined) { if (table === undefined) {
table = []; table = [];
} }
table = Setting.addRow(table, row); table = Setting.addRow(table, row);
this.setState({count: this.state.count + 1}); this.count = this.count + 1;
this.updateTable(table); this.updateTable(table);
} }
@ -61,8 +62,8 @@ class PropertyTable extends React.Component {
} }
getIndex(index) { getIndex(index) {
// Parameter is the row index in table, need to calculate the index in dataSource. 10 is the pageSize. // Need to be used in all place when modify table. Parameter is the row index in table, need to calculate the index in dataSource.
return index + (this.page - 1) * 10; return index + (this.page - 1) * this.pageSize;
} }
updateField(table, index, key, value) { updateField(table, index, key, value) {
@ -114,7 +115,10 @@ class PropertyTable extends React.Component {
<Button style={{marginRight: "5px"}} type="primary" size="small" onClick={() => this.addRow(table)}>{i18next.t("general:Add")}</Button> <Button style={{marginRight: "5px"}} type="primary" size="small" onClick={() => this.addRow(table)}>{i18next.t("general:Add")}</Button>
</div> </div>
)} )}
pagination={{onChange: page => {this.page = page;}}} pagination={{
defaultPageSize: this.pageSize,
onChange: page => {this.page = page;},
}}
columns={columns} dataSource={table} rowKey="key" size="middle" bordered columns={columns} dataSource={table} rowKey="key" size="middle" bordered
/> />
); );