diff --git a/docat/docat/app.py b/docat/docat/app.py index 5fae0a724..98ab779c9 100644 --- a/docat/docat/app.py +++ b/docat/docat/app.py @@ -243,9 +243,9 @@ def upload( extract_archive(target_file, base_path) if not (base_path / "index.html").exists(): - return ApiResponse(message="File successfully uploaded, but no index.html found at root of archive.") + return ApiResponse(message="Documentation uploaded successfully, but no index.html found at root of archive.") - return ApiResponse(message="File successfully uploaded") + return ApiResponse(message="Documentation uploaded successfully") @app.put("/api/{project}/{version}/tags/{new_tag}", response_model=ApiResponse, status_code=status.HTTP_201_CREATED) diff --git a/docat/tests/test_upload.py b/docat/tests/test_upload.py index da805ff80..2bd115e33 100644 --- a/docat/tests/test_upload.py +++ b/docat/tests/test_upload.py @@ -11,7 +11,7 @@ def test_successfully_upload(client): response_data = response.json() assert response.status_code == 201 - assert response_data["message"] == "File successfully uploaded" + assert response_data["message"] == "Documentation uploaded successfully" assert (docat.DOCAT_UPLOAD_FOLDER / "some-project" / "1.0.0" / "index.html").exists() @@ -30,7 +30,7 @@ def test_successfully_override(client_with_claimed_project): response_data = response.json() assert response.status_code == 201 - assert response_data["message"] == "File successfully uploaded" + assert response_data["message"] == "Documentation uploaded successfully" assert remove_mock.mock_calls == [call("some-project", "1.0.0", docat.DOCAT_UPLOAD_FOLDER)] @@ -182,6 +182,6 @@ def test_upload_issues_warning_missing_index_file(client_with_claimed_project): response_data = response.json() assert response.status_code == 201 - assert response_data["message"] == "File successfully uploaded, but no index.html found at root of archive." + assert response_data["message"] == "Documentation uploaded successfully, but no index.html found at root of archive." assert (docat.DOCAT_UPLOAD_FOLDER / "some-project" / "1.0.0" / "some-other-file.html").exists() assert not (docat.DOCAT_UPLOAD_FOLDER / "some-project" / "1.0.0" / "index.html").exists() diff --git a/web/src/pages/Upload.tsx b/web/src/pages/Upload.tsx index d7c0765b0..ccbca54cf 100644 --- a/web/src/pages/Upload.tsx +++ b/web/src/pages/Upload.tsx @@ -25,7 +25,7 @@ const okFileTypes = [ 'application/x-zip-compressed' ] -export default function Upload(): JSX.Element { +export default function Upload (): JSX.Element { document.title = 'Upload | docat' const { reload: reloadProjects } = useProjects() @@ -97,37 +97,36 @@ export default function Upload(): JSX.Element { if (!validateFile(file) || file === undefined) return setIsUploading(true) - try { - const formData = new FormData() - formData.append('file', file) + const formData = new FormData() + formData.append('file', file) - await ProjectRepository.upload(project, version, formData) + const { success, message } = await ProjectRepository.upload(project, version, formData) - // reset the form - setProject('') - setVersion('') - setFile(undefined) - setValidation({}) - showMessage({ - type: 'success', - content: 'Documentation uploaded successfully', - showMs: 6000 - }) - - // reload the projects - reloadProjects() - } catch (e) { - console.error(e) - - const message = (e as { message: string }).message + if (!success) { + console.error(message) showMessage({ type: 'error', content: message, showMs: 6000 }) - } finally { setIsUploading(false) + return } + + // reset the form + setProject('') + setVersion('') + setFile(undefined) + setValidation({}) + + showMessage({ + type: 'success', + content: message, + showMs: 6000 + }) + + reloadProjects() + setIsUploading(false) })() } diff --git a/web/src/repositories/ProjectRepository.ts b/web/src/repositories/ProjectRepository.ts index 09c45f6ad..6ce523da9 100644 --- a/web/src/repositories/ProjectRepository.ts +++ b/web/src/repositories/ProjectRepository.ts @@ -104,24 +104,45 @@ function getProjectDocsURL (projectName: string, version: string, docsPath?: str * @param {string} projectName Name of the project * @param {string} version Name of the version * @param {FormData} body Data to upload + * @returns {Promise<{ success: boolean, message: string }>} Success status and (error) message */ -async function upload (projectName: string, version: string, body: FormData): Promise { - const resp = await fetch(`/api/${projectName}/${version}`, - { - method: 'POST', - body +async function upload (projectName: string, version: string, body: FormData): Promise<{ success: boolean, message: string }> { + try { + const resp = await fetch(`/api/${projectName}/${version}`, + { + method: 'POST', + body + } + ) + + if (resp.ok) { + const json = await resp.json() as { message: string } + const msg = json.message + return { success: true, message: msg } } - ) - if (resp.ok) return - - switch (resp.status) { - case 401: - throw new Error('Failed to upload documentation: Version already exists') - case 504: - throw new Error('Failed to upload documentation: Server unreachable') - default: - throw new Error(`Failed to upload documentation: ${(await resp.json() as { message: string }).message}`) + switch (resp.status) { + case 401: + return { + success: false, + message: 'Failed to upload documentation: Version already exists' + } + case 504: + return { + success: false, + message: 'Failed to upload documentation: Server unreachable' + } + default: + return { + success: false, + message: `Failed to upload documentation: ${(await resp.json() as { message: string }).message}` + } + } + } catch (e) { + return { + success: false, + message: `Failed to upload documentation: ${(e as { message: string }).message}}` + } } } diff --git a/web/src/tests/repositories/ProjectRepository.test.ts b/web/src/tests/repositories/ProjectRepository.test.ts index 9adbc806a..c5e0bdfb4 100644 --- a/web/src/tests/repositories/ProjectRepository.test.ts +++ b/web/src/tests/repositories/ProjectRepository.test.ts @@ -90,12 +90,12 @@ describe('upload', () => { const project = 'test-project' const version = '1.0.0' - mockFetchData({}) + mockFetchData({ message: 'Documentation was uploaded successfully' }) const body = new FormData() body.append('file', new Blob([''], { type: 'text/plain' })) - await ProjectRepository.upload(project, version, body) + const { success, message } = await ProjectRepository.upload(project, version, body) expect(global.fetch).toHaveBeenCalledTimes(1) expect(global.fetch).toHaveBeenCalledWith(`/api/${project}/${version}`, @@ -104,6 +104,9 @@ describe('upload', () => { method: 'POST' } ) + + expect(success).toEqual(true) + expect(message).toEqual('Documentation was uploaded successfully') }) test('should throw version already exists on 401 status code', async () => { @@ -115,8 +118,10 @@ describe('upload', () => { const body = new FormData() body.append('file', new Blob([''], { type: 'text/plain' })) - expect(ProjectRepository.upload(project, version, body) - ).rejects.toThrow('Failed to upload documentation: Version already exists') + const { success, message } = await ProjectRepository.upload(project, version, body) + + expect(success).toEqual(false) + expect(message).toEqual('Failed to upload documentation: Version already exists') }) test('should throw server unreachable on 504 status code', async () => { @@ -128,8 +133,10 @@ describe('upload', () => { const body = new FormData() body.append('file', new Blob([''], { type: 'text/plain' })) - expect(ProjectRepository.upload(project, version, body) - ).rejects.toThrow('Failed to upload documentation: Server unreachable') + const { success, message } = await ProjectRepository.upload(project, version, body) + + expect(success).toEqual(false) + expect(message).toEqual('Failed to upload documentation: Server unreachable') }) test('should throw error on other status code', async () => { @@ -141,8 +148,10 @@ describe('upload', () => { const body = new FormData() body.append('file', new Blob([''], { type: 'text/plain' })) - expect(ProjectRepository.upload(project, version, body) - ).rejects.toThrow('Failed to upload documentation: Test Error') + const { success, message } = await ProjectRepository.upload(project, version, body) + + expect(success).toEqual(false) + expect(message).toEqual('Failed to upload documentation: Test Error') }) })