Commit a18d6e3b authored by David's avatar David
Browse files

Can add a document, and file it and rename it and description it

parent 62f275a2
......@@ -6,6 +6,9 @@
<div *ngIf="!loading" class="staging-documents-list">
<div *ngFor="let stagingFile of (stagingFiles | async)">
<app-staging-document [stagingFolderFile]="stagingFile"></app-staging-document>
<app-staging-document
[stagingFolderFile]="stagingFile.file"
[stagingFolder]="stagingFile.folder"
></app-staging-document>
</div>
</div>
import { Component, OnInit } from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {EMPTY, Observable, of, timer} from 'rxjs';
import {flatMap, tap} from 'rxjs/operators';
import {DocumentService} from './services/document.service';
import {StagingFolderFile} from '../mayan-api';
import {StagingFolder, StagingFolderFile} from '../mayan-api';
@Component({
selector: 'app-document-manager',
templateUrl: './document-manager.component.html',
styleUrls: ['./document-manager.component.scss']
selector: 'app-document-manager',
templateUrl: './document-manager.component.html',
styleUrls: ['./document-manager.component.scss']
})
export class DocumentManagerComponent implements OnInit {
loading = false;
stagingFiles: Observable<[StagingFolderFile]> = EMPTY;
loading = false;
stagingFiles: Observable<MayanStagedFile[]> = EMPTY;
constructor(private readonly documentService: DocumentService) { }
constructor(private readonly documentService: DocumentService) {
}
ngOnInit() {
// load staging documents, display them create a timer to update the list
of(1).pipe(
tap( m => this.loading = true),
flatMap(m => this.documentService.loadStagingFiles()),
tap(stagingFiles => {
this.stagingFiles = of(stagingFiles);
this.loading = false;
})
ngOnInit() {
// load staging documents, display them create a timer to update the list -- which is crappy
of(1).pipe(
tap(m => this.loading = true),
flatMap(m => this.documentService.loadStagingFolders()),
).subscribe(result => {
const stagedFiles: MayanStagedFile[] = result.flatMap(folder => {
return folder.files.map(file => {
return {
file,
folder
} as MayanStagedFile;
});
});
).subscribe(); // todo, need the destroy, or do this differently
}
this.stagingFiles = of(stagedFiles);
this.loading = false;
});
}
}
export interface MayanStagedFile {
file: StagingFolderFile;
folder: StagingFolder;
}
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {EMPTY, Observable} from 'rxjs';
import {Cabinet, MayanResult} from '../../mayan-api';
import {Cabinet, MayanResult, NewCabinetDocument} from '../../mayan-api';
import {environment} from '../../../environments/environment';
import {expand, map, reduce} from 'rxjs/operators';
......@@ -29,4 +29,11 @@ export class CabinetsService {
a.full_path.localeCompare(b.full_path)))
);
}
addDocumentToCabinet(cabinetId: number, documentKey: number): Observable<never> {
const payload = {
documents_pk_list: `${documentKey}`
} as NewCabinetDocument;
return this.httpClient.post<never>(`${environment.mayan}/cabinets/${cabinetId}/documents/`, payload);
}
}
import { Injectable } from '@angular/core';
import {Observable, of} from 'rxjs';
import {MayanResult, NewDocument, StagingFolder, StagingFolderFile} from '../../mayan-api';
import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {concat, EMPTY, Observable, of, throwError} from 'rxjs';
import {
MayanDocument,
MayanResult,
MayanWritableDocument,
NewDocument,
StagingFolder,
StagingFolderFile,
StagingFolderFileUpload
} from '../../mayan-api';
import {HttpClient, HttpParams} from '@angular/common/http';
import {environment} from '../../../environments/environment';
import {flatMap, map, tap} from 'rxjs/operators';
import {
catchError,
concatAll,
concatMap,
delay,
delayWhen,
expand,
flatMap,
map,
reduce,
retryWhen,
switchMap,
take,
tap
} from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class DocumentService {
constructor(private readonly httpClient: HttpClient) { }
constructor(private readonly httpClient: HttpClient) {
}
loadStagingFiles(page?: number ): Observable<[StagingFolderFile]> {
loadStagingFolders(): Observable<StagingFolder[]> {
return this.httpClient.get<MayanResult<StagingFolder>>(`${environment.mayan}/staging_folders/`).pipe(
expand(response => {
if (response.next !== null) {
// another request
return this.httpClient.get<MayanResult<StagingFolder>>(response.next);
} else {
return EMPTY;
}
}),
map(result => result.results.flatMap(array => array)),
reduce((acc: StagingFolder[], x: StagingFolder[]) => acc.concat(x), [])
);
}
uploadStagingDocument(stagingFolderFile: StagingFolderFile, documentType: number): Observable<MayanDocument> {
const payload = {
document_type: documentType,
expand: false
} as StagingFolderFileUpload;
return this.httpClient.post(
stagingFolderFile.upload_url,
payload)
.pipe(
tap(response => console.log('response', response)),
switchMap(r => this.findFreshDocumentByFilename(stagingFolderFile)),
tap(n => console.log('after finding document', n)),
);
}
findFreshDocumentByFilename(stagingFolderFile: StagingFolderFile): Observable<MayanDocument> {
const params = new HttpParams().set('label', stagingFolderFile.filename);
return this.httpClient.get<MayanResult<MayanDocument>>(`${environment.mayan}/search/documents.Document/`, {
params
}).pipe(
take(1),
map(result => {
return result.results[0];
if (result.count === 0) {
throw result.count;
} else {
return result.results[0];
}
}),
flatMap(stagingFolder => of(stagingFolder.files))
// https://www.dev6.com/angular/angular-rxjs-retrywhen-in-depth/
// Man this was confusing, because I'm not very smart.
retryWhen(result =>
result.pipe(
concatMap(internalResult => {
// here we can check the error.
// We can specify the retry only if we are getting 5xx errors for instance.
if (internalResult === 0) {
return of(result);
}
// in other cases we throw an error down the pipe
return throwError(result);
}),
delay(100),
// we can keep calling forever but usually we want to avoid this.
// So, we set the number of attempts including the initial one.
take(21),
o => concat(o, throwError(`Sorry, there was no result after 20 retries)`))
)
),
);
}
importDocument(newDocument: NewDocument): Observable<any> {
return this.httpClient.post(`${environment.mayan}/documents/`, newDocument).pipe(
tap(response => console.log('Creating document response: ', response))
updateDocument(mayanDocument: MayanDocument, writableDocument: MayanWritableDocument): Observable<MayanWritableDocument> {
return this.httpClient.patch(mayanDocument.url, writableDocument).pipe(
);
}
}
......@@ -25,7 +25,6 @@ export class TagsService {
return EMPTY;
}
}),
// @ts-ignore
map(result => result.results.flatMap(array => array)),
reduce((acc: Tag[], x: Tag[]) => acc.concat(x), []),
);
......
import {Component, Input, OnInit} from '@angular/core';
import {NewDocument, StagingFolderFile, Tag} from '../../mayan-api';
import {MayanWritableDocument, NewDocument, StagingFolder, StagingFolderFile, Tag} from '../../mayan-api';
import {TagsService} from '../services/tags.service';
import {FormControl, FormGroup, Validators} from '@angular/forms';
import {CabinetsService} from '../services/cabinets.service';
import {finalize, map, tap} from 'rxjs/operators';
import {finalize, flatMap, map, tap} from 'rxjs/operators';
import {DocumentTypesService} from '../services/document-types.service';
import {DocumentService} from '../services/document.service';
......@@ -15,6 +15,7 @@ import {DocumentService} from '../services/document.service';
export class StagingDocumentComponent implements OnInit {
@Input() stagingFolderFile: StagingFolderFile;
@Input() stagingFolder: StagingFolder;
constructor(private readonly tagsService: TagsService,
private readonly cabinetsService: CabinetsService,
......@@ -74,11 +75,31 @@ export class StagingDocumentComponent implements OnInit {
};
this.submitting = true;
this.documentService.importDocument(newDoc)
.subscribe(() => {
// TODO: need to remove myself from the list?
this.documentService.uploadStagingDocument(this.stagingFolderFile,
this.docForm.controls.selectedDocumentType.value)
.pipe(
flatMap(freshDocument => {
const update = {
description: this.docForm.controls.description.value,
label: this.docForm.controls.label.value,
language: 'English',
} as MayanWritableDocument;
return this.documentService.updateDocument(freshDocument, update).pipe(
map(_ => freshDocument),
);
}),
flatMap(doc => {
return this.cabinetsService.addDocumentToCabinet(
this.docForm.controls.selectedCabinet.value,
doc.id
);
}),
)
.subscribe(freshDocument => {
this.submitting = false;
// TODO: could trigger a reload of the stuff!
});
}
......
......@@ -23,6 +23,10 @@ export interface User {
}
export interface StagingFolder {
delete_after_upload: boolean;
id: number;
preview_height: number;
preview_width: number;
files: [StagingFolderFile];
}
......@@ -30,6 +34,8 @@ export interface StagingFolderFile {
filename: string;
image_url: string;
url: string;
encoded_filename: string;
upload_url: string;
}
export interface WritableTag {
......@@ -59,6 +65,11 @@ export interface Cabinet {
url: string;
}
export interface StagingFolderFileUpload {
document_type: number;
expand: boolean;
}
export interface NewDocument {
description: string;
document_type: number;
......@@ -85,3 +96,27 @@ export interface MayanDocumentType {
export interface DocumentTypeFilename {
filename: string;
}
export interface MayanDocument {
date_added: string;
description: string;
document_type: MayanDocumentType;
document_type_change_url: string;
id: number;
label: string;
language: string;
url: string;
uuid: string;
pk: number;
versions_url: string;
}
export interface MayanWritableDocument {
description?: string;
label?: string;
language?: string;
}
export interface NewCabinetDocument {
documents_pk_list: string;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment