Refactor out Setting.getArrayItem()

This commit is contained in:
Yang Luo
2021-07-09 23:05:50 +08:00
parent 3923574d57
commit 1bfddc5d53
3 changed files with 8 additions and 2 deletions

View File

@@ -73,7 +73,7 @@ class ProviderTable extends React.Component {
value={text} value={text}
onChange={value => { onChange={value => {
this.updateField(table, index, 'name', value); this.updateField(table, index, 'name', value);
const provider = this.props.providers.filter(provider => provider.name === value)[0]; const provider = Setting.getArrayItem(this.props.providers, "name", value);
this.updateField(table, index, 'provider', provider); this.updateField(table, index, 'provider', provider);
}} > }} >
{ {

View File

@@ -448,3 +448,8 @@ export function maskEmail(email) {
return `${username}@${domainTokens.join(".")}`; return `${username}@${domainTokens.join(".")}`;
} }
export function getArrayItem(array, key, value) {
const res = array.filter(item => item[key] === value)[0];
return res;
}

View File

@@ -87,7 +87,8 @@ class AffiliationSelect extends React.Component {
) : ( ) : (
<Select virtual={false} style={{width: '100%'}} value={this.props.user.affiliation} onChange={(value => { <Select virtual={false} style={{width: '100%'}} value={this.props.user.affiliation} onChange={(value => {
const name = value; const name = value;
const id = this.state.affiliationOptions.filter(affiliationOption => affiliationOption.name === name)[0].id; const affiliationOption = Setting.getArrayItem(this.state.affiliationOptions, "name", name);
const id = affiliationOption.id;
this.updateUserField('affiliation', name); this.updateUserField('affiliation', name);
this.updateUserField('score', id); this.updateUserField('score', id);
})}> })}>