integrasi user profil

This commit is contained in:
Fuzi_fauzia 2024-07-04 14:23:44 +07:00
parent 5913980330
commit ae74cf2332
3 changed files with 95 additions and 18 deletions

View File

@ -19,4 +19,25 @@ export class LoginService {
}); });
return this.http.put<any>(url, data, { headers }); return this.http.put<any>(url, data, { headers });
} }
getDataProfil(id): Observable<any> {
const endpoint = `/users`;
const url = `${BASE_URL}${endpoint}/byUserid/${id}`;
const headers = new HttpHeaders({
"Content-Type": "application/json",
"x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT",
});
return this.http.get<any>(url, { headers });
}
updateProfile(data: any, userId: string): Observable<any> {
const endpoint = `/users`;
const url = `${BASE_URL}${endpoint}/update/${userId}`;
const headers = new HttpHeaders({
"x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT",
});
return this.http.post(`${url}`, data, { headers });
}
} }

View File

@ -97,7 +97,7 @@
<div class="image-container text-center" style="position: relative"> <div class="image-container text-center" style="position: relative">
<button <button
*ngIf="url" *ngIf="url"
(click)="delete()" (click)="deleteImage()"
class="btn btn-danger" class="btn btn-danger"
style=" style="
position: absolute; position: absolute;
@ -125,7 +125,7 @@
<input <input
id="fileInput" id="fileInput"
type="file" type="file"
formControlName="profilePicture" formControlName="image"
class="select-profile-picture" class="select-profile-picture"
(change)="onSelectFile($event)" (change)="onSelectFile($event)"
accept="image/*" accept="image/*"
@ -159,7 +159,7 @@
border-radius: 10px; border-radius: 10px;
width: 145px; width: 145px;
" "
(click)="saveEdit()" [disabled]="disableButton"
> >
Save Save
</button> </button>

View File

@ -1,6 +1,7 @@
import { Component, ViewChild } from "@angular/core"; import { Component, ViewChild } from "@angular/core";
import { FormBuilder, FormGroup, NgForm, Validators } from "@angular/forms"; import { FormBuilder, FormGroup, NgForm, Validators } from "@angular/forms";
import { ActivatedRoute, Router } from "@angular/router"; import { LoginService } from "../../service/login.service";
import { ToastrService } from "ngx-toastr";
@Component({ @Component({
selector: "app-profil-information", selector: "app-profil-information",
templateUrl: "./profil-information.component.html", templateUrl: "./profil-information.component.html",
@ -12,34 +13,55 @@ export class ProfilInformationComponent {
profilInfo: FormGroup; profilInfo: FormGroup;
activeTab: string = "profile-info2"; activeTab: string = "profile-info2";
submitted = false; submitted = false;
storedData: any;
disableButton: boolean = false
url: any = ''; url: any = '';
fileSelected: any = null;
constructor( constructor(
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
private router: Router, private authService: LoginService,
private route: ActivatedRoute private toastr: ToastrService,
) {} ) {}
ngOnInit(): void { ngOnInit(): void {
this.storedData = JSON.parse(localStorage.getItem("account_info"));
this.profilInfo = this.formBuilder.group({ this.profilInfo = this.formBuilder.group({
firstName: ["", Validators.required], firstName: ["", Validators.required],
lastName: ["", Validators.required], lastName: ["", Validators.required],
email: ["", Validators.required], email: ["", Validators.required],
phone: ["", Validators.required], phone: ["", Validators.required],
profilePicture: [null] image: [null]
}); });
this.dataProfil(this.storedData.sub)
}
dataProfil(userId){
this.authService.getDataProfil(userId).subscribe(data => {
this.profilInfo.patchValue({
firstName: data.data.firstname,
lastName: data.data.lastname,
email: data.data.email,
phone: data.data.phone,
image: [null]
});
this.url = data.data.image_path
});
} }
onSelectFile(event) { onSelectFile(event) {
if (event.target.files && event.target.files[0]) { if (event.target.files && event.target.files[0]) {
const file = event.target.files[0]; const file = event.target.files[0];
this.fileSelected = file;
// Check if selected file is an image
if (!file.type.startsWith('image')) { if (!file.type.startsWith('image')) {
alert('Please select an image file.'); this.toastr.error("Warning", "Please select an image file.", {
timeOut: 5000,
closeButton: true,
});
this.profilInfo.patchValue({ this.profilInfo.patchValue({
profilePicture: null image: null
}); });
return; return;
} }
@ -53,12 +75,16 @@ export class ProfilInformationComponent {
} }
} }
public delete() { public deleteImage() {
this.url = null; this.url = null;
this.profilInfo.patchValue({ profilePicture: null }); if (this.url === null) {
this.disableButton = true
} else{
this.disableButton = false
}
this.profilInfo.patchValue({ image: null });
} }
selectTab(tabName: string) { selectTab(tabName: string) {
this.activeTab = tabName; this.activeTab = tabName;
} }
@ -69,11 +95,41 @@ export class ProfilInformationComponent {
onProjectInfoSubmit() { onProjectInfoSubmit() {
this.submitted = true; this.submitted = true;
if (this.profilInfo.invalid) {
return;
}
const formData = new FormData();
formData.append("firstname", this.profilInfo.get("firstName").value);
formData.append("lastname", this.profilInfo.get("lastName").value);
formData.append("email", this.profilInfo.get("email").value);
formData.append("phone", this.profilInfo.get("phone").value);
formData.append("statusId", "2");
formData.append("buildingId", "4");
formData.append("image", this.fileSelected);
this.authService.updateProfile(formData, this.storedData.sub).subscribe(
(response) => {
console.log("Profile updated successfully:", response);
this.toastr.success("success", "Profile updated successfully.", {
timeOut: 5000,
closeButton: true,
});
// Handle success response
},
(error) => {
console.error("Profile update failed:", error);
this.toastr.error("Error", "Profile update failed.", {
timeOut: 5000,
closeButton: true,
});
// Handle error response
}
);
} }
cancel() {} cancel() {
this.dataProfil(this.storedData.sub);
saveEdit() { this.disableButton = false
console.log("save");
} }
} }