diff --git a/ui/src/dataExplorer/components/SaveAsTaskForm.tsx b/ui/src/dataExplorer/components/SaveAsTaskForm.tsx index 1a672f8983..d7dde1c6d0 100644 --- a/ui/src/dataExplorer/components/SaveAsTaskForm.tsx +++ b/ui/src/dataExplorer/components/SaveAsTaskForm.tsx @@ -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}, diff --git a/ui/src/tasks/components/TaskTokenDropdown.tsx b/ui/src/tasks/components/TaskTokenDropdown.tsx index 3bb44f918e..ffc261910b 100644 --- a/ui/src/tasks/components/TaskTokenDropdown.tsx +++ b/ui/src/tasks/components/TaskTokenDropdown.tsx @@ -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 don’t have any tokens with appropriate permissions for this use'} + You don’t 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' }