diff --git a/src/app/container/register-tool/register-tool.service.ts b/src/app/container/register-tool/register-tool.service.ts index e1cb67c072..bba2cd8b15 100644 --- a/src/app/container/register-tool/register-tool.service.ts +++ b/src/app/container/register-tool/register-tool.service.ts @@ -16,18 +16,19 @@ import { HttpErrorResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; +// This line is super important for jQuery to work across the website for some reason +import * as $ from 'jquery'; import { BehaviorSubject } from 'rxjs'; import { ContainerService } from './../../shared/container.service'; import { Repository } from './../../shared/enum/Repository.enum'; import { StateService } from './../../shared/state.service'; import { ContainersService } from './../../shared/swagger/api/containers.service'; +import { HostedService } from './../../shared/swagger/api/hosted.service'; import { MetadataService } from './../../shared/swagger/api/metadata.service'; import { DockstoreTool } from './../../shared/swagger/model/dockstoreTool'; import { Tool } from './tool'; -import { HostedService } from './../../shared/swagger/api/hosted.service'; -// This line is super important for jQuery to work across the website for some reason -import * as $ from 'jquery'; +import { finalize } from 'rxjs/operators'; @Injectable() export class RegisterToolService { @@ -95,21 +96,25 @@ export class RegisterToolService { } registerTool(newTool: Tool, customDockerRegistryPath) { - this.setTool(newTool); - const normalizedToolObj: DockstoreTool = this.getNormalizedToolObj(newTool, customDockerRegistryPath); - this.containersService.registerManual(normalizedToolObj).subscribe(response => { - this.setToolRegisterError(null); - this.stateService.setRefreshMessage('Registering new tool...'); - this.containersService.refresh(response.id).subscribe((refreshResponse: DockstoreTool) => { - this.stateService.setRefreshMessage(null); - this.setIsModalShown(false); - this.containerService.addToTools(this.tools, refreshResponse); - this.containerService.setTool(refreshResponse); - this.router.navigateByUrl('/my-tools' + '/' + refreshResponse.tool_path); - }); - // Use types instead - }, error => this.setToolRegisterError(error) - ); + this.setToolRegisterError(null); + this.setTool(newTool); + this.stateService.setRefreshMessage('Registering new tool...'); + const normalizedToolObj: DockstoreTool = this.getNormalizedToolObj(newTool, customDockerRegistryPath); + this.containersService.registerManual(normalizedToolObj) + .subscribe((registeredDockstoreTool: DockstoreTool) => { + this.stateService.setRefreshMessage('Refreshing new tool...'); + this.containersService.refresh(registeredDockstoreTool.id).pipe(finalize(() => { + this.stateService.setRefreshMessage(null); + })).subscribe((refreshedDockstoreTool: DockstoreTool) => { + this.setIsModalShown(false); + this.containerService.addToTools(this.tools, refreshedDockstoreTool); + this.containerService.setTool(refreshedDockstoreTool); + this.router.navigateByUrl('/my-tools' + '/' + refreshedDockstoreTool.tool_path); + }, (error: HttpErrorResponse) => this.setToolRegisterError(error)); + }, (error: HttpErrorResponse) => { + this.stateService.setRefreshMessage(null); + this.setToolRegisterError(error); + }); } /** diff --git a/src/app/workflow/register-workflow-modal/register-workflow-modal.service.spec.ts b/src/app/workflow/register-workflow-modal/register-workflow-modal.service.spec.ts index 500cb6987a..fa1e07cc7e 100644 --- a/src/app/workflow/register-workflow-modal/register-workflow-modal.service.spec.ts +++ b/src/app/workflow/register-workflow-modal/register-workflow-modal.service.spec.ts @@ -74,12 +74,7 @@ describe('Service: RegisterWorkflowModal', () => { service.setWorkflowRepository('asdf'); service.workflow.subscribe(workflow => expect(workflow).toEqual(expectedWorkflow)); })); - it('should set register error and clear refreshing state', inject([RegisterWorkflowModalService, StateService], - (service: RegisterWorkflowModalService, stateService: StateService) => { - service.setWorkflowRegisterError('oh no!', 'oh yes'); - service.workflowRegisterError$.subscribe(error => expect(error).toEqual(expectedError)); - stateService.refreshMessage$.subscribe(refreshMessage => expect(refreshMessage).toBeFalsy()); - })); + it('should set register workflow and clear refreshing state and error', inject([RegisterWorkflowModalService, StateService], (service: RegisterWorkflowModalService, stateService: StateService) => { service.registerWorkflow(); diff --git a/src/app/workflow/register-workflow-modal/register-workflow-modal.service.ts b/src/app/workflow/register-workflow-modal/register-workflow-modal.service.ts index 15ccaed2f6..8e82686cfa 100644 --- a/src/app/workflow/register-workflow-modal/register-workflow-modal.service.ts +++ b/src/app/workflow/register-workflow-modal/register-workflow-modal.service.ts @@ -13,9 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { HttpErrorResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; import { BehaviorSubject, Observable } from 'rxjs'; +import { finalize } from 'rxjs/operators'; import { DescriptorLanguageService } from './../../shared/entry/descriptor-language.service'; import { StateService } from './../../shared/state.service'; @@ -59,13 +61,25 @@ export class RegisterWorkflowModalService { this.isModalShown$.next(isModalShown); } - setWorkflowRegisterError(message: any, errorDetails) { - const error = { - message: message, - errorDetails: errorDetails - }; - this.workflowRegisterError$.next(error); - this.stateService.refreshMessage$.next(null); + setWorkflowRegisterError(error: HttpErrorResponse) { + let errorObj = null; + if (error) { + if (error.status === 0) { + errorObj = { + message: 'The webservice is currently down, possibly due to load. Please wait and try again later.', + errorDetails: '' + }; + } else { + errorObj = { + message: 'The webservice encountered an error trying to create this ' + + 'workflow, please ensure that the workflow attributes are ' + + 'valid.', + errorDetails: '[HTTP ' + error.status + '] ' + error.statusText + ': ' + + error.error + }; + } + } + this.workflowRegisterError$.next(errorObj); } setWorkflow(workflow: Workflow) { @@ -78,7 +92,8 @@ export class RegisterWorkflowModalService { } registerWorkflow() { - this.stateService.setRefreshMessage('Registering workflow...'); + this.clearWorkflowRegisterError(); + this.stateService.setRefreshMessage('Registering new workflow...'); this.workflowsService.manualRegister( this.actualWorkflow.repository, this.actualWorkflow.gitUrl, @@ -86,28 +101,19 @@ export class RegisterWorkflowModalService { this.actualWorkflow.workflowName, this.actualWorkflow.descriptorType, this.actualWorkflow.defaultTestParameterFilePath).subscribe(result => { - this.workflowsService.refresh(result.id).subscribe(refreshResult => { + this.stateService.setRefreshMessage('Refreshing new workflow...'); + this.workflowsService.refresh(result.id).pipe(finalize(() => { + this.stateService.setRefreshMessage(null); + })).subscribe(refreshResult => { this.workflows.push(refreshResult); this.workflowService.setWorkflows(this.workflows); - this.stateService.setRefreshMessage(null); this.setIsModalShown(false); - this.clearWorkflowRegisterError(); this.router.navigateByUrl('/my-workflows' + '/' + refreshResult.full_workflow_path); - }, error => this.stateService.setRefreshMessage(null)); + }, error => this.setWorkflowRegisterError(error)); }, error => { - if (error) { - if (error.status === 0) { - this.setWorkflowRegisterError('The webservice is currently down, possibly due to load. ' + - 'Please wait and try again later.', ''); - } else { - this.setWorkflowRegisterError('The webservice encountered an error trying to create this ' + - 'workflow, please ensure that the workflow attributes are ' + - 'valid and the same image has not already been registered.', '[HTTP ' + error.status + '] ' + error.statusText + ': ' + - error.error); - } - } - } - ); + this.stateService.setRefreshMessage(null); + this.setWorkflowRegisterError(error); + }); } /** @@ -115,30 +121,21 @@ export class RegisterWorkflowModalService { * @param hostedWorkflow hosted workflow object */ registerHostedWorkflow(hostedWorkflow) { - this.stateService.setRefreshMessage('Registering workflow...'); + this.clearWorkflowRegisterError(); + this.stateService.setRefreshMessage('Registering new workflow...'); this.hostedService.createHostedWorkflow( hostedWorkflow.name, - hostedWorkflow.descriptorType).subscribe(result => { + hostedWorkflow.descriptorType).pipe(finalize(() => { + this.stateService.setRefreshMessage(null); + })).subscribe(result => { this.workflows.push(result); this.workflowService.setWorkflows(this.workflows); - this.stateService.setRefreshMessage(null); this.setIsModalShown(false); this.clearWorkflowRegisterError(); this.router.navigateByUrl('/my-workflows' + '/' + result.full_workflow_path); }, error => { - if (error) { - if (error.status === 0) { - this.setWorkflowRegisterError('The webservice is currently down, possibly due to load. ' + - 'Please wait and try again later.', ''); - } else { - this.setWorkflowRegisterError('The webservice encountered an error trying to create this ' + - 'workflow, please ensure that the workflow attributes are ' + - 'valid and the same workflow has not already been registered.', '[HTTP ' + error.status + '] ' + error.statusText + ': ' + - error.error); - } - } - } - ); + this.setWorkflowRegisterError(error); + }); } friendlyRepositoryKeys(): Array {