Skip to content
This repository has been archived by the owner on Feb 4, 2025. It is now read-only.

Commit

Permalink
Merge pull request #278 from CampusDual/BFS42024-330v2
Browse files Browse the repository at this point in the history
BFS42024-330v2
  • Loading branch information
JoseMRLC authored Jan 21, 2025
2 parents 1acfc08 + 45b7fac commit 716426e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ throw new Error('Method not implemented.');
protected service: OntimizeService;
avatar: any

defaultImage: string = 'assets/images/no-image.png'; // Ruta de la imagen por defecto


constructor(protected injector: Injector,private router: Router, private mainService: MainService,protected dialogService: DialogService,private oUserInfoService: OUserInfoService) {
this.service= this.injector.get(OntimizeService);
this.configureService();
Expand Down Expand Up @@ -74,55 +77,61 @@ throw new Error('Method not implemented.');
}
}
onImageChange(event: any) {
// Si no hay evento o el archivo no está definido, simplemente retorna
if (!event || !this.UsrPhoto.currentFileName) {
this.oUserInfoService.setUserInfo({
...this.oUserInfoService.getUserInfo(),
avatar:this.defaultImage
})
return;
}

if (this.isUpdatingImage) {
return;
}

const base64String = event;
const currentFileName = this.UsrPhoto.currentFileName || '';

const validExtensions = ['jpg', 'jpeg', 'png', 'gif'];
const fileExtension = currentFileName.split('.').pop()?.toLowerCase();

// Validar si el nombre del archivo o la extensión son inválidos

if (!fileExtension || !validExtensions.includes(fileExtension)) {
this.showAlert(); // Muestra la alerta de error
this.isUpdatingImage = true;
this.UsrPhoto.setValue(''); // Limpia el valor del archivo
this.isUpdatingImage = false;
//this.showAlert();
//this.UsrPhoto.setValue('');
return;
}

if (base64String) {
const img = new Image();
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
img.src = `data:image/jpg;base64, ${base64String}`;

img.onload = () => {
if (ctx) {
const newWidth = 200;
const newHeight = 200;

canvas.width = newWidth;
canvas.height = newHeight;

ctx.drawImage(img, 0, 0, newWidth, newHeight);
const modifiedImageBase64 = canvas.toDataURL('image/jpg');

this.isUpdatingImage = true;
this.UsrPhoto.setValue(modifiedImageBase64); // Actualiza la imagen redimensionada
this.isUpdatingImage = false;


// Actualiza el avatar en el servicio OUserInfoService
this.oUserInfoService.setUserInfo({
...this.oUserInfoService.getUserInfo(),
avatar: `data:image/png;base64,${base64String}`
});

ctx.clearRect(0, 0, canvas.width, canvas.height); // Limpia el canvas
}
};

img.onerror = () => {
console.error('Error al cargar la imagen.');
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import { MainService } from 'src/app/shared/services/main.service';
export class PersonalTutorInfoComponent {
@ViewChild("userId") inputTutorId: OTextInputComponent;
@ViewChild("form") form: OFormComponent;
@ViewChild("UsrPhoto") UsrPhoto: OImageComponent;
@ViewChild("tutorsPhoto") tutorsPhoto: OImageComponent;
isUpdatingImage: boolean = false;
isUpdateOtherFile: boolean = false;
mainInfo: any = {};
protected service: OntimizeService;

defaultImage: string = 'assets/images/no-image.png'; // Ruta de la imagen por defecto


constructor(protected injector: Injector, private mainService: MainService,private router: Router,protected dialogService: DialogService,private oUserInfoService:OUserInfoService) {
this.service= this.injector.get(OntimizeService);
this.configureService();
Expand Down Expand Up @@ -65,27 +68,31 @@ export class PersonalTutorInfoComponent {
}
}
onImageChange(event: any) {
// Si no hay evento o el archivo no está definido, simplemente retorna
if (!event || !this.UsrPhoto.currentFileName) {
if (!event || !this.tutorsPhoto.currentFileName) {
this.oUserInfoService.setUserInfo({
...this.oUserInfoService.getUserInfo(),
avatar:this.defaultImage
})
return;
}

if(!this.tutorsPhoto.currentFileName){

}

if (this.isUpdatingImage) {
return;
}

const base64String = event;
const currentFileName = this.UsrPhoto.currentFileName || '';
const currentFileName = this.tutorsPhoto.currentFileName || '';

const validExtensions = ['jpg', 'jpeg', 'png', 'gif'];
const fileExtension = currentFileName.split('.').pop()?.toLowerCase();

// Validar si el nombre del archivo o la extensión son inválidos
if (!fileExtension || !validExtensions.includes(fileExtension)) {
this.showAlert(); // Muestra la alerta de error
this.isUpdatingImage = true;
this.UsrPhoto.setValue(''); // Limpia el valor del archivo
this.isUpdatingImage = false;
//this.tutorsPhoto.setValue(''); // Limpia el valor del archivo
return;
}

Expand All @@ -106,27 +113,27 @@ export class PersonalTutorInfoComponent {
ctx.drawImage(img, 0, 0, newWidth, newHeight);
const modifiedImageBase64 = canvas.toDataURL('image/jpg');

this.isUpdatingImage = true;
this.UsrPhoto.setValue(modifiedImageBase64); // Actualiza la imagen redimensionada
this.isUpdatingImage = false;

ctx.clearRect(0, 0, canvas.width, canvas.height); // Limpia el canvas
}
};
this.oUserInfoService.setUserInfo({
...this.oUserInfoService.getUserInfo(),
avatar:modifiedImageBase64
})
ctx.clearRect(0, 0, canvas.width, canvas.height);
}}


img.onerror = () => {
console.error('Error al cargar la imagen.');
};
}

}}


}



showAlert() {
if (this.dialogService) {
this.dialogService.error('Error de tipo de archivo', 'Por favor, sube una imagen con extensión .jpg, .jpeg .png o .gif');
}
}

}

0 comments on commit 716426e

Please sign in to comment.