Skip to content

Commit

Permalink
files: add files features to the organisations and collections.
Browse files Browse the repository at this point in the history
Co-Authored-by: Johnny Mariéthoz <[email protected]>
  • Loading branch information
jma committed Aug 5, 2024
1 parent 28b4283 commit ff4830e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ <h1 class="mb-4">{{ record.metadata.name | languageValue | async }}</h1>
<h4 translate>Description</h4>
<p [innerHtml]="record.metadata.description | languageValue | async | markdown"></p>
}
<sonar-upload-files
recordType="collections"
[pid]="record.metadata.pid"
></sonar-upload-files>
</ng-container>
21 changes: 11 additions & 10 deletions projects/sonar/src/app/record/document/detail/detail.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,17 @@ <h5 class="d-inline mr-1" *ngFor="let value of subject.label.value">

<!-- TABS -->
<tabset>
<tab #tabOther="tab" id="documents-other-files-tab" [active]="true">
@if (filteredFiles.length > 1) {
<ng-template tabHeading translate>Other files</ng-template>
<!-- FILES -->
@if (tabOther.active) {
<div class="mt-4">
<sonar-other-files [documentPid]="record.pid"></sonar-other-files>
</div>
} }
</tab>
@if (filteredFiles.length > 1) {
<tab #tabOther="tab" id="documents-other-files-tab" [active]="true">
<ng-template tabHeading translate>Other files</ng-template>
<!-- FILES -->
@if (tabOther.active) {
<div class="mt-4">
<sonar-other-files [documentPid]="record.pid"></sonar-other-files>
</div>
}
</tab>
}

<tab #tabStats="tab" id="documents-stats">
<ng-template tabHeading translate>Statistics</ng-template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
*/

import { HttpClient } from '@angular/common/http';
import { Component, ViewChild, effect, inject, input, output } from '@angular/core';
import {
Component,
ViewChild,
effect,
inject,
input,
output,
} from '@angular/core';
import { toObservable, toSignal } from '@angular/core/rxjs-interop';
import { TranslateService } from '@ngx-translate/core';
import { DialogService, RecordService } from '@rero/ng-core';
Expand Down Expand Up @@ -147,7 +154,7 @@ export class UploadFilesComponent {
);
if (indexToUpdate >= 0) {
this.fileService
.put(`/api/documents/${this.pid()}`, this.record)
.put(`/api/${this.recordType()}/${this.pid()}`, this.record)
.subscribe((record: any) => {
// update the current record
this.record = record.metadata;
Expand Down Expand Up @@ -212,7 +219,10 @@ export class UploadFilesComponent {
let fileUpload: File = event.fileUpload;
this.spinner.show('file-upload');
this.fileService
.put(`/api/documents/${this.pid()}/files/${file.key}`, fileUpload)
.put(
`/api/${this.recordType()}/${this.pid()}/files/${file.key}`,
fileUpload
)
.pipe(
catchError((e: any) => {
let msg = this.translateService.instant('Server error');
Expand Down Expand Up @@ -261,7 +271,10 @@ export class UploadFilesComponent {
private generateCreateRequests(event): Observable<any> {
return from(event.files).pipe(
concatMap((f: any) =>
this.fileService.put(`/api/documents/${this.pid()}/files/${f.name}`, f)
this.fileService.put(
`/api/${this.recordType()}/${this.pid()}/files/${f.name}`,
f
)
),
map((file: any) => {
this.nUploadedFiles += 1;
Expand Down Expand Up @@ -335,7 +348,9 @@ export class UploadFilesComponent {
if (confirm === true) {
// remove the file
return this.fileService
.delete(`/api/documents/${this.pid()}/files/${file.key}`)
.delete(
`/api/${this.recordType()}/${this.pid()}/files/${file.key}`
)
.pipe(
map((res) => {
this.files = this.files.filter((f) => f.key !== file.key);
Expand Down Expand Up @@ -368,7 +383,7 @@ export class UploadFilesComponent {
*/
private getFiles(record): Observable<any> {
return this.fileService
.get(`/api/documents/${record.pid}/files?versions`)
.get(`/api/${this.recordType()}/${record.pid}/files?versions`)
.pipe(
map((record: any) => {
if (record?.contents) {
Expand Down Expand Up @@ -405,15 +420,21 @@ export class UploadFilesComponent {
// get old versions
let versions = {};
files.map((file) => {
if (file?.metadata?.type === 'file' && file.is_head === false) {
if (file?.metadata?.type && file.metadata.type !== 'file') {
return;
}
if (file.is_head === false) {
if (!(file.key in versions)) versions[file.key] = [];
versions[file.key].push(file);
}
});
// get head files only
let headFiles = [];
files.map((file) => {
if (file?.metadata?.type === 'file' && file.is_head) {
if (file?.metadata?.type && file.metadata.type !== 'file') {
return;
}
if (file.is_head) {
// add versions if exists
if (versions[file.key]) {
let fileVersions = versions[file.key];
Expand All @@ -436,7 +457,7 @@ export class UploadFilesComponent {
recordFile.order = index + 1;
});
this.fileService
.put(`/api/documents/${this.pid()}`, this.record)
.put(`/api/${this.recordType()}/${this.pid()}`, this.record)
.subscribe((record: any) => {
this.record = record.metadata;
this.files.map((file) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,8 @@ <h1 class="mb-4">{{ record.metadata.name }}</h1>
</ng-template>
</sonar-field-description>
</dl>
<sonar-upload-files
recordType="organisations"
[pid]="record.metadata.pid"
></sonar-upload-files>
</ng-container>
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, inject } from '@angular/core';
import { RecordService } from '@rero/ng-core';
import { combineLatest, Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { map, switchMap } from 'rxjs/operators';

@Component({
templateUrl: './detail.component.html',
Expand All @@ -29,6 +29,8 @@ export class DetailComponent implements OnInit {
/** Organisation record. */
record: any;

recordService = inject(RecordService);

/** Subdivisions list. */
subdivisions: Array<any> = [];

Expand All @@ -40,7 +42,7 @@ export class DetailComponent implements OnInit {
*
* @param _recordService: Record service.
*/
constructor(private _recordService: RecordService) {}
constructor() {}

/**
* Component init.
Expand All @@ -53,11 +55,11 @@ export class DetailComponent implements OnInit {
switchMap((record: any) => {
this.record = record;
return combineLatest([
this._recordService.getRecords(
this.recordService.getRecords(
'subdivisions',
`organisation.pid:${record.id}`
),
this._recordService.getRecords(
this.recordService.getRecords(
'collections',
`organisation.pid:${record.id}`
),
Expand All @@ -69,4 +71,10 @@ export class DetailComponent implements OnInit {
this.collections = result[1].hits.hits;
});
}

updateFiles(files) {
this.recordService.getRecord('organisations', this.record.id, 1).pipe(
map(doc => this.record._files = doc.metadata._files)
).subscribe();
}
}

0 comments on commit ff4830e

Please sign in to comment.