From 678be4498d11755a53d2b752a5dc8b02c4f0ce00 Mon Sep 17 00:00:00 2001 From: Brock Anderson Date: Fri, 27 Sep 2024 08:55:23 -0700 Subject: [PATCH] reduce code duplication --- .../dashboard/RecentlySubmittedReports.vue | 19 +------------------ .../dashboard/RecentlyViewedReports.vue | 19 +------------------ .../components/dashboard/ReportsWidget.vue | 18 +++++++++++++++++- 3 files changed, 19 insertions(+), 37 deletions(-) diff --git a/admin-frontend/src/components/dashboard/RecentlySubmittedReports.vue b/admin-frontend/src/components/dashboard/RecentlySubmittedReports.vue index f1d0a30f..9f2396d9 100644 --- a/admin-frontend/src/components/dashboard/RecentlySubmittedReports.vue +++ b/admin-frontend/src/components/dashboard/RecentlySubmittedReports.vue @@ -26,11 +26,7 @@ import { import ReportsWidget from './ReportsWidget.vue'; import ApiService from '../../services/apiService'; import { formatIsoDateTimeAsLocalDate } from '../../utils/date'; -import { ref, onMounted, onUnmounted } from 'vue'; -import { - ReportChangeService, - ReportChangedEventPayload, -} from '../../services/reportChangeService'; +import { ref } from 'vue'; const pageSize = 5; const reportsWidget = ref(); @@ -60,19 +56,6 @@ const headers = [ }, ]; -onMounted(() => { - ReportChangeService.listen(onAnyReportChanged); -}); - -onUnmounted(() => { - ReportChangeService.unlisten(onAnyReportChanged); -}); - -function onAnyReportChanged(payload: ReportChangedEventPayload) { - console.log(`recentlySubmittedReports - onAnyReportChanged`); - refresh(); -} - async function getRecentlySubmittedReports(): Promise { const filter: ReportFilterType = []; const sort: IReportSearchSort = [{ update_date: 'desc' }]; diff --git a/admin-frontend/src/components/dashboard/RecentlyViewedReports.vue b/admin-frontend/src/components/dashboard/RecentlyViewedReports.vue index 874bc01a..091ec5c4 100644 --- a/admin-frontend/src/components/dashboard/RecentlyViewedReports.vue +++ b/admin-frontend/src/components/dashboard/RecentlyViewedReports.vue @@ -26,11 +26,7 @@ import { import ReportsWidget from './ReportsWidget.vue'; import ApiService from '../../services/apiService'; import { formatIsoDateTimeAsLocalDate } from '../../utils/date'; -import { ref, onMounted, onUnmounted } from 'vue'; -import { - ReportChangeService, - ReportChangedEventPayload, -} from '../../services/reportChangeService'; +import { ref } from 'vue'; const pageSize = 5; const reportsWidget = ref(); @@ -56,19 +52,6 @@ const headers = [ }, ]; -onMounted(() => { - ReportChangeService.listen(onAnyReportChanged); -}); - -onUnmounted(() => { - ReportChangeService.unlisten(onAnyReportChanged); -}); - -function onAnyReportChanged(payload: ReportChangedEventPayload) { - console.log(`recentlyViewedReports - onAnyReportChanged`); - refresh(); -} - async function refresh() { await reportsWidget.value?.refresh(); } diff --git a/admin-frontend/src/components/dashboard/ReportsWidget.vue b/admin-frontend/src/components/dashboard/ReportsWidget.vue index b8745c3a..c294fdf8 100644 --- a/admin-frontend/src/components/dashboard/ReportsWidget.vue +++ b/admin-frontend/src/components/dashboard/ReportsWidget.vue @@ -53,8 +53,12 @@ export default { */ import { Report } from '../../types/reports'; -import { ref } from 'vue'; +import { ref, onMounted, onUnmounted } from 'vue'; import ReportActions from '../reports/ReportActions.vue'; +import { + ReportChangeService, + ReportChangedEventPayload, +} from '../../services/reportChangeService'; const props = defineProps<{ pageSize: string | number | undefined; @@ -66,6 +70,18 @@ const isSearching = ref(false); const hasSearched = ref(false); const reports = ref(); +onMounted(() => { + ReportChangeService.listen(onAnyReportChanged); +}); + +onUnmounted(() => { + ReportChangeService.unlisten(onAnyReportChanged); +}); + +function onAnyReportChanged(payload: ReportChangedEventPayload) { + refresh(); +} + /** * Refresh the reports */