Skip to content

Commit

Permalink
feat ✨: UI Store Document management functionality added
Browse files Browse the repository at this point in the history
- Added document management functionality to the main store.
  • Loading branch information
tmeftah committed Aug 25, 2024
1 parent 3c83935 commit 95a701b
Showing 1 changed file with 93 additions and 1 deletion.
94 changes: 93 additions & 1 deletion frontend/src/stores/main-store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineStore } from "pinia";

import { Notify } from "quasar";
import { useAuthStore } from "stores/auth";

const authStore = useAuthStore();
Expand All @@ -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: {},

Expand Down Expand Up @@ -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;
},
},
});

0 comments on commit 95a701b

Please sign in to comment.