-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-class-form.component.ts
More file actions
136 lines (127 loc) · 4.25 KB
/
add-class-form.component.ts
File metadata and controls
136 lines (127 loc) · 4.25 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { minValueValidator } from './../../../utils/validators/min-value-validator';
import { patternValidator } from "src/utils/validators/pattern-validator";
import { maxLengthValidator } from "./../../../utils/validators/max-length-validator";
import { ITeacherToSelect } from "./../../models/ITacherToSelect";
import { maxValueValidator } from "./../../../utils/validators/max-value-validator";
import { FormGroup, FormBuilder, FormControl } from "@angular/forms";
import { Component, OnInit } from "@angular/core";
import { IFormField } from "src/app/models/IFormField";
import { requiredValidator } from "src/utils/validators/required-validator";
import { validationHelper } from "src/utils/helpers/validation-helper";
import { apiUrl } from "src/constants/urls";
import { HttpClient } from "@angular/common/http";
import { HttpOptionsService } from "src/app/services/http-options/http-options.service";
import { PupilsService } from "src/app/services/pupils/pupils.service";
import { IPupilToSelect } from "src/app/models/IPupilToSelect";
import { TeachersService } from 'src/app/services/teachers/teachers.service';
import { ClassesService } from 'src/app/services/classes/classes.service'
@Component({
selector: "yps-add-class-form",
templateUrl: "./add-class-form.component.html",
styleUrls: [
"./add-class-form.component.scss",
"../../../scss/adding-forms.scss"
]
})
export class AddClassFormComponent implements OnInit {
form: FormGroup;
formIsOpen: boolean = false;
teachers: ITeacherToSelect[];
listOfPupils: IPupilToSelect[];
showPupils: boolean = false;
fields: IFormField[] = [
{
id: "number-field",
type: "number",
label: "number",
placeholder: "enter number of the class",
name: "number",
errorMsg: null
},
{
id: "character-field",
type: "text",
label: "character",
placeholder: "enter character of the class",
name: "character",
errorMsg: null
}
];
constructor(
private formBuilder: FormBuilder,
private http: HttpClient,
private teachersService: TeachersService,
private pupilsService: PupilsService,
private httpOptionsService: HttpOptionsService,
private classesService: ClassesService
) {}
toggleForm = () => (this.formIsOpen = !this.formIsOpen);
getTeachersToSelectData = () => {
this.teachersService
.getTeachersToSelect()
.subscribe(data => (this.teachers = data));
};
getPupilsToSelectData = (numb: number) => {
this.pupilsService
.getPupilsToSelect(numb)
.subscribe(data => (this.listOfPupils = data));
};
onSubmit = () => {
const { fields, isValid } = validationHelper(
this.form.controls,
this.fields
);
this.fields = fields;
console.info(`Created class form is ${isValid ? "valid" : "invalid"}`);
if (isValid) {
console.log("value", this.form.value);
let request = {
...this.form.value,
number: parseInt(this.form.value.number, 10)
};
return this.http
.post(apiUrl + "/Classes", request, this.httpOptionsService.options)
.subscribe((successRes: any) => {
this.toggleForm();
console.log("add classes response", successRes);
this.classesService.getClassesBySchool();
});
}
};
ngOnInit() {
this.getTeachersToSelectData();
this.httpOptionsService.loadHeaders();
this.form = this.formBuilder.group({
character: [
null,
[
requiredValidator("character is required"),
maxLengthValidator(1, "character must be 1 symbol"),
patternValidator(/[A-Za-zÀ-ÿ]/, "must be single character"),
]
],
number: [
null,
[
requiredValidator("number is required"),
maxValueValidator(12, "max value is 12"),
minValueValidator(1,"min value is 1")
]
],
classTeacherId: [null, [requiredValidator("teacher is required")]],
selectedPupils: [null]
});
this.onChanges();
}
onChanges(): void {
this.form.controls.number.valueChanges.subscribe(val => {
if (val != "") {
this.showPupils = true;
console.log(typeof val);
this.getPupilsToSelectData(val);
} else {
this.showPupils = false;
}
});
}
}