Commit 3f611be4 by sawit

Modal แก้ไข และการแสดงหน้าจอ

parent 52593de8
...@@ -14,6 +14,7 @@ import { TokenService } from "../../../../shared/services/token.service"; ...@@ -14,6 +14,7 @@ import { TokenService } from "../../../../shared/services/token.service";
import { RoleModel } from "../../../models/role.model"; import { RoleModel } from "../../../models/role.model";
import { AuthModel } from "../../../models/auth.model"; import { AuthModel } from "../../../models/auth.model";
import { ProfileModel } from "../../../models/profile.model"; import { ProfileModel } from "../../../models/profile.model";
import { Observable, forkJoin } from "rxjs";
@Component({ @Component({
selector: 'app-user-setting', selector: 'app-user-setting',
...@@ -39,30 +40,42 @@ export class UserSettingComponent { ...@@ -39,30 +40,42 @@ export class UserSettingComponent {
confirmPassword = "" confirmPassword = ""
itemsList: ProfileModel[] = [] itemsList: ProfileModel[] = []
filterList: ProfileModel[] = [] filterList: ProfileModel[] = []
pagedList: ProfileModel[] = [];
selectModel: ProfileModel = new ProfileModel() selectModel: ProfileModel = new ProfileModel()
selectedItems = new Map<string, boolean>(); selectedItems = new Map<string, boolean>();
roleList: RoleModel[] = [] roleList: RoleModel[] = []
empList: ProfileModel[] = [] empList: ProfileModel[] = []
descName = 'engName' descName = 'engName'
pageIndex = 0; pageIndex = 0;
pageSize = 10;
totalCount = 0;
totalPages = 1;
uploaderProfile: FileUploader | undefined; uploaderProfile: FileUploader | undefined;
uploadErrorMsg: string = ""; uploadErrorMsg: string = "";
modalStatus: "add" | "edit" = "add" modalStatus: "add" | "edit" = "add"
existingEmails: ProfileModel[] = [] existingEmails: ProfileModel[] = []
isEmailDuplicate = false; isEmailDuplicate = false;
password: string = ''; password: string = '';
showPasswordFields = false;
newPassword = '';
confirmNewPassword = '';
newPasswordVisible = false;
confirmNewPasswordVisible = false;
get searchTerm(): string { get searchTerm(): string {
return this._searchTerm; return this._searchTerm;
} }
set searchTerm(val: string) { set searchTerm(val: string) {
this.pageIndex = 0; this.pageIndex = 0;
this.allSelected = false this.allSelected = false;
this._searchTerm = val; this._searchTerm = val;
if (val != '') { if (val != '') {
this.filterList = this.filter(val); this.filterList = this.filter(val);
} else { } else {
this.updatePagedItems() this.filterList = this.itemsList.slice();
} }
this.onSearchChange();
} }
_searchTerm = ""; _searchTerm = "";
...@@ -70,6 +83,14 @@ export class UserSettingComponent { ...@@ -70,6 +83,14 @@ export class UserSettingComponent {
this.uploadConfig() this.uploadConfig()
} }
togglePasswordFields() {
this.showPasswordFields = !this.showPasswordFields;
if (!this.showPasswordFields) {
this.newPassword = '';
this.confirmNewPassword = '';
}
}
uploadConfig() { uploadConfig() {
this.uploaderProfile = new FileUploader({ this.uploaderProfile = new FileUploader({
url: environment.baseUrl + "/files/upload-image", url: environment.baseUrl + "/files/upload-image",
...@@ -125,8 +146,9 @@ export class UserSettingComponent { ...@@ -125,8 +146,9 @@ export class UserSettingComponent {
ngOnInit(): void { ngOnInit(): void {
this.userService.getListsProfile().subscribe(result => { this.userService.getListsProfile().subscribe(result => {
this.itemsList = result.map(item => new ProfileModel(item, this.translate)); this.itemsList = result.map(item => new ProfileModel(item, this.translate));
this.filterList = [...this.itemsList]; this.filterList = this.itemsList.slice();
this.existingEmails = result; this.existingEmails = result;
this.updatePagedItems();
}); });
} }
...@@ -175,13 +197,18 @@ export class UserSettingComponent { ...@@ -175,13 +197,18 @@ export class UserSettingComponent {
this.action = 'add' this.action = 'add'
this.selectModel = new ProfileModel() this.selectModel = new ProfileModel()
this.selectModel.status = 1; this.selectModel.status = 1;
this.showPasswordFields = false;
this.newPassword = '';
this.confirmNewPassword = '';
} }
view(item: ProfileModel) { view(item: ProfileModel) {
this.action = 'edit' this.action = 'edit'
this.confirmPassword = '' this.confirmPassword = ''
this.selectModel = new ProfileModel(item) this.selectModel = new ProfileModel(item)
this.showPasswordFields = false;
this.newPassword = '';
this.confirmNewPassword = '';
} }
save() { save() {
...@@ -233,14 +260,34 @@ export class UserSettingComponent { ...@@ -233,14 +260,34 @@ export class UserSettingComponent {
}); });
} else if (this.action === 'edit') { } else if (this.action === 'edit') {
this.userService.update(this.selectModel).subscribe({ const observables: Observable<any>[] = [];
next: result => {
swal("Update Success!!", "บันทึกข้อมูลสมาชิก", "success"); observables.push(this.userService.update(this.selectModel));
if (this.showPasswordFields && this.newPassword && this.newPassword === this.confirmNewPassword) {
const authData = new AuthModel();
authData.username = this.selectModel.email;
authData.password = this.newPassword;
authData.profile = new ProfileModel({
candidateId: this.selectModel.candidateId,
profileId: this.selectModel.profileId
});
observables.push(this.userService.editPassword(authData));
} else if (this.showPasswordFields && (this.newPassword || this.confirmNewPassword)) {
swal("Invalid Password", "Please ensure passwords match.", "warning");
return;
}
forkJoin(observables).subscribe({
next: results => {
swal("Update Success!!", "บันทึกข้อมูลเรียบร้อยแล้ว", "success");
this.ngOnInit(); this.ngOnInit();
this.childModal?.nativeElement.click(); this.childModal?.nativeElement.click();
}, },
error: err => { error: err => {
swal("Error", "ไม่สามารถอัปเดตได้", "error"); console.error('Update failed:', err);
swal("Error", "เกิดข้อผิดพลาดในการอัปเดตข้อมูล", "error");
} }
}); });
} }
...@@ -252,9 +299,47 @@ export class UserSettingComponent { ...@@ -252,9 +299,47 @@ export class UserSettingComponent {
updatePagedItems() { updatePagedItems() {
const startIndex = this.pageIndex * 10; this.totalCount = this.filterList.length;
const endIndex = startIndex + 10; this.totalPages = Math.max(1, Math.ceil(this.totalCount / this.pageSize));
this.filterList = this.itemsList.slice(startIndex, endIndex);
const startIndex = this.pageIndex * this.pageSize;
const endIndex = startIndex + this.pageSize;
this.pagedList = this.filterList.slice(startIndex, endIndex);
}
onSearchChange() {
this.pageIndex = 0;
this.updatePagedItems();
}
get pages(): number[] {
return Array.from({ length: this.totalPages }, (_, i) => i);
}
get showingEnd(): number {
return Math.min((this.pageIndex + 1) * this.pageSize, this.totalCount);
}
goPrev() {
if (this.pageIndex > 0) {
this.pageIndex--;
this.updatePagedItems();
}
}
goNext() {
if (this.pageIndex < this.totalPages - 1) {
this.pageIndex++;
this.updatePagedItems();
}
}
goTo(p: number) {
if (p >= 0 && p < this.totalPages && p !== this.pageIndex) {
this.pageIndex = p;
this.updatePagedItems();
}
} }
toggleAll(event: any) { toggleAll(event: any) {
......
...@@ -109,6 +109,9 @@ export class UserService { ...@@ -109,6 +109,9 @@ export class UserService {
registerCompany(body: AuthModel) { registerCompany(body: AuthModel) {
return this.http.post<ResponseModel>(this.apiAuthComUrl + "/register", body); return this.http.post<ResponseModel>(this.apiAuthComUrl + "/register", body);
} }
editPassword(body: AuthModel) {
return this.http.post<ResponseModel>("/users", body)
}
editPasswordCompany(body: AuthModel) { editPasswordCompany(body: AuthModel) {
return this.http.post<ResponseModel>("/users/company", body) return this.http.post<ResponseModel>("/users/company", body)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment