/
create-integration.component.ts
77 lines (70 loc) · 2.72 KB
/
create-integration.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component, DestroyRef, inject } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { catchError, tap } from 'rxjs/operators';
import { EMPTY } from 'rxjs';
import { CreateIntegrationPayload } from '../integrations.model';
import { IntegrationsService } from '../../../services-ngx/integrations.service';
import { SnackBarService } from '../../../services-ngx/snack-bar.service';
@Component({
selector: 'app-create-integration',
templateUrl: './create-integration.component.html',
styleUrls: ['./create-integration.component.scss'],
})
export class CreateIntegrationComponent {
public isLoading = false;
private destroyRef = inject(DestroyRef);
public informationForm = this.formBuilder.group({
name: ['', [Validators.required, Validators.maxLength(50), Validators.minLength(1)]],
description: ['', Validators.maxLength(250)],
});
constructor(
private integrationsService: IntegrationsService,
private formBuilder: FormBuilder,
private readonly router: Router,
private activatedRoute: ActivatedRoute,
private snackBarService: SnackBarService,
) {}
public onSubmit(): void {
const payload: CreateIntegrationPayload = {
name: this.informationForm.controls.name.getRawValue(),
description: this.informationForm.controls.description.getRawValue(),
provider: 'amazon',
};
this.isLoading = true;
this.integrationsService
.createIntegration(payload)
.pipe(
tap(() => {
this.isLoading = false;
this.snackBarService.success(`Integration ${payload.name} created successfully`);
}),
catchError((_) => {
this.isLoading = false;
this.snackBarService.error('An error occurred. Integration not created');
return EMPTY;
}),
takeUntilDestroyed(this.destroyRef),
)
.subscribe(() => {
this.isLoading = false;
this.router.navigate(['..'], { relativeTo: this.activatedRoute });
});
}
}