Skip to content

Instantly share code, notes, and snippets.

@michaelilyin
Last active May 19, 2020 08:02
Show Gist options
  • Save michaelilyin/c71a9fd8f3b480ab4133433af6abeec7 to your computer and use it in GitHub Desktop.
Save michaelilyin/c71a9fd8f3b480ab4133433af6abeec7 to your computer and use it in GitHub Desktop.
Find an issue and propose decision
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { TasksService } from '../tasks.service';
import { Location } from '@angular/common';
interface TaskForm {
name: string;
description: string;
}
@Component({
selector: 'hrh-tasks',
templateUrl: './tasks.component.html',
styleUrls: ['./tasks.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class TasksComponent implements OnInit {
form: FormGroup;
constructor(
private readonly fb: FormBuilder,
private readonly taskService: TasksService,
private readonly location: Location
) {
this.form = fb.group({
name: this.fb.control('', Validators.required),
description: this.fb.control('', [Validators.required, Validators.maxLength(500)])
});
}
ngOnInit(): void {}
handleFormSubmit(form: TaskForm) {
this.taskService.createTask({
name: form.name,
description: form.description
});
this.location.back();
}
}
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class TasksService {
constructor(private http: HttpClient) {}
createTask(body: { name: string; description: string }): Observable<unknown> {
return this.http.get<User[]>('/tasks', body);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment