Skip to content

Commit

Permalink
more changes for multiple evidences
Browse files Browse the repository at this point in the history
  • Loading branch information
tsubik committed Jan 9, 2024
1 parent 2db6232 commit 33e901c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/app/models/observation_document.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Observation } from 'app/models/observation.model';
import { ObservationReport } from 'app/models/observation_report';
import { JsonApiModel, JsonApiModelConfig, Attribute, BelongsTo } from 'angular2-jsonapi';
import { JsonApiModel, JsonApiModelConfig, Attribute, BelongsTo, HasMany } from 'angular2-jsonapi';

@JsonApiModelConfig({
type: 'observation-documents'
Expand All @@ -11,6 +11,6 @@ export class ObservationDocument extends JsonApiModel {
@Attribute() 'document-type': string;
@Attribute() attachment: string|{ url: string };

@BelongsTo() observation: Observation;
@HasMany() observations: Observation[];
@BelongsTo() 'observation-report'?: ObservationReport;
}
11 changes: 8 additions & 3 deletions src/app/pages/observations/observation-detail.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ <h2 *ngIf="!isDisabled() && observation && !isCopied">{{'Edit observation' | tra
<p class="note">{{'observation.evidenceType.note' | translate}}</p>
</div>

<ng-template [ngIf]="isEvidenceOnReport">
<ng-template [ngIf]="evidenceType === 'Evidence presented in the report'">
<div class="form-group -required">
<label for="evidence_details">{{'observation.evidenceDetails.title' | translate}}</label>
<textarea id="evidence_details" name="evidence_details" [(ngModel)]="evidenceOnReport" #evidence_details="ngModel" required [disabled]="isDisabled()"></textarea>
Expand Down Expand Up @@ -128,7 +128,10 @@ <h3>{{'List of evidence' | translate}}</h3>
<ul class="documents-list" *ngIf="documents.length">
<li *ngFor="let document of documents">
<div class="content">
<div class="title">{{document.name}}</div>
<div class="title" *ngIf="!document.id">{{document.name}}</div>
<div class="title" *ngIf="document.id">
<a href="{{document.attachment.url}}" target="_blank" download>{{document.name}}</a>
</div>
<div class="status">{{document['document-type']}} | {{ (document.id ? 'Already uploaded' : 'To upload') | translate}}</div>
</div>
<div class="actions">
Expand All @@ -146,7 +149,9 @@ <h3>{{'List of evidence' | translate}}</h3>
<ul class="documents-list" *ngIf="reportDocuments.length">
<li *ngFor="let document of reportDocuments">
<div class="content">
<div class="title">{{document.name}}</div>
<div class="title">
<a href="{{document.attachment.url}}" target="_blank" download>{{document.name}}</a>
</div>
<div class="status">{{document['document-type']}}</div>
</div>
<div class="actions">
Expand Down
5 changes: 5 additions & 0 deletions src/app/pages/observations/observation-detail.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
max-height: 400px;
overflow: auto;

a {
color: $color-primary;
text-decoration: underline;
}

> li {
display: flex;
justify-content: space-between;
Expand Down
49 changes: 19 additions & 30 deletions src/app/pages/observations/observation-detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,10 +664,6 @@ export class ObservationDetailComponent implements OnDestroy {
);
}

get isEvidenceOnReport() {
return this._evidenceType === 'Evidence presented in the report';
}

get mapLayers(): any[] {
const layers = [];

Expand Down Expand Up @@ -697,20 +693,12 @@ export class ObservationDetailComponent implements OnDestroy {
set reportChoice(reportChoice) {
if (reportChoice && reportChoice.id) {
this.observationDocumentsService.getAll({ filter: { 'observation-report-id': reportChoice.id } }).then((documents) => {
const notLinkedWithObservation = documents.filter((d) => !(this.observation['observation-documents'] || []).find((od) => od.id === d.id));
this.reportDocuments = orderBy(
uniqBy(documents, 'id'),
uniqBy(notLinkedWithObservation, 'id'),
[(d) => d.name.toLowerCase()]
);
});

// this.observationReportsService.getById(reportChoice.id, { include: 'observations,observations.observation-documents' }).then((report) => {
// if (report.observations && report.observations.length > 0) {
// this.reportDocuments = orderBy(
// uniqBy(flatten(report.observations.map(o => o['observation-documents'])), 'id'),
// [(d) => d.name.toLowerCase()]
// );
// }
// });
}

if (this.observation) {
Expand Down Expand Up @@ -852,7 +840,7 @@ export class ObservationDetailComponent implements OnDestroy {

this.observationsService.getById(this.existingObservation, {
// tslint:disable-next-line:max-line-length
include: 'country,operator,subcategory,severity,observers,governments,modified-user,fmu,observation-report,law,user,relevant-operators'
include: 'country,operator,subcategory,severity,observers,governments,modified-user,fmu,observation-report,observation-documents,law,user,relevant-operators'
}).then((observation) => {
this.observation = observation;
if (this.route.snapshot.params.copiedId) {
Expand All @@ -877,6 +865,8 @@ export class ObservationDetailComponent implements OnDestroy {
this.latitude = this.observation.lat;
this.longitude = this.observation.lng;
this.operatorChoice = this.observation.operator;
this.reportChoice = this.observation['observation-report'];
this.documents = this.observation['observation-documents'];
})
.catch(() => {
// The only reason the request should fail is that the user
Expand All @@ -887,13 +877,13 @@ export class ObservationDetailComponent implements OnDestroy {
.then(() => this.loading = false);

// We load the list of documents only if we edit an observation
if (this.route.snapshot.params.id) {
this.observationDocumentsService.getAll({
sort: 'name',
filter: { observation_id: this.route.snapshot.params.id }
}).then(documents => this.documents = documents)
.catch(err => console.error(err)); // TODO: visual feedback
}
// if (this.route.snapshot.params.id) {
// this.observationDocumentsService.getAll({
// sort: 'name',
// filter: { observation_id: this.route.snapshot.params.id }
// }).then(documents => this.documents = documents)
// .catch(err => console.error(err)); // TODO: visual feedback
// }
} else {
if (this.route.snapshot.params.useDraft) {
this.draft = this.observationsService.getDraftObservation();
Expand All @@ -907,7 +897,7 @@ export class ObservationDetailComponent implements OnDestroy {
this.evidenceOnReport = this.draft.evidenceOnReport;
this.documents = this.draft.documents.map(document => this.datastoreService.createRecord(ObservationDocument, {
name: document.name,
type: this.draft.evidenceType,
'document-type': this.draft.evidenceType,
attachment: document.attachement
}));
// If we were going to add an evidence
Expand Down Expand Up @@ -992,11 +982,6 @@ export class ObservationDetailComponent implements OnDestroy {
}
}

// public onChangeDocumentType(type: string): void {
// console.log('onChangeEvidenceCategory', previousType, type, typeElement);
// this.evidenceCategory = type;
// }

private saveAsDraftObservation(): void {
const draftModel: DraftObservation = {
observationType: this.type,
Expand Down Expand Up @@ -1366,6 +1351,7 @@ export class ObservationDetailComponent implements OnDestroy {
if (document.id) {
this.reportDocuments.push(document);
this.reportDocuments = orderBy(this.reportDocuments, [(d) => d.name.toLowerCase()]);

// else {
// // If the document is an existing one, we add it
// // to the list of documents to delete
Expand Down Expand Up @@ -1619,7 +1605,7 @@ export class ObservationDetailComponent implements OnDestroy {

// We create an array of the documents to upload
const uploadPromises = !this.isEvidenceTypeOnReport(this.evidenceType) ? this.documentsToUpload.map((d) => {
d.observation = observation; // We link the document to the observation
// d.observation = observation; // We link the document to the observation TODO:
d['observation-report'] = observation['observation-report']; // We link the document to the report
return d.save().toPromise();
}) : [];
Expand Down Expand Up @@ -1719,8 +1705,11 @@ export class ObservationDetailComponent implements OnDestroy {
observation['observation-report'] = this.reportChoice;
}
})
.then(() => observation.save().toPromise())
.then(() => this.updateDocuments(observation))
.then(() => {
observation['observation-documents'] = this.documents;
})
.then(() => observation.save().toPromise())
.then(async () => {
if (this.observation && !this.isCopied) {
alert(await this.translateService.get('observationUpdate.success').toPromise());
Expand Down

0 comments on commit 33e901c

Please sign in to comment.