1- // Libraries & stores
21import { plausible } from "@/ts/plausible"
32import * as API from "@/api/documents"
4- import { documentsPath } from "@/api/documents"
53import * as Utils from "@/api/utils"
64import stores from "@/stores"
7- // Types & API
8- import { type DocumentMetadata , Format } from "@/types/documents"
9- import { useAxios } from "@/api/useAxios"
5+ import { type DocumentMetadata } from "@/types/documents"
6+ import { addContentTypeHeader , fileExtension } from "@/ts/file"
107
118// Custom types
129type FileStatus = { status : "busy" | "success" | "error" ; message ?: string }
@@ -17,20 +14,12 @@ type FileStatus = { status: "busy" | "success" | "error"; message?: string }
1714 */
1815const documents = defineStore ( "documents" , ( ) => {
1916 // Stores
20- const errors = stores . useErrors ( )
2117 const { corpusId, corpus } = storeToRefs ( stores . useCorpora ( ) )
2218 const { reload : reloadCorpora } = stores . useCorpora ( )
2319
2420 // Fields
25- const {
26- data : documents ,
27- loading,
28- reload,
29- } = useAxios < DocumentMetadata [ ] > (
30- ( ) : string | undefined => ( corpusId . value ? documentsPath ( corpusId . value ) : undefined ) ,
31- [ ] ,
32- )
33-
21+ const loading = ref < boolean > ( false )
22+ const documents = ref < DocumentMetadata [ ] > ( [ ] )
3423 const numSourceAnnotations = computed ( ( ) => documents . value . filter ( ( i ) => i . summary ?. annotations . token > 0 ) . length )
3524 const uploading : Record < string , FileStatus > = reactive ( { } )
3625 const uploadBusyCount = computed ( ( ) => Object . values ( uploading ) . filter ( ( i ) => i . status === "busy" ) . length )
@@ -43,36 +32,34 @@ const documents = defineStore("documents", () => {
4332 } )
4433 } )
4534
46- /**
47- * Delete a document.
48- * @param name Document name.
49- */
50- function deleteDocument ( name : string ) : void {
35+ /** Reload documents and corpora (number of docs in corpusmetadata can change). */
36+ function reload ( ) : void {
37+ reloadCorpora ( )
38+ loading . value = true
39+ API . getDocuments ( corpusId . value )
40+ . then ( ( res ) => ( documents . value = res . data ) )
41+ . finally ( ( ) => ( loading . value = false ) )
42+ }
43+
44+ /** Delete a document. */
45+ function remove ( name : string ) : void {
5146 plausible . documentDeleted ( corpus . value , getDocument ( name ) )
5247 API . deleteDocument ( corpusId . value , name )
53- . catch ( ( error ) => errors . handle ( error ) )
5448 . finally ( reload )
5549 }
5650
57- /**
58- * Download original source document.
59- * @param name Document name.
60- */
61- function downloadRaw ( name : string ) : void {
51+ /** Download original source document. */
52+ function download ( name : string ) : void {
6253 plausible . documentDownloaded ( corpus . value , getDocument ( name ) )
6354 API . getRawDocument ( corpusId . value , name )
6455 . then ( Utils . browserDownloadResponseFile )
65- . catch ( ( res ) => Utils . handleBlobError ( res , "download raw document" , errors ) )
6656 }
6757
6858 function getDocument ( name : string ) : DocumentMetadata {
6959 return documents . value . find ( ( d : DocumentMetadata ) => d . name === name ) as DocumentMetadata
7060 }
7161
72- /**
73- * Upload all files in filesToUpload.
74- * Creates timeouts to spread load.
75- */
62+ /** Upload all files in filesToUpload. Creates timeouts to spread load. */
7663 function uploadAll ( ) : void {
7764 for ( let i = 0 ; i < filesToUpload . value . length ; i ++ ) {
7865 const formData = new FormData ( )
@@ -86,45 +73,13 @@ const documents = defineStore("documents", () => {
8673 filesToUpload . value = [ ]
8774 }
8875
89- /**
90- * Clear errors from not yet uploaded files.
91- */
76+ /** Clear errors from not yet uploaded files. */
9277 function clearUploadErrors ( ) : void {
9378 Object . keys ( uploading ) . forEach ( ( key ) => {
9479 if ( uploading [ key ] . status === "error" ) delete uploading [ key ]
9580 } )
9681 }
9782
98- /**
99- * Add content type header.
100- * @param fd FormData with file to upload.
101- * @param contentType Content type header.
102- * @param exts File extensions to apply the content type header to.
103- */
104- function addContentTypeHeader ( fd : FormData ) : Record < string , string > | null {
105- const exts_and_headers = {
106- tsv : "text/tab-separated-values" ,
107- conllu : "text/tab-separated-values" ,
108- naf : "text/xml" ,
109- }
110-
111- let file = fd . get ( "file" ) as File
112- const extension = fileExtension ( file )
113- let header = null
114-
115- if ( Object . keys ( exts_and_headers ) . includes ( extension ) ) {
116- const contentType = exts_and_headers [ extension ]
117- file = new File ( [ file ] , file . name , { type : contentType } )
118- header = { "Content-Type" : contentType }
119- fd . set ( "file" , file )
120- }
121- return header
122- }
123-
124- function fileExtension ( file : File ) : string {
125- return file . name . split ( "." ) . at ( - 1 )
126- }
127-
12883 /**
12984 * Upload a single file. Takes http content type header into account.
13085 * @param formData FormData with file to upload.
@@ -147,25 +102,10 @@ const documents = defineStore("documents", () => {
147102 . finally ( ( ) => {
148103 if ( uploadBusyCount . value === 0 ) {
149104 reload ( )
150- reloadCorpora ( )
151105 }
152106 } )
153107 }
154108
155- /**
156- * Checks if the documentsStore.documents contains at least one file of the given format.
157- */
158- function containsFormat ( format : Format ) : boolean {
159- return documents . value . some ( ( i ) => {
160- // Overwrite the format for legacy formats.
161- let otherFormat = i . format
162- if ( otherFormat === Format . TEI_P5_LEGACY ) {
163- otherFormat = Format . TEI_P5
164- }
165- return otherFormat === format
166- } )
167- }
168-
169109 // Exports
170110 return {
171111 // Fields
@@ -178,11 +118,11 @@ const documents = defineStore("documents", () => {
178118 uploadErrorCount,
179119 numSourceAnnotations,
180120 // Methods
181- deleteDocument,
182- downloadRaw,
121+ reload,
122+ remove,
123+ download,
183124 uploadAll,
184125 clearUploadErrors,
185- containsFormat,
186126 }
187127} )
188128
0 commit comments