Skip to content

Commit

Permalink
Fix better typing for reports store
Browse files Browse the repository at this point in the history
  • Loading branch information
rien committed Jan 11, 2024
1 parent 5552858 commit 6be6444
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
22 changes: 15 additions & 7 deletions web/src/components/upload/UploadFormCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,21 @@ const acceptRules = [
const uploadProgress = shallowRef(25);
// Report
const reportActiveId = shallowRef<string>();
const reportActive = computed(() =>
reports.getReportById(reportActiveId.value)
);
const reportRoute = computed(() =>
reports.getReportRouteById(reportActiveId.value)
);
const reportActiveId = shallowRef<string | undefined>();
const reportActive = computed(() => {
if (reportActiveId.value) {
return reports.getReportById(reportActiveId.value);
} else {
return undefined;
}
});
const reportRoute = computed(() => {
if (reportActiveId.value) {
return reports.getReportRouteById(reportActiveId.value);
} else {
return undefined;
}
});
// Clear the form.
const clearForm = (): void => {
Expand Down
12 changes: 8 additions & 4 deletions web/src/components/upload/UploadsTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ const items = computed(() =>
}))
);
const selectedReportId = ref<string>();
const selectedReport = computed(() =>
reports.getReportById(selectedReportId.value)
);
const selectedReportId = ref<string | undefined>();
const selectedReport = computed(() => {
if (selectedReportId.value) {
return reports.getReportById(selectedReportId.value)
} else {
return undefined;
}
});
const infoDialog = ref(false);
const deleteDialog = ref(false);
Expand Down
6 changes: 5 additions & 1 deletion web/src/composables/useAppMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ export function useAppMode() {
// URL to the report.
const reportUrl = computed(() => {
if (import.meta.env.VITE_MODE === "server") {
return reports.getReportUrlById(reports.currentReport?.reportId);
if (reports.currentReport) {
return reports.getReportUrlById(reports.currentReport.reportId);
} else {
return undefined;
}
} else {
return DATA_URL;
}
Expand Down
28 changes: 16 additions & 12 deletions web/src/stores/reports.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ export const useReportsStore = defineStore("reports", () => {
}

// Get a report in the list by reportId.
function getReportById(reportId: string | undefined) {
function getReportById(reportId: string) {
return reports.value.find((r) => r.reportId === reportId);
}

// Get a report in the list by referenceId.
function getReportByReferenceId(referenceId: string | undefined) {
function getReportByReferenceId(referenceId: string) {
return reports.value.find((r) => r.referenceId === referenceId);
}

// Get the route for a given report id.
function getReportRouteById(reportId: string | undefined) {
function getReportRouteById(reportId: string) {
const report = getReportById(reportId);

// If no report is found, return the home page.
Expand All @@ -64,31 +64,29 @@ export const useReportsStore = defineStore("reports", () => {
return {
name: "Overview",
params: {
referenceId: report?.referenceId,
referenceId: report.referenceId,
},
};
}

// Get the share route for a given report id.
function getReportShareRouteById(reportId: string | undefined) {
function getReportShareRouteById(reportId: string) {
return {
name: "Share",
params: {
reportId: reportId ?? "",
reportId: reportId,
},
};
}

// Delete a report by reportId.
function deleteReportById(reportId: string | undefined) {
function deleteReportById(reportId: string) {
reports.value = reports.value.filter((r) => r.reportId !== reportId);
}

// Get the URL for a given report id.
const getReportUrlById = (reportId: string | undefined) => {
if (reportId) {
return `${import.meta.env.VITE_API_URL}/reports/${reportId}`;
}
const getReportUrlById = (reportId: string) => {
return `${import.meta.env.VITE_API_URL}/reports/${reportId}`;
};

async function checkReports() {
Expand Down Expand Up @@ -119,8 +117,14 @@ export const useReportsStore = defineStore("reports", () => {
const route = useRoute();
const currentReport = computed(() => {
const reportId = route.params.reportId as string | undefined;
if (reportId) {
return getReportById(reportId);
}
const referenceId = route.params.referenceId as string | undefined;
return getReportById(reportId) ?? getReportByReferenceId(referenceId);
if (referenceId) {
return getReportByReferenceId(referenceId);
}
return undefined;
});

return {
Expand Down

0 comments on commit 6be6444

Please sign in to comment.