|
- import {AfterViewInit, Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
- import {
- PartnerJsonld,
- ProductJsonld,
- ProductService,
- TaskJsonld,
- TaskService,
- UserService
- } from "@app/core/api/v1";
- import {ModalStatus} from "@app/_helpers/modal.states";
- import {FormGroupInitializer} from "@app/_helpers/formgroup.initializer";
- import {FormGroup} from "@angular/forms";
- import {taskForm} from "@app/_forms/apiForms";
- import {AppHelperService} from "@app/_helpers/app-helper.service";
- import {SearchSelectComponent} from "@app/_components/search-select/search-select.component";
- import {ListGetDataFunctionType} from "@app/_components/list/list-get-data-function-type";
-
- @Component({
- selector: 'app-new-task',
- templateUrl: './new-task.component.html',
- styleUrl: './new-task.component.scss'
- })
- export class NewTaskComponent implements OnInit, AfterViewInit {
- @Input() public task!: TaskJsonld;
- @Input() public partner!: PartnerJsonld;
- @Output() public submit: EventEmitter<ModalStatus> = new EventEmitter<ModalStatus>();
- @ViewChild('userSearchSelect', {static: false}) userSearchSelect!: SearchSelectComponent;
- @ViewChild('productSearchSelect', {static: false}) productSearchSelect!: SearchSelectComponent;
- protected readonly SearchSelectComponent = SearchSelectComponent;
-
- protected taskForm: FormGroup;
- protected dueAtValue: string;
-
- constructor(
- protected taskService: TaskService,
- protected userService: UserService,
- protected productService: ProductService,
- protected appHelperService: AppHelperService
- ) {
- this.taskForm = taskForm;
- this.dueAtValue = "";
- }
-
- ngOnInit(): void {
- this.taskForm = FormGroupInitializer.initFormGroup(this.taskForm, this.task);
- if (this.taskForm.get('dueAt')?.value !== null) {
- this.taskForm.get('dueAt')?.setValue(this.taskForm.value.dueAt.slice(0, 10));
- this.dueAtValue = this.taskForm.get('dueAt')?.value;
- }
- const prio = this.taskForm.get('prio');
- // Set first radio checked
- if (prio && !prio.value) {
- prio.patchValue('low');
- }
- }
-
- ngAfterViewInit(): void {
- }
-
- getUsers: ListGetDataFunctionType = (index: number, pageSize: number, term?: string) => {
- return this.userService.usersGetCollection(
- index,
- pageSize,
- undefined,
- undefined,
- term
- );
- }
-
- getProducts: ListGetDataFunctionType = (index: number, pageSize: number, term?: string) => {
- return this.productService.productsGetCollection(
- index,
- pageSize,
- term
- );
- }
-
- protected onDueAtChange(selectedItem: any) {
- // Set T12:00 for correct string
- let selectedItemValue = selectedItem.target.value + "T12:00";
- this.taskForm.get('dueAt')?.setValue(selectedItemValue);
- }
-
- protected onSubmit() {
- if (this.taskForm.valid) {
- if (this.task.id === null || this.task.id === undefined) {
- // Create new task
- this.taskService.tasksPost(
- this.taskForm.value as TaskJsonld
- ).subscribe(
- data => {
- this.taskForm.reset();
- this.submit.emit(ModalStatus.Submitted);
- }
- );
- } else {
- // Edit task
- this.taskService.tasksIdPatch(
- this.appHelperService.extractId(this.task.id),
- this.taskForm.value as TaskJsonld
- ).subscribe(
- data => {
- this.taskForm.reset();
- this.submit.emit(ModalStatus.Submitted);
- }
- );
- }
- }
- }
- }
|