Skip to content

Commit

Permalink
thêm phần thông thành công sau khi thêm hoặc sửa thông tin môn học
Browse files Browse the repository at this point in the history
  • Loading branch information
duykypaul committed Apr 7, 2019
1 parent 393e006 commit 1b30e9c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 32 deletions.
8 changes: 4 additions & 4 deletions Management/src/app/shared/services/subjects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import {ISubject} from '../defines/subject';
import {AppSettings} from '../helper/app.setting';
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {Observable, of} from "rxjs";
import {Observable, of, Subject} from "rxjs";
import {catchError, tap} from "rxjs/operators";
import {Router} from '@angular/router';

Expand Down Expand Up @@ -54,8 +54,9 @@ export class SubjectsService {
);
}

addSubject(newSubject: ISubject): Observable<ISubject> {
return this._httpService.post<ISubject>(`${this.apiUrl}`, newSubject, this.options)
addSubject(newSubject: any): Observable<ISubject> {
console.log(newSubject);
return this._httpService.post(`${this.apiUrl}`, newSubject, this.options)
.pipe(
tap((subject: ISubject) => console.log(`inserted subject = ${JSON.stringify(subject)}`)),
catchError(this.handleError<any>('addSubject'))
Expand All @@ -68,5 +69,4 @@ export class SubjectsService {
return of(result as T);
};
}

}
8 changes: 5 additions & 3 deletions Management/src/app/subjects/form/form.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ <h4 class="card-title">Add New Subject</h4>
</div>
</div>
<div class="modal-footer text-center">
<button *ngIf="!edittingSubject" (click)="onSubmitSubject()" type="button" #closemodal class="btn btn-primary btn-round" data-dismiss="modal">
<i class="material-icons">person_add</i> Submit
<button *ngIf="!edittingSubject" (click)="onSubmitSubject()" type="button"
class="btn btn-primary btn-round" [disabled]='formSubject.invalid'><i
class="material-icons">person_add</i> Submit
</button>
<button *ngIf="edittingSubject" (click)="onSubmitSubject(selectedSubject._id)" type="button" #closemodal class="btn btn-primary btn-round" data-dismiss="modal">
<button *ngIf="edittingSubject" (click)="onSubmitSubject(selectedSubject._id)" type="button"
class="btn btn-primary btn-round" [disabled]='formSubject.invalid'>
<i class="material-icons">person_add</i> Submit
</button>
</div>
Expand Down
10 changes: 6 additions & 4 deletions Management/src/app/subjects/form/form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {ckeConfig, getSlug, validateAllFormFields} from '../../shared/helper/con
import {Router} from '@angular/router';
import {NgProgress} from 'ngx-progressbar';
import { ISubject } from '../../shared/defines/subject';
declare var $: any;
@Component({
selector: 'subjects-form',
templateUrl: './form.component.html',
Expand All @@ -23,10 +24,9 @@ export class FormComponent implements OnInit {
@Input('userLogin') userLogin: any;
@Input() selectedSubject: ISubject;
@Input() edittingSubject: boolean;
@Output("onUpdateForListSubject") currentSubject = new EventEmitter<ISubject>();
@Output("onSubmit") onSubmit = new EventEmitter<boolean>();


@ViewChild('closemodal') closemodal: ElementRef;

constructor(
private _formBuilder: FormBuilder,
private _subjectService: SubjectsService,
Expand Down Expand Up @@ -54,6 +54,8 @@ export class FormComponent implements OnInit {
};
}

@Output("onSubmit ") currentSubject = new EventEmitter<ISubject>();

onSubmitSubject(id: string = '') {
if (this.formSubject.valid) {
const subject: ISubject = this.formSubject.value;
Expand All @@ -63,7 +65,7 @@ export class FormComponent implements OnInit {
validateAllFormFields(this.formSubject);
}
this.edittingSubject = false;
this.onSubmit.emit(this.edittingSubject);
this.ngOnInit();
}

reloadPageIfError() {
Expand Down
4 changes: 2 additions & 2 deletions Management/src/app/subjects/list/list.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<h4 class="card-title">Subjects List</h4>
<div class="toolbar">
<div class="row">
<div class="col-sm-6 col-md-4">
<div class="col-sm-6 col-md-2">
<mat-form-field>
<mat-select placeholder="Choosen Status" [(ngModel)]="statusSelect"
(selectionChange)="filterSubjects()">
<mat-option value="all">
Choose Status
All
</mat-option>
<mat-option *ngFor="let status of statuses" [value]="status.value">
{{status.viewValue | capitalize}}
Expand Down
56 changes: 45 additions & 11 deletions Management/src/app/subjects/list/list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export class ListComponent implements OnInit, OnChanges {
pagedItems: ISubject[];
selectAll: boolean = false;
@Input('userLogin') userLogin: any;

@Input('currentSubject') currentSubject: ISubject;
@Input('edittingSubject') edittingSubject: Boolean;

constructor(
private _subjectService: SubjectsService,
Expand Down Expand Up @@ -68,16 +68,50 @@ export class ListComponent implements OnInit, OnChanges {
}
};

this._subjectService.deleteSubject(this.currentSubject._id).subscribe(_ => {
this.pagedItems = this.pagedItems.filter(eachSubject => eachSubject._id !== this.currentSubject._id);
});

this._subjectService.addSubject(objSubject).subscribe(insertedSubject => {
this.pagedItems.push(insertedSubject);
// this._subjectService.deleteSubject(this.currentSubject._id).subscribe(_ => {
// this.pagedItems = this.pagedItems.filter(eachSubject => eachSubject._id !== this.currentSubject._id);
// });

let isAdd = true;
this.allItems.map(value => {
if (value._id === this.currentSubject._id) {
isAdd = false;
value.status = objSubject.status;
value.name = objSubject.name;
value.slug = objSubject.slug;
value.ordering = objSubject.ordering;
value.content = objSubject.content;
objSubject.id = value._id;
this._subjectService.addSubject(objSubject).subscribe(
insertedSubject => {},
error => this.reloadPageIfError(),
() => {
showAlert('success',
'Success' ,
'Click to continue !',
false,
'btn btn-success');
$('#noticeModal').modal('hide');
return;
});
}
});

// this.sortSubjectsBy('name');
// this.setPage(this.currentNumberSubjectOnPage);
if (isAdd) {
this._subjectService.addSubject(objSubject).subscribe(
insertedSubject => {
this.pagedItems.push(insertedSubject);
},
error => this.reloadPageIfError(),
() => {
showAlert('success',
'Success' ,
'Click to continue !',
false,
'btn btn-success');
$('#noticeModal').modal('hide');
return;
});
}
}
}
ngOnInit(): void {
Expand All @@ -96,12 +130,12 @@ export class ListComponent implements OnInit, OnChanges {
this.allItems = data;
showNotification('top', 'right', 1000, 'Have ' + this.allItems.length + ' subject(s)');
this.setPage(this.pager.currentPage);
this.selectAll = false;
},
error => this.reloadPageIfError(),
() => {
// this.ngProgress.done();
// this.loading = false;
this.selectAll = false;
});

}
Expand Down
2 changes: 0 additions & 2 deletions Management/src/app/subjects/subjects.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
[userLogin]="userLogin"
(onEditSubject)="onEditSubject($event)"
[currentSubject]="currentSubject"
[edittingSubject]="edittingSubject"
></subjects-list>
</div>
</div>
Expand All @@ -26,7 +25,6 @@ <h5 class="modal-title" id="myModalLabel">How Do You Become an Affiliate?</h5>
[userLogin]="userLogin"
[selectedSubject]="selectedSubject"
[edittingSubject]="edittingSubject"
(onUpdateForListSubject)="onUpdateForListSubject($event)"
(onSubmit)="onSubmit($event)"
></subjects-form>
</div>
Expand Down
8 changes: 2 additions & 6 deletions Management/src/app/subjects/subjects.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {Component, ElementRef, OnInit, ViewChild, Input} from '@angular/core';
import {ISubject} from '../shared/defines/subject';
import {AuthenticationService} from '../shared/services/authentication.service';

Expand Down Expand Up @@ -28,12 +28,8 @@ export class SubjectsComponent implements OnInit {
this.selectedSubject = e;
this.edittingSubject = true;
}
onUpdateForListSubject(item: ISubject) {
onSubmit(item: ISubject) {
this.currentSubject = item;
// console.log(this.currentSubject);
}

onSubmit(event: boolean) {
this.edittingSubject = event;
}
}

0 comments on commit 1b30e9c

Please sign in to comment.