From 75e8ae6ebd9df23275fb14eea88da0b56d006313 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 9 Jun 2021 17:37:43 +0200 Subject: Frontend has proper error handling & api fetching is in it's own file --- .../client/src/components/BaseErrorMessage.vue | 27 ++++++ .../client/src/components/RepositoryTreeTree.vue | 2 +- packages/client/src/util/fetch.js | 35 ++++++++ packages/client/src/views/Home.vue | 29 +++++-- packages/client/src/views/RepositoryCommit.vue | 96 ++++++++++++---------- packages/client/src/views/RepositoryLog.vue | 32 +++++--- packages/client/src/views/RepositoryTree.vue | 49 +++++------ packages/server/package.json | 2 +- 8 files changed, 186 insertions(+), 86 deletions(-) create mode 100644 packages/client/src/components/BaseErrorMessage.vue create mode 100644 packages/client/src/util/fetch.js diff --git a/packages/client/src/components/BaseErrorMessage.vue b/packages/client/src/components/BaseErrorMessage.vue new file mode 100644 index 0000000..7f193ce --- /dev/null +++ b/packages/client/src/components/BaseErrorMessage.vue @@ -0,0 +1,27 @@ + + + + + diff --git a/packages/client/src/components/RepositoryTreeTree.vue b/packages/client/src/components/RepositoryTreeTree.vue index 89ffb81..376cafc 100644 --- a/packages/client/src/components/RepositoryTreeTree.vue +++ b/packages/client/src/components/RepositoryTreeTree.vue @@ -38,7 +38,7 @@ - {{ getPrettyLastUpdated(entry.last_commit.time) }} + {{ getPrettyLastUpdated(entry.last_commit.date) }} diff --git a/packages/client/src/util/fetch.js b/packages/client/src/util/fetch.js new file mode 100644 index 0000000..a6a43b0 --- /dev/null +++ b/packages/client/src/util/fetch.js @@ -0,0 +1,35 @@ +export default async function(endpoint, fetch_failed, is_loading, data_name) { + const fetch_timeout = setTimeout(() => { + if(!fetch_failed.value) { + fetch_failed.value = `Failed to fetch ${data_name} data.`; + is_loading.value = false; + } + }, 5000); + + const data_req = await fetch(`${window.location.protocol}//${window.location.host}/api/v1/${endpoint}`).catch(() => { + if(!fetch_failed.value) { + fetch_failed.value = `Failed to fetch ${data_name} data.`; + is_loading.value = false; + clearTimeout(fetch_timeout); + } + return null; + }); + + if(data_req !== null) { + const data = await data_req.json().catch(() => { + fetch_failed.value = "Failed to parse server response."; + }); + + if(data_req.ok) { + clearTimeout(fetch_timeout); + is_loading.value = false; + return data.data; + } else { + fetch_failed.value = `Failed to fetch ${data_name} data.`; + } + } + + clearTimeout(fetch_timeout); + is_loading.value = false; + return null; +}; diff --git a/packages/client/src/views/Home.vue b/packages/client/src/views/Home.vue index 305d42b..397a3a1 100644 --- a/packages/client/src/views/Home.vue +++ b/packages/client/src/views/Home.vue @@ -3,8 +3,8 @@
-
-
    +
    +
    • @@ -17,6 +17,11 @@
    + +
@@ -25,26 +30,33 @@