Skip to content

Commit 88fa419

Browse files
paulbertlmmrssa
authored andcommitted
New course title validation on exam add (fixes #2939) (#2950)
1 parent b2fbcc6 commit 88fa419

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/app/courses/add-courses/courses-add.component.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ export class CoursesAddComponent implements OnInit, OnDestroy {
6565
courseTitle: [
6666
'',
6767
Validators.required,
68-
// an arrow function is for lexically binding 'this' otherwise 'this' would be undefined
69-
this.route.snapshot.url[0].path === 'update'
70-
? ac => this.validatorService.isNameAvailible$(this.dbName, 'courseTitle', ac, this.route.snapshot.params.id)
71-
: ac => this.validatorService.isUnique$(this.dbName, 'courseTitle', ac)
68+
this.courseTitleValidator(this.route.snapshot.paramMap.get('id') || this.coursesService.course._id)
7269
],
7370
description: [ '', Validators.required ],
7471
languageOfInstruction: '',
@@ -90,6 +87,10 @@ export class CoursesAddComponent implements OnInit, OnDestroy {
9087
});
9188
}
9289

90+
courseTitleValidator(id: string = '') {
91+
return ac => this.validatorService.isUnique$(this.dbName, 'courseTitle', ac, { selectors: { '_id': { '$ne': id } } });
92+
}
93+
9394
ngOnInit() {
9495
if (this.route.snapshot.url[0].path === 'update') {
9596
this.couchService.get('courses/' + this.route.snapshot.paramMap.get('id'))
@@ -107,6 +108,7 @@ export class CoursesAddComponent implements OnInit, OnDestroy {
107108
});
108109
}
109110
if (this.route.snapshot.params.continue === 'true') {
111+
this.documentInfo = { '_rev': this.coursesService.course._rev, '_id': this.coursesService.course._id };
110112
this.setFormAndSteps(this.coursesService.course);
111113
this.submitAddedExam();
112114
}
@@ -173,7 +175,9 @@ export class CoursesAddComponent implements OnInit, OnDestroy {
173175
if (shouldNavigate) {
174176
this.navigateBack();
175177
}
178+
this.courseForm.get('courseTitle').setAsyncValidators(this.courseTitleValidator(response.id));
176179
this.documentInfo = { '_id': response.id, '_rev': response.rev };
180+
this.coursesService.course = { ...this.documentInfo };
177181
this.planetMessageService.showMessage(message);
178182
}
179183

src/app/courses/courses.service.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ import { StateService } from '../shared/state.service';
1313
export class CoursesService {
1414
private dbName = 'courses';
1515
private progressDb = 'courses_progress';
16-
course: any = { _id: '' };
16+
private _course: any = {};
17+
get course() {
18+
return this._course;
19+
}
20+
set course(newCourse: any) {
21+
this._course = { ...this._course, ...newCourse };
22+
}
1723
progress: any;
1824
submission: any = { courseId: '', examId: '' };
1925
private courseUpdated = new Subject<{ progress: any, course: any }>();
@@ -72,7 +78,7 @@ export class CoursesService {
7278
}
7379

7480
reset() {
75-
this.course = { _id: '' };
81+
this._course = {};
7682
this.stepIndex = -1;
7783
this.returnUrl = '';
7884
}

0 commit comments

Comments
 (0)