Skip to content

Commit

Permalink
Xử lý sau khi click submit
Browse files Browse the repository at this point in the history
  • Loading branch information
anhducc13 committed Apr 5, 2019
1 parent 893bfa2 commit 360736f
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Management/src/app/exams/form/form.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ <h4 class="card-title">Add New Exam</h4>
</div>
<div class="row">
<legend>Answers</legend>
<div class="col-md-2 col-sm-4" *ngFor="let item of currentExam.answers">
<div class="col-md-2 col-sm-4" *ngFor="let item of currentExam.answers;let index = index;">
<label class="control-label">Question {{item.number}}:
<span class="star">*</span>
</label>
<input [value]="item.value"
<input [(ngModel)]="currentExam.answers[index].value"
formControlName="answer"
type="text"
class="form-control">
Expand Down
119 changes: 119 additions & 0 deletions Management/src/app/exams/form/form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,125 @@ export class FormComponent implements OnInit {
console.log(this.currentExam.answers);
}

/*--------------------------------------------------------------
| onSubmitExam(id: string = '') id === '' => edit && id !== '' => add
----------------------------------------------------------------*/
onSubmitExam(id: string = '') {
if (this.currentExam._id) {
id = this.currentExam._id;
}
if (this.formExam.valid) {
const modified = {
user_id: this.userLogin._id,
user_name: this.userLogin.local.username,
time: Date.now()
};
const created = {
user_id: this.userLogin._id,
user_name: this.userLogin.local.username,
time: Date.now()
};
// Create form data to post exam
let formData: FormData = new FormData();
formData.append('id', id);
formData.append('name', this.currentExam.name);
formData.append('slug', this.currentExam.slug);
formData.append('ordering', this.currentExam.ordering.toString());
formData.append('price', this.currentExam.price.toString());
formData.append('status', this.currentExam.status);
formData.append('special', this.currentExam.special);
formData.append('subject_id', this.currentExam.subject.id);
this.subjects.forEach((subj) => {
if (subj._id === this.currentExam.subject.id) {
formData.append('subject_name', subj.name);
}
})
formData.append('level', this.currentExam.level);
formData.append('rates', this.currentExam.rates.toString());
formData.append('time', this.currentExam.time.toString());
formData.append('content', this.currentExam.content);
formData.append('onlineExam', this.currentExam.isOnlineExam ? 'online' : 'offline');
formData.append('answers', JSON.stringify(this.currentExam.answers));
if (this.currentExam.isOnlineExam) {
formData.append('starttime', this.currentExam.timeStart.toString());
}
formData.append('modified', JSON.stringify(modified));
formData.append('created', JSON.stringify(created));
let flagSubmit = true;
if (id === '') { // Add
if (this.selectedExamPdf === null) {
flagSubmit = false;
showAlert('warning',
'Select Exam PDF To Upload' ,
'Click to continue !',
false,
'btn btn-warning');
} else {
formData.append('exam_pdf', this.selectedExamPdf, this.selectedExamPdf.name);
}
if (this.selectedThumb === null) {
flagSubmit = false;
showAlert('warning',
'Select Thumb To Upload' ,
'Click to continue !',
false,
'btn btn-warning');
} else {
formData.append('thumb', this.selectedThumb, this.selectedThumb.name);
}
} else {
const exam = this.formExam.value;
formData.append('imageOld', exam.imageOld);
if (this.selectedThumb !== null) {
formData.append('thumb', this.selectedThumb, this.selectedThumb.name);
}
formData.append('pdfOld', exam.pdfOld);
if (this.selectedExamPdf !== null) {
formData.append('exam_pdf', this.selectedExamPdf, this.selectedExamPdf.name);
}
}
if (this.currentExam.answers && this.currentExam.answers.length > 0) {
this.currentExam.answers.forEach(item => {
if (item.value === '') {
flagSubmit = false;
showAlert('warning',
'Enter full answer' ,
'Click to continue !',
false,
'btn btn-warning');
return;
}
});
}
if (flagSubmit) {
this.ngProgress.start();
this._examService.saveUser(formData)
.subscribe(
data => {
console.log(data);
},
error => this.reloadPageIfError(),
() => {
this.ngProgress.done();
showAlert('success',
'Success' ,
'Click to continue !',
false,
'btn btn-success');
});
}
} else {
validateAllFormFields(this.formExam);
}
}

onChangeThumb(e) {
this.selectedThumb = e.target.files[0];
}

onChangeExamPdf(e) {
this.selectedExamPdf = e.target.files[0];
}

reloadPageIfError() {
SwalConfirm('Click Ok to reload the page', () => {
Expand Down
8 changes: 8 additions & 0 deletions Management/src/app/shared/services/exams.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@ export class ExamsService {
return this.changeSpecialMulti(objUpdate);
}
}

saveUser(formData: any): Observable<IExam> {
return this._httpService.post(this.apiUrl, formData)
.pipe(
tap(_ => {}),
catchError(this.handleError<any>('saveUser'))
);
}
}

0 comments on commit 360736f

Please sign in to comment.