Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/app/_services/data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class DataService {
return this.httpClient.get<Data[]>(environment.backendUrl + '/data').toPromise();
}

getCount(): Promise<any> {
return this.httpClient.get<any>(environment.backendUrl + '/data/count').toPromise();
}

add(data: Data): Promise<any> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be number here but not any :)

return this.httpClient.post(environment.backendUrl + '/data', data).toPromise();
}
Expand Down
23 changes: 17 additions & 6 deletions src/app/data/add/add.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {isNameExists} from '../../_validators/uniq-name.validator';
styleUrls: ['./add.component.scss']
})
export class AddComponent implements OnInit {
dataCount: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Number, not string

form: FormGroup;
inProgress = false;
type: FormControl = new FormControl('', Validators.required);
Expand Down Expand Up @@ -39,6 +40,9 @@ export class AddComponent implements OnInit {
}

ngOnInit() {
this.dataService.getCount().then(data => {
this.dataCount = data
})
}

back() {
Expand All @@ -47,12 +51,19 @@ export class AddComponent implements OnInit {

onSubmit() {
this.inProgress = true;
const data = this.form.value;
data.name = this.getFormattedName();
this.dataService.add(data).then(() => {
this.alertService.success('Data added successfully');
this.router.navigate(['/data/' + data.name]);
}).finally(() => this.inProgress = false);

if(+this.dataCount > 100) {
this.alertService.success('You cannot have more than 100 records!');
this.router.navigate(['/data']);
this.inProgress = false;
} else {
const data = this.form.value;
data.name = this.getFormattedName();
this.dataService.add(data).then(() => {
this.alertService.success('Data added successfully');
this.router.navigate(['/data/' + data.name]);
}).finally(() => this.inProgress = false);
}
}

onTypeChange() {
Expand Down