From 95a701b25a16c7877fcb21058f506c6f595ba249 Mon Sep 17 00:00:00 2001 From: tmeftah Date: Sun, 25 Aug 2024 18:38:45 +0200 Subject: [PATCH] =?UTF-8?q?feat=20=E2=9C=A8:=20UI=20Store=20Document=20man?= =?UTF-8?q?agement=20functionality=20added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added document management functionality to the main store. --- frontend/src/stores/main-store.js | 94 ++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/frontend/src/stores/main-store.js b/frontend/src/stores/main-store.js index b11db52..5ccfc90 100644 --- a/frontend/src/stores/main-store.js +++ b/frontend/src/stores/main-store.js @@ -1,5 +1,5 @@ import { defineStore } from "pinia"; - +import { Notify } from "quasar"; import { useAuthStore } from "stores/auth"; const authStore = useAuthStore(); @@ -13,6 +13,8 @@ export const useMainStore = defineStore("main", { solution: "", models: JSON.parse(localStorage.getItem("models")) || [], model_name: localStorage.getItem("model_name") || "", + documents: [], + show_uploader: false, }), getters: {}, @@ -119,5 +121,95 @@ export const useMainStore = defineStore("main", { // }, + + async get_documents_list() { + try { + const response = await fetch(`${baseUrl}/documents`, { + method: "GET", + headers: { + Accept: "application/json", + Authorization: `Bearer ${authStore.token}`, + }, + }); + + if (!response.ok) { + // Extract error message if available + const errorData = await response.json().catch(() => ({})); // Parsing might fail, default to empty object + const errorMessage = + errorData.message || `Error: ${response.statusText}`; + throw new Error(errorMessage); + } + + const data = await response.json(); + this.documents = data; + + // + } catch (error) { + // Handle network errors and HTTP errors + if (error.name === "TypeError") { + // This typically indicates a network error + console.error("Network error: Could not reach the server"); + Notify.create({ + color: "negative", + position: "bottom", + message: error.message, + icon: "report_problem", + }); + } else { + // HTTP error, or some other error + console.error(`API error: ${error.message}`); + Notify.create({ + color: "negative", + position: "bottom", + message: error.message, + icon: "report_problem", + }); + } + + // You can rethrow the error or handle it in some way, e.g., user notification + } + }, + + async upload_documents() { + // returning a Promise + + return new Promise((resolve) => { + // simulating a delay of 2 seconds + setTimeout(() => { + resolve({ + url: `${baseUrl}/documents/upload`, + method: "POST", + headers: [ + { name: "Authorization", value: `Bearer ${authStore.token}` }, + ], + }); + }, 2000); + }); + }, + + async uploaded_success() { + Notify.create({ + color: "positive", + position: "bottom", + message: "uploaded", + icon: "done", + }); + + setTimeout(() => { + this.show_uploader = false; + }, 2000); + + this.get_documents_list(); + }, + + async upload_failed() { + Notify.create({ + color: "negative", + position: "bottom", + message: "could not be uploaded", + icon: "report_problem", + }); + this.show_uploader = true; + }, }, });