Skip to content

Commit

Permalink
feat(rup): cambia a drive los archivos adjuntos
Browse files Browse the repository at this point in the history
  • Loading branch information
liquid36 committed Jan 7, 2021
1 parent 547e2c9 commit 5a059a2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 83 deletions.
2 changes: 2 additions & 0 deletions src/app/modules/rup/components/core/rup.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Engine } from 'json-rules-engine';
import { calcularEdad } from '@andes/shared';
import { Subject } from 'rxjs';
import { filter, takeUntil } from 'rxjs/operators';
import { DriveService } from 'src/app/services/drive.service';

@Component({
selector: 'rup',
Expand Down Expand Up @@ -144,6 +145,7 @@ export class RUPComponent implements OnInit, AfterViewInit, OnDestroy {
public conceptosTurneablesService: ConceptosTurneablesService,
public plantillasService: PlantillasService,
public vacunasService: VacunasService,
public driveService: DriveService,
@Optional() public ejecucionService: RupEjecucionService
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ export class AdjuntarDocumentoComponent extends RUPComponent implements OnInit {
@Input() permiteCarga: boolean;
@Input() parametroRegistro;

@ViewChildren('upload') childsComponents: QueryList<any>;
imagenes = IMAGENES_EXT;
extensions = FILE_EXT;
extensions = [...FILE_EXT, ...IMAGENES_EXT];

adjunto: any;
loading = false;
Expand All @@ -27,21 +26,16 @@ export class AdjuntarDocumentoComponent extends RUPComponent implements OnInit {
uploadValid = true;

// fotos: { file?: any, ext: string, id?: any, descripcion?: ISnomedConcept, fecha?: Date }[] = [];
lightbox = false;
indice;
fileToken: String = null;

public descendientesInformeClinico: ISnomedConcept[] = [];
public hoy = moment(new Date()).endOf('day').toDate();
public hoy = moment().endOf('day').toDate();

ngOnInit() {

if (isUndefined(this.permiteCarga)) {
this.permiteCarga = true;
}

this.extensions = this.extensions.concat(this.imagenes);

if (!isUndefined(this.parametroRegistro)) {
this.registro = this.parametroRegistro;
}
Expand All @@ -51,7 +45,8 @@ export class AdjuntarDocumentoComponent extends RUPComponent implements OnInit {
if (!this.registro.valor.documentos) {
this.registro.valor.documentos = [];
}
this.adjuntosService.generateToken().subscribe((data: any) => {

this.adjuntosService.token$.subscribe((data: any) => {
this.fileToken = data.token;
});

Expand All @@ -72,83 +67,18 @@ export class AdjuntarDocumentoComponent extends RUPComponent implements OnInit {
return this.registro.valor.documentos.length === 0;
}

changeListener($event): void {
this.readThis($event.target);
}


readThis(inputValue: any): void {
let ext = this.fileExtension(inputValue.value);
this.errorExt = false;
if (!this.extensions.find((item) => item === ext.toLowerCase())) {
(this.childsComponents.first as any).nativeElement.value = '';
this.errorExt = true;
return;
}
let file: File = inputValue.files[0];
let myReader: FileReader = new FileReader();

myReader.onloadend = (e) => {
(this.childsComponents.first as any).nativeElement.value = '';
let metadata = {
prestacion: this.prestacion.id,
registro: this.registro.id
};
this.adjuntosService.upload(myReader.result, metadata).subscribe((data) => {
this.registro.valor.documentos.push({
ext,
id: data._id,
});
});


};
myReader.readAsDataURL(file);
this.uploadValid = true;
}


fileExtension(file) {
if (file.lastIndexOf('.') >= 0) {
return file.slice((file.lastIndexOf('.') + 1));
} else {
return '';
}
}

esImagen(extension) {
return this.imagenes.find(x => x === extension.toLowerCase());
}

imageRemoved($event) {
let index = this.registro.valor.documentos.indexOf($event);
this.registro.valor.documentos.splice(index, 1);
}

activaLightbox(index) {
if (this.registro.valor.documentos[index].ext !== 'pdf') {
this.lightbox = true;
this.indice = index;
}
}

imagenPrevia(i) {
let imagenPrevia = i - 1;
if (imagenPrevia >= 0) {
this.indice = imagenPrevia;
}
}

imagenSiguiente(i) {
let imagenSiguiente = i + 1;
if (imagenSiguiente <= this.registro.valor.documentos.length - 1) {
this.indice = imagenSiguiente;
}
imageRemoved(doc) {
this.driveService.deleteFile(doc.id).subscribe(() => {
const index = this.registro.valor.documentos.indexOf(doc);
this.registro.valor.documentos.splice(index, 1);
});
}

createUrl(doc) {
/** Hack momentaneo */
// let jwt = window.sessionStorage.getItem('jwt');
if (doc.id) {
let apiUri = environment.API;
return apiUri + '/modules/rup/store/' + doc.id + '?token=' + this.fileToken;
Expand Down Expand Up @@ -210,5 +140,15 @@ export class AdjuntarDocumentoComponent extends RUPComponent implements OnInit {
return [];
}
}

onUpload($event) {
const { status, body } = $event;
if (status === 200) {
this.registro.valor.documentos.push({
ext: body.ext,
id: body.id
});
}
}
}
// ElementosRUPRegister.register('AdjuntarDocumentoComponent', AdjuntarDocumentoComponent);
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
<div class="container-fluid col-12">
<div class="row mb-3" *ngIf="permiteCarga">
<div class="col-12">
<input type="file" (change)="changeListener($event)" style="display:none;" #upload>
<strong *ngIf="!soloValores">Cargar documentos</strong>
<span *ngIf="!waiting && !soloValores">
<plex-button type="primary" class="btn-sm" label="Desde mi PC" (click)="upload.click()">
</plex-button>
<upload-file label="Desde mi PC" (onUpload)="onUpload($event)" modulo="rup"
[extensiones]="extensions"></upload-file>
<plex-button *ngIf="!waiting" class="btn-sm" type="primary" label="Desde mi App móvil"
(click)="fromMobile()"></plex-button>
<span *ngIf="errorExt">
Expand Down
2 changes: 1 addition & 1 deletion src/app/shared/components/upload-file.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface ICompleted {
@Component({
selector: 'upload-file',
template: `
<input style="visibility: hidden;" type="file" (change)="onChange($event)" #upload>
<input style="visibility: hidden; position: absolute;" type="file" (change)="onChange($event)" #upload>
<plex-button type="info" [label]="btnLabel" (click)="upload.click()" [disabled]="disabled"></plex-button>
`,
})
Expand Down

0 comments on commit 5a059a2

Please sign in to comment.