-
Notifications
You must be signed in to change notification settings - Fork 39
/
courses-step.component.ts
120 lines (105 loc) Β· 3.66 KB
/
courses-step.component.ts
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
import { Component, Input, Output, EventEmitter, OnDestroy, ViewEncapsulation } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup } from '@angular/forms';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { CoursesService } from '../courses.service';
import { DialogsAddResourcesComponent } from '../../shared/dialogs/dialogs-add-resources.component';
import { DialogsLoadingService } from '../../shared/dialogs/dialogs-loading.service';
import { ChatService } from '../../shared/chat.service';
@Component({
selector: 'planet-courses-step',
templateUrl: 'courses-step.component.html',
styleUrls: [ 'courses-step.scss' ],
encapsulation: ViewEncapsulation.None
})
export class CoursesStepComponent implements OnDestroy {
@Input() steps: any[];
@Output() stepsChange = new EventEmitter<any>();
@Output() addStepEvent = new EventEmitter<void>();
stepForm: FormGroup;
dialogRef: MatDialogRef<DialogsAddResourcesComponent>;
activeStep: any;
activeStepIndex = -1;
rating: Text;
private onDestroy$ = new Subject<void>();
constructor(
private router: Router,
private fb: FormBuilder,
private dialog: MatDialog,
private coursesService: CoursesService,
private dialogsLoadingService: DialogsLoadingService,
private chatService: ChatService
) {
this.stepForm = this.fb.group({
id: '',
stepTitle: '',
description: ''
});
this.stepForm.valueChanges.pipe(takeUntil(this.onDestroy$)).subscribe(value => {
this.steps[this.activeStepIndex] = { ...this.activeStep, ...value };
this.stepsChange.emit(this.steps);
});
}
ngOnDestroy() {
this.onDestroy$.next();
this.onDestroy$.complete();
}
stepClick(index: number) {
this.activeStepIndex = index;
if (index > -1) {
this.activeStep = this.steps[index];
this.stepForm.patchValue(this.steps[index]);
}
}
addResources() {
this.dialogRef = this.dialog.open(DialogsAddResourcesComponent, {
width: '80vw',
data: {
okClick: this.resourcsDialogOkClick.bind(this),
excludeIds: this.steps[this.activeStepIndex].resources.map((resource: any) => resource._id),
canAdd: true
} });
}
resourcsDialogOkClick(selected: any) {
this.steps[this.activeStepIndex].resources = [
...this.steps[this.activeStepIndex].resources,
...selected.map(res => res.doc)
];
this.activeStep = this.steps[this.activeStepIndex];
this.stepsChange.emit(this.steps);
this.dialogsLoadingService.stop();
this.dialogRef.close();
}
removeResource(position: number) {
this.steps[this.activeStepIndex].resources.splice(position, 1);
}
addExam(type = 'exam') {
this.coursesService.stepIndex = this.activeStepIndex;
if (this.activeStep[type]) {
this.router.navigate([ '/courses/update/exam/', this.activeStep[type]._id, { type } ]);
} else {
this.router.navigate([ '/courses/exam/', { type } ]);
}
}
stepsMoved(steps) {
this.steps = steps;
this.stepsChange.emit(this.steps);
}
addStep() {
this.addStepEvent.emit();
}
rateStep() {
const content = `In 3 clear blobs, 1. provide a rating for this course step(granular rating out of 100%), 2. Explain why you gave the rating 3. Explain how to improve the course step
${this.stepForm.get('stepTitle').value} - ${this.stepForm.get('description').value.text}`;
this.chatService.getPrompt(content).subscribe(
(completion: any) => {
this.rating = completion?.chat;
},
(error: any) => {
console.log(error);
}
);
}
}