{{ marker.location }}
| # | diff --git a/frontend/src/components/Tabelle/TabellaSegnalazioni.vue b/frontend/src/components/Tabelle/TabellaSegnalazioni.vue new file mode 100644 index 0000000..cd47864 --- /dev/null +++ b/frontend/src/components/Tabelle/TabellaSegnalazioni.vue @@ -0,0 +1,109 @@ + + + +
|---|
| # | +Autore | +Inviata il | +Luogo | +Stato | ++ |
|---|---|---|---|---|---|
| + {{ index + 1 }} + | ++ {{ report.email }} + | ++ {{ report.createdAt }} + | ++ {{ report.location }} + | +
+ |
+
+ |
+
| # | diff --git a/frontend/src/data/algolia.js b/frontend/src/data/algolia.js deleted file mode 100644 index 6ec1e00..0000000 --- a/frontend/src/data/algolia.js +++ /dev/null @@ -1,24 +0,0 @@ -import { algoliasearch } from "algoliasearch"; - -// Aggiorna emergencies_index con i dati presi da apiEmergencies -export async function updateAlgoliaRecords() { - // Mi collego all'API di Algolia - const algolia = algoliasearch(import.meta.env.VITE_ALGOLIA_APP_ID, import.meta.env.VITE_ALGOLIA_WRITE_KEY); - - const processRecords = async () => { - // Svuoto prima emergencies_index - const response = await algolia.clearObjects({ indexName: 'emergencies_index' }); - - // Recupero poi le emergenze aggiornata dalla chiamata API - const apiEmergencies = import.meta.env.VITE_API_BASE_URL + "/emergencies/" - const datasetRequest = await fetch(apiEmergencies); - const emergenciesRecord = await datasetRequest.json(); - - // Inserisco infine le emergenze ottenuto in emergencies_index - return await algolia.saveObjects({ indexName: 'emergencies_index', objects: emergenciesRecord }); - }; - - processRecords() - .then(() => console.log('Algolia: emergencies successfully updated and indexed')) - .catch((err) => console.error(err)); -} \ No newline at end of file diff --git a/frontend/src/data/emergencies.js b/frontend/src/data/emergencies.js index 61a7dae..90a4e6e 100644 --- a/frontend/src/data/emergencies.js +++ b/frontend/src/data/emergencies.js @@ -23,6 +23,12 @@ function formattaData(date) { }); } +export function resetEmergencies() { + emergency.value = []; + emergencies.value = []; + emergenciesInProgress.value = []; +} + export async function nEmergencies() { try { const response = await fetch(apiEmergencies); @@ -70,7 +76,7 @@ export async function getEmergencyById(id) { } export function getEmergenciesInProgress() { - fetch(apiEmergencies + "/?state=in_progress") + fetch(apiEmergencies + "?state=in_corso") .then((response) => response.json()) .then((data) => { emergenciesInProgress.value = data.map((data) => { diff --git a/frontend/src/data/reports.js b/frontend/src/data/reports.js new file mode 100644 index 0000000..917e7ba --- /dev/null +++ b/frontend/src/data/reports.js @@ -0,0 +1,170 @@ +import { loggedUser } from "@/states/loggedUser"; +import { ref } from "vue"; + +const apiReports = import.meta.env.VITE_API_BASE_URL + "/reports/"; +const apiUsers = import.meta.env.VITE_API_BASE_URL + "/users/"; + +export const report = ref([]); +export const reports = ref([]); + +// Funzione per recuperare l'id delle segnalazioni +function recuperaId(self) { + return self.substring(self.lastIndexOf("/") + 1); +} + +// Funzione per formattare la data delle segnalazioni +function formattaData(date) { + return new Date(date).toLocaleString("it-IT", { + year: "numeric", + month: "long", + day: "numeric", + hour: "2-digit", + minute: "2-digit", + }); +} + +export function resetReports() { + report.value = []; + reports.value = []; +} + +export async function nReports(role) { + try { + const response = await fetch(apiReports, { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${loggedUser.token}`, + }, + }); + + const data = await response.json(); + return data.filter((user) => user.role === role).length; + } catch (error) { + console.error("Errore da nReports(): ", error); + return 0; + } +} + +export async function getReports() { + try { + const response = await fetch(apiReports, { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${loggedUser.token}`, + }, + }); + const data = await response.json(); + + // Wait for all emails to resolve + const reportsData = await Promise.all(data.map(async (item) => { + const email = await getEmailByUserId(item.createdBy); + return { + ...item, + email: email, + startDate: formattaData(item.startDate), + createdAt: formattaData(item.createdAt), + id: recuperaId(item.self), + }; + })); + + reports.value = reportsData; + } catch (error) { + console.error("Errore da getReports(): ", error); + reports.value = null; + } +} + +export async function getMyReports() { + try { + const response = await fetch(apiReports + "myReports", { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${loggedUser.token}`, + }, + }); + + const data = await response.json(); + const reportsData = data.map((item) => ({ + ...item, + startDate: formattaData(item.startDate), + createdAt: formattaData(item.createdAt), + id: recuperaId(item.self), + })); + reports.value = reportsData; + } catch (error) { + console.error("Errore da getMyReports(): ", error); + reports.value = null; + } +} + + +export async function getReportById(id) { + fetch(apiReports + id, { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${loggedUser.token}`, + }, + }) + .then((response) => response.json()) + .then((data) => { + emergency.value = { + ...data, + email: getEmailByUserId(data.createdBy), + startDate: formattaData(data.startDate), + createdAt: formattaData(data.createdAt), + id: recuperaId(data.self), + }; + }) + .catch((error) => { + console.error("Errore da getReportById(" + id + "): ", error); + emergency.value = null; + }); +} + +export async function getEmailByUserId(id) { + try { + const response = await fetch(apiUsers + id, { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${loggedUser.token}`, + }, + }); + + const data = await response.json(); + return data.email; + } catch (error) { + console.error("Errore da getEmailByUserId(" + id + "): ", error); + return null; + } +} + +export async function createReport(data) { + try { + const response = await fetch(apiReports, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${loggedUser.token}`, + }, + body: JSON.stringify(data), + }); + + if (!response.ok) { + throw new Error("Errore nella creazione del report"); + } + + if (response.status === 204 || response.headers.get("content-length") === "0") { + return null; + } + + return response.json(); + } catch (error) { + console.error("Errore da createReport(): ", error); + return null; + } +} diff --git a/frontend/src/data/users.js b/frontend/src/data/users.js index ae3327c..edb62c1 100644 --- a/frontend/src/data/users.js +++ b/frontend/src/data/users.js @@ -22,6 +22,11 @@ function formattaData(date) { }); } +export function resetUser() { + user.value = []; + users.value = []; +} + export async function nUsers(role) { try { const response = await fetch(apiUsers, { diff --git a/frontend/src/router/index.js b/frontend/src/router/index.js index dd98a23..e0e9ee6 100644 --- a/frontend/src/router/index.js +++ b/frontend/src/router/index.js @@ -11,47 +11,47 @@ const router = createRouter({ { path: '/storico', name: 'storico', - component: () => import('../views/ViewStorico.vue'), + component: () => import('../views/emergencies/ViewStorico.vue'), }, { path: '/dettagli', name: 'dettagli', - component: () => import('../views/ViewDettagli.vue'), + component: () => import('../views/emergencies/ViewDettagli.vue'), }, { path: '/accedi', name: 'accedi', - component: () => import('../views/ViewAccedi.vue'), + component: () => import('../views/user/ViewAccedi.vue'), }, { path: '/registrati', name: 'registrati', - component: () => import('../views/ViewRegistrati.vue'), + component: () => import('../views/user/ViewRegistrati.vue'), }, { path: '/profilo', name: 'profilo', - component: () => import('../views/ViewProfilo.vue'), + component: () => import('../views/user/ViewProfilo.vue'), }, { path: '/dashboard', name: 'dashboard', - component: () => import('../views/ViewDashboard.vue'), + component: () => import('../views/dashboard/ViewDashboard.vue'), }, { path: '/invia_segnalazione', name: 'invia_segnalazione', - component: () => import('../views/ViewSegnalazione.vue'), + component: () => import('../views/user/ViewSegnalazione.vue'), }, { path: '/pubblica_comunicazione', name: 'pubblica_comunicazione', - component: () => import('../views/ViewComunicazione.vue'), + component: () => import('../views/dashboard/ViewComunicazione.vue'), }, { path: '/modifica_comunicazione', name: 'modifica_comunicazione', - component: () => import('../views/ViewEditComunicazione.vue'), + component: () => import('../views/dashboard/ViewEditComunicazione.vue'), }, { path: '/:pathMatch(.*)*', diff --git a/frontend/src/states/loggedUser.js b/frontend/src/states/loggedUser.js index 038240a..daf6b8b 100644 --- a/frontend/src/states/loggedUser.js +++ b/frontend/src/states/loggedUser.js @@ -1,5 +1,8 @@ import { reactive, watch } from 'vue'; import Cookies from 'js-cookie'; +import { resetUser } from '@/data/users'; +import { resetEmergencies } from '@/data/emergencies'; +import { resetReports } from '@/data/reports'; const loggedUser = reactive({ self: undefined, @@ -47,6 +50,11 @@ function clearLoggedUser() { // Remove from cookies Cookies.remove('loggedUser'); + + // Reset all data states + resetUser() + resetEmergencies() + resetReports() } export { loggedUser, setLoggedUser, clearLoggedUser, loadUserFromCookies }; diff --git a/frontend/src/views/ViewHome.vue b/frontend/src/views/ViewHome.vue index c024426..672c642 100644 --- a/frontend/src/views/ViewHome.vue +++ b/frontend/src/views/ViewHome.vue @@ -1,11 +1,10 @@ - - -
|---|