From 3f2ed6c3fd9d9ec980af37b5b7776205fead0bb8 Mon Sep 17 00:00:00 2001 From: Orzu Ionut Date: Thu, 3 Jun 2021 15:03:55 +0300 Subject: [PATCH] Fix various issues --- .../SearchAndDisplaceController.php | 11 +- app/SearchDisplace/Documents/DocumentFile.php | 16 +- app/SearchDisplace/Searchers/Searcher.php | 10 +- public/js/app.js | 269 ++++++++++-------- resources/js/SearchDisplace/helpers.ts | 18 ++ resources/js/components/Home/Home.ts | 5 + resources/js/components/Home/Home.vue | 76 +++-- .../js/components/ProcessFile/ProcessFile.ts | 69 +++-- .../js/components/ProcessFile/ProcessFile.vue | 4 +- .../responses/FileStatusResponse.ts | 6 +- 10 files changed, 302 insertions(+), 182 deletions(-) create mode 100644 resources/js/SearchDisplace/helpers.ts diff --git a/app/Http/Controllers/SearchAndDisplaceController.php b/app/Http/Controllers/SearchAndDisplaceController.php index 41be3e4..3300679 100644 --- a/app/Http/Controllers/SearchAndDisplaceController.php +++ b/app/Http/Controllers/SearchAndDisplaceController.php @@ -12,14 +12,13 @@ class SearchAndDisplaceController extends Controller $handler = new DocumentFile(); try { - $documentContent = $handler->getAfterIngest($id); + $result = $handler->getAfterIngest($id); - $handler->destroy($id); + if ($result['status'] !== 'processing') { + $handler->destroy($id); + } - return response()->json([ - 'content' => $documentContent, - 'ingest_status' => !! $documentContent ? 'success' : 'fail', - ], 200); + return response()->json($result, 200); } catch (\Exception $exception) { return response()->json([ 'message' => $exception->getMessage(), diff --git a/app/SearchDisplace/Documents/DocumentFile.php b/app/SearchDisplace/Documents/DocumentFile.php index eb4905a..e79c54f 100644 --- a/app/SearchDisplace/Documents/DocumentFile.php +++ b/app/SearchDisplace/Documents/DocumentFile.php @@ -19,15 +19,23 @@ class DocumentFile // Ingest success. if ($this->storage->exists("$path.md")) { - return $this->storage->get("$path.md"); + return [ + 'status' => 'success', + 'content' => $this->storage->get("$path.md"), + ]; } // Ingest fail. if ($this->storage->exists($path)) { - return ''; + return [ + 'status' => 'fail', + ]; } - throw new \Exception('Document has not been processed yet.'); + return [ + 'status' => 'processing', + 'message' => 'Document has not been processed yet.', + ]; } public function destroy($id) @@ -44,7 +52,7 @@ class DocumentFile return $this->storage->delete($path); } - throw new \Exception('Document has not been processed yet.'); + return false; } protected function getPath($id) diff --git a/app/SearchDisplace/Searchers/Searcher.php b/app/SearchDisplace/Searchers/Searcher.php index 37e6f33..2b56b80 100644 --- a/app/SearchDisplace/Searchers/Searcher.php +++ b/app/SearchDisplace/Searchers/Searcher.php @@ -179,13 +179,21 @@ class Searcher return []; } - return array_map(function ($item) { + $results = array_map(function ($item) { + if ( ! $item[0]) { + return []; + } + return [ 'start' => $item[1], 'end' => $item[1] + strlen($item[0]) - 1, 'content' => $item[0], ]; }, $matches[0]); + + return array_filter($results, function ($result) { + return count($result) > 0; + }); } protected function processResults($results) diff --git a/public/js/app.js b/public/js/app.js index 1a49512..8dd53c9 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -3383,6 +3383,7 @@ var Home = /*#__PURE__*/function (_Vue) { id: '', file_name: '' }; + _this.error = ''; return _this; } /** @@ -3448,6 +3449,11 @@ var Home = /*#__PURE__*/function (_Vue) { return uploadFile; }() + }, { + key: "onError", + value: function onError(error) { + this.error = error; + } }]); return Home; @@ -3642,7 +3648,7 @@ var ProcessFile = /*#__PURE__*/function (_Vue) { key: "waitForFile", value: function () { var _waitForFile = _asyncToGenerator( /*#__PURE__*/_babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0___default().mark(function _callee() { - var response; + var response, error; return _babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0___default().wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { @@ -3653,27 +3659,38 @@ var ProcessFile = /*#__PURE__*/function (_Vue) { case 2: response = _context.sent; - if (response.content !== null) { - if (response.ingest_status === 'fail') { - this.$toast.add({ - severity: 'error', - summary: 'File error', - detail: 'THere was an error processing the file in ingest', - life: 3000 - }); - } else { - this.fileContent = response.content; - this.$toast.add({ - severity: 'success', - summary: 'File loaded', - detail: 'The file has been processed by ingest.', - life: 3000 - }); - clearInterval(this.intervalId); - } + if (!(response.status === 'processing')) { + _context.next = 5; + break; } - case 4: + return _context.abrupt("return"); + + case 5: + clearInterval(this.intervalId); + + if (response.status === 'success') { + this.fileContent = response.content ? response.content : ''; + this.$toast.add({ + severity: 'success', + summary: 'File loaded', + detail: 'The file has been processed by ingest.', + life: 3000 + }); + } + + if (response.status === 'fail') { + error = 'There was an error processing the file in ingest'; + this.$toast.add({ + severity: 'error', + summary: 'File error', + detail: error, + life: 3000 + }); + this.$emit('error', error); + } + + case 8: case "end": return _context.stop(); } @@ -3726,22 +3743,32 @@ var ProcessFile = /*#__PURE__*/function (_Vue) { 'replace_with': _this2.searchersOptions[searcher.id] || '' }); }); - _context2.next = 6; + _context2.prev = 4; + _context2.next = 7; return this.$api.filterDocument(this.fileContent, searchers); - case 6: + case 7: response = _context2.sent; this.processedFileContent = response.content; this.documentDiffIndexes = response.indexes; this.createDiffPreview(); this.processing = false; + _context2.next = 17; + break; - case 11: + case 14: + _context2.prev = 14; + _context2.t0 = _context2["catch"](4); + this.$emit('error', 'Server error.'); // if (isServerError(e)) { + // this.$emit('error', getServerErrorMessage(e)); + // } + + case 17: case "end": return _context2.stop(); } } - }, _callee2, this); + }, _callee2, this, [[4, 14]]); })); function runSearchers() { @@ -27997,85 +28024,111 @@ var render = function() { var _vm = this var _h = _vm.$createElement var _c = _vm._self._c || _h - return !_vm.fileUploaded && !_vm.uploading - ? _c( - "div", - { staticClass: "wrap" }, - [ - _c( - "Panel", - { attrs: { header: "Please upload a file" } }, - [ - _c("FileUpload", { - attrs: { name: "demo[]", customUpload: true, auto: true }, - on: { uploader: _vm.uploadFile }, - scopedSlots: _vm._u( - [ - { - key: "empty", - fn: function() { - return [ - _c("p", [ - _vm._v("Drag and drop files to here to upload.") - ]) - ] - }, - proxy: true - } - ], - null, - false, - 4144631135 - ) - }) - ], - 1 - ), - _vm._v(" "), - _c("BlockUI", { attrs: { blocked: _vm.uiBlocked, fullScreen: true } }) - ], - 1 - ) - : !_vm.fileUploaded && _vm.uploading - ? _c( - "div", - { staticClass: "wrap" }, - [ - _c("Skeleton"), - _c("br"), - _vm._v(" "), - _c("Skeleton"), - _c("br"), - _vm._v(" "), - _c("Skeleton"), - _c("br"), - _vm._v(" "), - _c("Skeleton"), - _c("br"), - _vm._v(" "), - _c("Skeleton"), - _c("br"), - _vm._v(" "), - _c("Skeleton"), - _c("br"), - _vm._v(" "), - _c("Skeleton"), - _c("br") - ], - 1 - ) - : _c( - "div", - { staticClass: "wrap" }, - [ - _c("process-file", { - attrs: { file: _vm.uploadResult, searchers: _vm.searchers } - }) - ], - 1 - ) + return _c( + "div", + { staticClass: "wrap" }, + [ + !_vm.error + ? [ + !_vm.fileUploaded + ? [ + !_vm.uploading + ? [ + _c( + "Panel", + { attrs: { header: "Please upload a file" } }, + [ + _c("FileUpload", { + attrs: { + name: "demo[]", + customUpload: true, + auto: true + }, + on: { uploader: _vm.uploadFile }, + scopedSlots: _vm._u( + [ + { + key: "empty", + fn: function() { + return [ + _c("p", [ + _vm._v( + "Drag and drop files to here to upload." + ) + ]) + ] + }, + proxy: true + } + ], + null, + false, + 4144631135 + ) + }) + ], + 1 + ), + _vm._v(" "), + _c("BlockUI", { + attrs: { blocked: _vm.uiBlocked, fullScreen: true } + }) + ] + : [ + _c("Skeleton"), + _c("br"), + _vm._v(" "), + _c("Skeleton"), + _c("br"), + _vm._v(" "), + _c("Skeleton"), + _c("br"), + _vm._v(" "), + _c("Skeleton"), + _c("br"), + _vm._v(" "), + _c("Skeleton"), + _c("br"), + _vm._v(" "), + _c("Skeleton"), + _c("br"), + _vm._v(" "), + _c("Skeleton"), + _c("br") + ] + ] + : [ + _c("process-file", { + attrs: { file: _vm.uploadResult, searchers: _vm.searchers }, + on: { error: _vm.onError } + }) + ] + ] + : [ + _c("h1", [_vm._v(" Something went wrong while processing. ")]), + _vm._v(" "), + _c("p", [_vm._v(" " + _vm._s(_vm.error) + " ")]), + _vm._v(" "), + _vm._m(0) + ] + ], + 2 + ) } -var staticRenderFns = [] +var staticRenderFns = [ + function() { + var _vm = this + var _h = _vm.$createElement + var _c = _vm._self._c || _h + return _c("a", { attrs: { href: "/" } }, [ + _c( + "button", + { staticClass: "p-button p-button-primary", attrs: { type: "button" } }, + [_vm._v("\n Try another file\n ")] + ) + ]) + } +] render._withStripped = true @@ -28236,11 +28289,7 @@ var render = function() { icon: "pi pi-download", disabled: _vm.processedFileContent == "" }, - on: { - click: function($event) { - return _vm.downloadOdt() - } - } + on: { click: _vm.downloadOdt } }), _vm._v(" "), _c("Button", { @@ -28253,11 +28302,7 @@ var render = function() { _vm.fileContent == "" || _vm.selectedSearchers.length === 0 }, - on: { - click: function($event) { - return _vm.runSearchers() - } - } + on: { click: _vm.runSearchers } }) ] }, diff --git a/resources/js/SearchDisplace/helpers.ts b/resources/js/SearchDisplace/helpers.ts new file mode 100644 index 0000000..f85bf3a --- /dev/null +++ b/resources/js/SearchDisplace/helpers.ts @@ -0,0 +1,18 @@ +export function isServerError(e: any) { + return e && + typeof (e) === 'object' && + e.hasOwnProperty('response') && + e.response && + e.response.hasOwnProperty('data') && + e.response.data; +} + +export function getServerErrorMessage(e: any) { + const error = e.response.data; + + if (error.hasOwnProperty('message')) { + return error.message; + } + + return ''; +} diff --git a/resources/js/components/Home/Home.ts b/resources/js/components/Home/Home.ts index 1ddeea5..380ed24 100644 --- a/resources/js/components/Home/Home.ts +++ b/resources/js/components/Home/Home.ts @@ -13,6 +13,7 @@ export default class Home extends Vue { id: '', file_name: '', }; + public error: string = ''; /** * A method which uploads the files to the server for processing @@ -46,4 +47,8 @@ export default class Home extends Vue { this.fileUploaded = false; } } + + public onError(error: string) { + this.error = error; + } } diff --git a/resources/js/components/Home/Home.vue b/resources/js/components/Home/Home.vue index eb8e1e5..5774bf5 100644 --- a/resources/js/components/Home/Home.vue +++ b/resources/js/components/Home/Home.vue @@ -1,33 +1,55 @@ diff --git a/resources/js/interfaces/responses/FileStatusResponse.ts b/resources/js/interfaces/responses/FileStatusResponse.ts index f94ad96..6a5cb36 100644 --- a/resources/js/interfaces/responses/FileStatusResponse.ts +++ b/resources/js/interfaces/responses/FileStatusResponse.ts @@ -1,5 +1,5 @@ export default interface FileStatusResponse { - content: string; - ingest_status: string; -} \ No newline at end of file + content?: string; + status: string; +}