fixed reports list sorting

This commit is contained in:
Mason Coloretti 2024-07-12 21:39:52 -06:00
parent 407468ff14
commit fa1fa87391

View file

@ -1,60 +1,61 @@
<template> <template>
<div> <div>
<h1>Incident Reports</h1> <h1>Incident Reports</h1>
<div v-if="isLoading">Loading...</div> <div v-if="isLoading">Loading...</div>
<div v-else-if="incidentReports.length"> <div v-else-if="filteredReports.length">
<div v-for="report in incidentReports" :key="report._id"> <div v-for="report in filteredReports" :key="report._id" class="big-padding">
<h3>{{ formatDate(report._createdDate) }}</h3> <h3>{{ formatDate(report._createdDate) }}</h3>
<hr> <hr>
<h4>{{ report._title }}</h4> <h4>{{ report._title }}</h4>
<p>{{ report._content }}</p> <p>{{ report._content }}</p>
<hr><br><br> <hr><br><br>
</div> </div>
</div>
<p v-else>No incident reports found or an error occurred.</p>
</div> </div>
<p v-else>No incident reports found or an error occurred.</p>
</div>
</template> </template>
<script> <script>
export default { export default {
data() { data() {
return { return {
incidentReports: [], incidentReports: [],
isLoading: false, isLoading: false,
error: null, error: null,
}; };
},
mounted() {
this.fetchIncidentReports();
},
methods: {
async fetchIncidentReports() {
this.isLoading = true;
try {
const response = await fetch("/api/incident-reports"); // Replace with your API endpoint
const data = await response.json();
this.incidentReports = data;
} catch (error) {
this.error = error;
console.error("Error fetching incident reports:", error);
} finally {
this.isLoading = false;
}
}, },
mounted() { formatDate(dateString) {
this.fetchIncidentReports(); const date = new Date(dateString);
return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
}, },
methods: { },
async fetchIncidentReports() { computed: {
this.isLoading = true; filteredReports() {
try { return this.incidentReports
const response = await fetch("/api/incident-reports"); // Replace with your API endpoint .slice() // Create a copy to avoid mutating the original array
const data = await response.json(); .sort((a, b) => new Date(b._createdDate) - new Date(a._createdDate))
this.incidentReports = data; .slice(-25); // Get the last 25 sorted reports
console.log(data);
} catch (error) {
this.error = error;
console.error("Error fetching incident reports:", error);
} finally {
this.isLoading = false;
this.filteredReports = this.incidentReports.slice(-25); // Get the last 25 reports
}
},
formatDate(dateString) { const date = new Date(dateString); return date.toLocaleDateString('en-US', { year: 'numeric',month: 'long',day: 'numeric' });
},
},
computed: {
filteredReports() {
// You can implement additional filtering/sorting logic here
return this.incidentReports.slice(-25);
},
}, },
},
}; };
</script> </script>
<style> <style>