Update getting tokens to have helper functions

pull/14223/head
Palak Bhojani 2019-06-27 14:39:34 -07:00
parent 272868d087
commit adc108c53e
2 changed files with 58 additions and 41 deletions

View File

@ -103,7 +103,7 @@ class SaveAsTaskForm extends PureComponent<Props & WithRouterProps> {
onSubmit={this.handleSubmit}
canSubmit={this.isFormValid}
dismiss={dismiss}
tokens={this.getRelevantTokens()}
tokens={this.getRelevantTokens}
selectedToken={selectedToken}
onTokenChange={this.handleTokenChange}
/>
@ -111,40 +111,9 @@ class SaveAsTaskForm extends PureComponent<Props & WithRouterProps> {
)
}
private getRelevantTokens() {
const authorizations = this.props.tokens
const {draftQueries, activeQueryIndex} = this.props
let readBucketName = ''
if (draftQueries[activeQueryIndex].editMode === 'builder') {
readBucketName = draftQueries[activeQueryIndex].builderConfig.buckets[0]
} else {
const text = draftQueries[activeQueryIndex].text
const splitBucket = text.split('bucket:')
const splitQuotes = splitBucket[1].split('"')
readBucketName = splitQuotes[1]
}
const writeBucketName = this.props.taskOptions.toBucketName
const readAuthorizations = authorizations.filter(auth =>
auth.permissions.some(
permission =>
permission.action === 'read' &&
permission.resource.type === 'buckets' &&
(!permission.resource.name ||
permission.resource.name === readBucketName)
)
)
const writeAuthorizations = authorizations.filter(auth =>
auth.permissions.some(
permission =>
permission.action === 'write' &&
permission.resource.type === 'buckets' &&
(!permission.resource.name ||
permission.resource.name === writeBucketName)
)
)
private get getRelevantTokens() {
const readAuthorizations = this.getReadAuthorizations
const writeAuthorizations = this.getWriteAuthorizations
const relevantAuthorizations = _.intersectionBy(
readAuthorizations,
@ -155,6 +124,56 @@ class SaveAsTaskForm extends PureComponent<Props & WithRouterProps> {
return relevantAuthorizations
}
private get readBucketName() {
const {draftQueries, activeQueryIndex} = this.props
const query = draftQueries[activeQueryIndex]
let readBucketName = ''
if (query.editMode === 'builder') {
readBucketName = query.builderConfig.buckets[0] || ''
} else {
const text = query.text
const splitBucket = text.split('bucket:')
const splitQuotes = splitBucket[1].split('"')
readBucketName = splitQuotes[1]
}
return readBucketName
}
private get getReadAuthorizations() {
const authorizations = this.props.tokens
const readBucketName = this.readBucketName
return authorizations.filter(auth =>
auth.permissions.some(
permission =>
permission.action === 'read' &&
permission.resource.type === 'buckets' &&
(!permission.resource.name ||
permission.resource.name === readBucketName)
)
)
}
private get getWriteAuthorizations() {
const authorizations = this.props.tokens
const {
taskOptions: {toBucketName},
} = this.props
return authorizations.filter(auth =>
auth.permissions.some(
permission =>
permission.action === 'write' &&
permission.resource.type === 'buckets' &&
(!permission.resource.name ||
permission.resource.name === toBucketName)
)
)
}
private get isFormValid(): boolean {
const {
taskOptions: {name, cron, interval},

View File

@ -30,7 +30,7 @@ export default class TaskTokenDropdown extends PureComponent<Props> {
}
private get dropdownTokens(): JSX.Element[] {
const {tokens} = this.props
if (tokens.length > 0) {
if (tokens.length) {
return tokens.map(t => (
<Dropdown.Item id={t.id} key={t.id} value={t}>
{t.description || 'Name this token'}
@ -39,7 +39,7 @@ export default class TaskTokenDropdown extends PureComponent<Props> {
}
return [
<Dropdown.Item id="no-tokens" key="no-tokens" value="no-tokens">
{'You dont have any tokens with appropriate permissions for this use'}
You dont have any tokens with appropriate permissions for this use
</Dropdown.Item>,
]
}
@ -47,10 +47,8 @@ export default class TaskTokenDropdown extends PureComponent<Props> {
private get selectedID(): string {
const {selectedToken, tokens} = this.props
if (tokens.length > 0) {
if (selectedToken) {
return selectedToken.id
}
if (tokens.length && selectedToken) {
return selectedToken.id
}
return 'no-tokens'
}