Skip to content

Commit

Permalink
feat(error): add toast for if connection fails
Browse files Browse the repository at this point in the history
  • Loading branch information
brucetony committed Jul 29, 2024
1 parent ddcae7f commit 2c15e89
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 44 deletions.
20 changes: 14 additions & 6 deletions components/analysis/AnalysesTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getAnalyses } from "~/composables/useAPIFetch";
import { formatDataRow } from "~/utils/format-data-row";
import TableRowMetadata from "~/components/TableRowMetadata.vue";
import ExpandRowButtons from "~/components/table/ExpandRowButtons.vue";
import { showConnectionErrorToast } from "~/composables/connectionErrorToast";
const analyses = ref();
const expandedRows = ref();
Expand All @@ -19,12 +20,19 @@ const expandRowEntries = [
onMounted(() => {
nextTick(async () => {
const { data: response } = await getAnalyses();
analyses.value = formatDataRow(
response.value!.data as unknown as Map<string, string | number | null>[],
["created_at", "updated_at"],
expandRowEntries,
);
const { data: response, status, error } = await getAnalyses();
if (status.value === "success") {
analyses.value = formatDataRow(
response.value!.data as unknown as Map<
string,
string | number | null
>[],
["created_at", "updated_at"],
expandRowEntries,
);
} else if (error.value?.statusCode === 500) {
showConnectionErrorToast();
}
loading.value = false;
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
} from "~/services/Api";
import DetailedDataStoreTable from "~/components/data-stores/tables/DetailedDataStoreTable.vue";
import DetailedAnalysisTable from "~/components/data-stores/tables/DetailedAnalysisTable.vue";
import { showConnectionErrorToast } from "~/composables/connectionErrorToast";
const dataStores = ref();
const consumers = ref();
Expand All @@ -38,27 +39,31 @@ onBeforeMount(() => {
});
async function loadDetailedDataStoreTable() {
const { data: response } = await getDataStores(true);
const { data: response, status, error } = await getDataStores(true);
let formattedDataStores = formatDataRow(
response.value!.data,
dataRowUnixCols,
expandRowEntries,
) as DetailedService[];
if (status.value === "success") {
let formattedDataStores = formatDataRow(
response.value!.data,
dataRowUnixCols,
expandRowEntries,
) as DetailedService[];
formattedDataStores.forEach((store: DetailedService) => {
if (store.routes!.length) {
store.routes = formatDataRow(
store.routes,
dataRowUnixCols,
expandRowEntries,
);
store.routes?.forEach((proj: Route) => {
proj["projectId"] = extractProjectIdFromPath(proj.paths! as string[]);
});
}
});
dataStores.value = formattedDataStores;
formattedDataStores.forEach((store: DetailedService) => {
if (store.routes!.length) {
store.routes = formatDataRow(
store.routes,
dataRowUnixCols,
expandRowEntries,
);
store.routes?.forEach((proj: Route) => {
proj["projectId"] = extractProjectIdFromPath(proj.paths! as string[]);
});
}
});
dataStores.value = formattedDataStores;
} else if (error.value?.statusCode === 500) {
showConnectionErrorToast();
}
}
async function fetchDataFromHub(route: string) {
Expand Down
3 changes: 2 additions & 1 deletion components/projects/ApproveRejectButtons.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script setup lang="ts">
import { approveRejectProjectProposal } from "~/composables/useAPIFetch";
import { useToastService } from "~/composables/connectionErrorToast";
const props = defineProps({
projectId: String,
});
const toast = useToast();
const toast = useToastService();
const emit = defineEmits(["updatedRow"]);
Expand Down
21 changes: 15 additions & 6 deletions components/projects/ProjectTable.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { getProjects } from "~/composables/useAPIFetch";
import { formatDataRow } from "~/utils/format-data-row";
import { showConnectionErrorToast } from "~/composables/connectionErrorToast";
const projects = ref();
const loading = ref(true);
Expand All @@ -10,12 +11,20 @@ const expandRowEntries = [];
onMounted(() => {
nextTick(async () => {
const { data: response } = await getProjects();
projects.value = formatDataRow(
response.value!.data as unknown as Map<string, string | number | null>[],
dataRowUnixCols,
expandRowEntries,
);
const { data: response, status, error } = await getProjects();
if (status.value === "success") {
projects.value = formatDataRow(
response.value!.data as unknown as Map<
string,
string | number | null
>[],
dataRowUnixCols,
expandRowEntries,
);
} else if (error.value?.statusCode === 500) {
showConnectionErrorToast();
}
loading.value = false;
});
});
Expand Down
20 changes: 14 additions & 6 deletions components/projects/ProposalTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { formatDataRow } from "~/utils/format-data-row";
import TableRowMetadata from "~/components/TableRowMetadata.vue";
import type { ProjectNode } from "~/services/Api";
import ExpandRowButtons from "~/components/table/ExpandRowButtons.vue";
import { showConnectionErrorToast } from "~/composables/connectionErrorToast";
const proposals = ref();
const expandedRows = ref({});
Expand All @@ -15,12 +16,19 @@ const expandRowEntries = ["project_id", "node_id"];
onMounted(() => {
nextTick(async () => {
const { data: response } = await getProposals();
proposals.value = formatDataRow(
response.value!.data as unknown as Map<string, string | number | null>[],
dataRowUnixCols,
expandRowEntries,
);
const { data: response, status, error } = await getProposals();
if (status.value === "success") {
proposals.value = formatDataRow(
response.value!.data as unknown as Map<
string,
string | number | null
>[],
dataRowUnixCols,
expandRowEntries,
);
} else if (error.value?.statusCode === 500) {
showConnectionErrorToast();
}
loading.value = false;
});
});
Expand Down
18 changes: 18 additions & 0 deletions composables/connectionErrorToast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useToast } from "primevue/usetoast";

export const useToastService = () => {
const nuxtApp = useNuxtApp();
const getToast: typeof useToast = () =>
nuxtApp.vueApp.config.globalProperties.$toast;
return getToast();
};

export const showConnectionErrorToast = () => {
const toast = useToastService();
toast.add({
severity: "error",
summary: "Connection error",
detail: "Unable to contact the API.",
life: 3000,
});
};
4 changes: 3 additions & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ export default defineNuxtConfig({
"primevue/resources/themes/lara-dark-amber/theme.css",
"primeicons/primeicons.css",
],
});

compatibilityDate: "2024-07-29",
});
2 changes: 1 addition & 1 deletion pages/data-stores/create.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import ResourceManagerLayout from "~/components/data-stores/ResourceManagerLayout.vue";
import ResourceManagerLayout from "~/components/data-stores/ResourceManagerTabs.vue";
definePageMeta({
middleware: ["auth"],
Expand Down
2 changes: 1 addition & 1 deletion pages/data-stores/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import DataStoreLayout from "~/components/data-stores/DataStoreListLayout.vue";
import DataStoreLayout from "~/components/data-stores/DataStoreListTabs.vue";
definePageMeta({
middleware: ["auth"],
Expand Down
4 changes: 1 addition & 3 deletions plugins/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export default defineNuxtPlugin((nuxtApp) => {
headers.set("Authorization", `Bearer ${user?.value.accessToken}`);
options.headers = headers;
},
onRequestError({ request, options, error }) {
console.log(request);
console.log(options);
onRequestError({ error }) {
console.log(error);
},
onResponse({ response }) {
Expand Down

0 comments on commit 2c15e89

Please sign in to comment.