mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
304 lines
8.7 KiB
TypeScript
304 lines
8.7 KiB
TypeScript
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
|
|
import { ModalController } from '@ionic/angular';
|
|
import { Publication } from 'src/app/models/publication';
|
|
import { PublicationFolder } from 'src/app/models/publicationfolder';
|
|
import { PublicationsService } from 'src/app/services/publications.service';
|
|
|
|
import { NewPublicationPage } from 'src/app/pages/publications/new-publication/new-publication.page';
|
|
import { PublicationPipe } from 'src/app/pipes/publication.pipe';
|
|
import { ThemeService } from 'src/app/services/theme.service'
|
|
import { ToastService } from 'src/app/services/toast.service';
|
|
import { EditActionPage } from 'src/app/pages/publications/edit-action/edit-action.page';
|
|
import { Storage } from '@ionic/storage';
|
|
import { PermissionService } from 'src/app/services/permission.service';
|
|
import { HttpErrorHandle } from 'src/app/services/http-error-handle.service';
|
|
import { PublicationModel } from 'src/app/models/beast-orm';
|
|
|
|
@Component({
|
|
selector: 'app-view-publications',
|
|
templateUrl: './view-publications.page.html',
|
|
styleUrls: ['./view-publications.page.scss'],
|
|
})
|
|
export class ViewPublicationsPage implements OnInit {
|
|
showLoader: boolean;
|
|
loading: any;
|
|
|
|
publicationList: {[key: string]: Publication[] } = {};
|
|
publicationItem: {[key: string]: PublicationFolder } = {};
|
|
getpublication = [];
|
|
error: any;
|
|
|
|
@Input() folderId: any;
|
|
@Output() addNewPublication = new EventEmitter<any>();
|
|
@Output() editPublication = new EventEmitter<any>();
|
|
@Output() openPublicationDetails= new EventEmitter<any>();
|
|
@Output() goBackToViewPublications = new EventEmitter();
|
|
@Output() closeDesktopComponent = new EventEmitter<any>();
|
|
@Output() goBacktoPublicationDetails = new EventEmitter<any>();
|
|
@Output() getActions= new EventEmitter<any>();
|
|
|
|
publicationPipe = new PublicationPipe()
|
|
|
|
constructor(
|
|
private modalController: ModalController,
|
|
private publications: PublicationsService,
|
|
public ThemeService: ThemeService,
|
|
private toastService: ToastService,
|
|
private storage: Storage,
|
|
public p:PermissionService,
|
|
private httpErrorHandle: HttpErrorHandle
|
|
) {
|
|
this.createPublicationList()
|
|
}
|
|
|
|
ngOnInit() {
|
|
if(typeof(this.folderId) == 'object') {
|
|
this.folderId = this.folderId['ProcessId']
|
|
}
|
|
|
|
this.createPublicationList()
|
|
|
|
window['app-view-publications-page-doRefresh'] = this.doRefresh
|
|
|
|
this.getFromDB();
|
|
}
|
|
|
|
ngOnChanges(changes: any): void {
|
|
|
|
if(typeof(this.folderId) == 'object') {
|
|
this.folderId = this.folderId['ProcessId']
|
|
}
|
|
|
|
if(!this.publicationList[this.folderId]) {
|
|
this.publicationItem[this.folderId] = new PublicationFolder();
|
|
}
|
|
|
|
this.createPublicationList()
|
|
|
|
this.getFromDB();
|
|
|
|
this.getPublicationDetail();
|
|
this.getPublicationsIds();
|
|
|
|
|
|
}
|
|
|
|
|
|
createPublicationList(folderId = this.folderId) {
|
|
if(!this.publicationList[this.folderId]) {
|
|
this.publicationList[this.folderId] = []
|
|
}
|
|
if(!this.publicationItem[this.folderId]) {
|
|
this.publicationItem[this.folderId] = new PublicationFolder();
|
|
}
|
|
|
|
}
|
|
|
|
doRefresh =(event) => {
|
|
|
|
this.getPublicationDetail();
|
|
this.getPublicationsIds();
|
|
}
|
|
|
|
close() {
|
|
this.closeDesktopComponent.emit();
|
|
}
|
|
|
|
getPublicationDetail() {
|
|
this.showLoader = true;
|
|
const folderId = this.folderId
|
|
this.publications.GetPresidentialAction(folderId).subscribe(res=>{
|
|
this.showLoader = false;
|
|
this.publicationItem[folderId] = res
|
|
this.storage.set(folderId+"name", res)
|
|
}, (error) => {
|
|
this.showLoader = false;
|
|
// this.httpErroHandle.httpStatusHandle(error)
|
|
});
|
|
}
|
|
|
|
async getPublicationsIds() {
|
|
|
|
this.showLoader = true;
|
|
const folderId = this.folderId
|
|
|
|
try {
|
|
const publicationIds = await this.publications.GetPublicationsList(folderId).toPromise();
|
|
|
|
this.createPublicationList(folderId)
|
|
let loadLater = []
|
|
for (let publicationId of publicationIds) {
|
|
|
|
if(!this.publicationIsPresent(publicationId, folderId)) {
|
|
await this.loadPublication(publicationId, folderId)
|
|
|
|
} else {
|
|
loadLater.push(publicationId)
|
|
}
|
|
}
|
|
|
|
for( let publicationId of loadLater) {
|
|
await this.loadPublication(publicationId, folderId)
|
|
}
|
|
|
|
this.showLoader = false;
|
|
|
|
this.storage.set(folderId, this.publicationList[folderId]);
|
|
this.getpublication = this.publicationList[folderId];
|
|
} catch(error) {
|
|
this.showLoader = false;
|
|
}
|
|
|
|
}
|
|
|
|
publicationIsPresent(publicationId, folderId) {
|
|
return this.publicationList[folderId].find( e => e.DocumentId == publicationId )
|
|
}
|
|
publicationFind(publicationId, folderId) {
|
|
return this.publicationList[folderId].find( e => e.DocumentId == publicationId )
|
|
}
|
|
publicationFindIndex(publicationId, folderId) {
|
|
return this.publicationList[folderId].findIndex( e => e.DocumentId == publicationId )
|
|
}
|
|
|
|
async loadPublication(publicationId, folderId) {
|
|
let Publication = await this.publications.GetPublicationById(publicationId).toPromise();
|
|
let publicationDetails: Publication = this.publicationPipe.itemList(Publication)
|
|
|
|
const findIndex = this.publicationFindIndex(publicationId, folderId)
|
|
const found = this.publicationIsPresent(publicationId, folderId)
|
|
if(!found) {
|
|
this.publicationList[folderId].push(publicationDetails)
|
|
} else {
|
|
this.publicationList[folderId][findIndex] = publicationDetails
|
|
}
|
|
|
|
}
|
|
|
|
getFromDB() {
|
|
const folderId = this.folderId
|
|
this.storage.get(folderId).then((viewPublications) => {
|
|
this.publicationList[folderId] = viewPublications
|
|
})
|
|
this.storage.get(folderId+"name").then((viewPublications) => {
|
|
this.publicationItem[folderId] = viewPublications
|
|
})
|
|
}
|
|
|
|
// getPublications() {
|
|
// this.showLoader = true;
|
|
// const folderId = this.folderId
|
|
// this.publicationList = new Array();
|
|
// this.publications.GetPublications(folderId).subscribe(async res=> {
|
|
|
|
// res.forEach(element => {
|
|
// let item: Publication = this.publicationPipe.itemList(element)
|
|
// this.publicationList.push(item);
|
|
// });
|
|
|
|
// this.showLoader = false;
|
|
// await this.storage.remove(folderId);
|
|
// await this.storage.set(folderId, this.publicationList);
|
|
// //this.getFromDB();
|
|
|
|
// },
|
|
// (error)=>{
|
|
// if(error.status == '404') {
|
|
// this.error = 'Sem publicações disponíveis!';
|
|
// this.publicationList= [];
|
|
// }
|
|
|
|
// this.showLoader = false;
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
async AddPublication(publicationType:any, folderId:any) {
|
|
|
|
if( window.innerWidth < 701) {
|
|
const modal = await this.modalController.create({
|
|
component: NewPublicationPage,
|
|
componentProps:{
|
|
publicationType: publicationType,
|
|
folderId: folderId,
|
|
},
|
|
cssClass: 'new-publication modal modal-desktop',
|
|
backdropDismiss: false
|
|
});
|
|
await modal.present();
|
|
modal.onDidDismiss().then(()=>{
|
|
this.doRefresh(event);
|
|
});
|
|
} else {
|
|
this.addNewPublication.emit({
|
|
publicationType: publicationType,
|
|
folderId: folderId
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
async openEditPublication(folderId?:any){
|
|
|
|
if( window.innerWidth < 701) {
|
|
const modal = await this.modalController.create({
|
|
component: EditActionPage,
|
|
componentProps: {
|
|
folderId: folderId,
|
|
},
|
|
cssClass: 'new-action modal modal-desktop',
|
|
backdropDismiss: true
|
|
});
|
|
await modal.present();
|
|
modal.onDidDismiss().then(() => {
|
|
// Do nothing
|
|
});
|
|
}
|
|
else {
|
|
this.editPublication.emit(folderId);
|
|
}
|
|
}
|
|
|
|
async deletePublication(folderId?:any) {
|
|
const loader = this.toastService.loading();
|
|
try {
|
|
await this.publications.DeletePresidentialAction(folderId).toPromise();
|
|
this.httpErrorHandle.httpsSucessMessagge('Eliminar Acção')
|
|
} catch (error) {
|
|
this.httpErrorHandle.httpStatusHandle(error)
|
|
}
|
|
finally {
|
|
loader.remove()
|
|
}
|
|
this.close();
|
|
this.getActions.emit();
|
|
}
|
|
|
|
async viewPublicationDetail(publicationId:string) {
|
|
|
|
|
|
// if( window.innerWidth <= 1024) {
|
|
// const modal = await this.modalController.create({
|
|
// component: PublicationDetailPage,
|
|
// componentProps:{
|
|
// publicationId: publicationId,
|
|
// },
|
|
// cssClass: 'publication-detail modal modal-desktop',
|
|
// //backdropDismiss: false
|
|
// });
|
|
// await modal.present();
|
|
// modal.onDidDismiss().then(()=>{
|
|
// this.doRefresh(event);
|
|
// });
|
|
// } else {
|
|
// // open publication details
|
|
// this.openPublicationDetails.emit(publicationId);
|
|
// }
|
|
|
|
this.openPublicationDetails.emit(publicationId);
|
|
|
|
}
|
|
|
|
}
|