Skip to content

Commit

Permalink
chain promise fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Sudarshan-21 committed Apr 15, 2024
1 parent 33675e8 commit f324b3c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 48 deletions.
Binary file added zimui/src/assets/Error.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 21 additions & 7 deletions zimui/src/components/TopicHome.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import TopicSectionType from '@/types/TopicSection'
import { transformTopicSectionOrSubSectionToCardData } from '@/types/TopicCardData'
import ToolBar from '../components/ToolBar.vue'
import { useLoading } from 'vue-loading-overlay'
import errimageData from '../assets/Error.jpg'
const errimage = ref(errimageData)
const main = useMainStore()
const props = defineProps({
Expand Down Expand Up @@ -39,7 +41,7 @@ const fetchData = async function () {
topic.value = resp
}
} catch (error) {
//console.error('Error fetching topic:', error)
main.setErrorMessage('An error occured, Please try again.')
}finally {
loader.hide()
dataLoaded.value = true
Expand Down Expand Up @@ -91,9 +93,10 @@ const goToPreviousPage = () => {
</script>

<template>
<div v-if="main.errorMessage" class="error-message" >
Error: {{ main.errorMessage }}
</div>
<div v-if="main.errorMessage" class="error-container">
<img :src="errimage" class="error-image" />
<p class="error-text">{{ main.errorMessage }}</p>
</div>
<div v-else-if="topic" class="content">
<nav
class="navbar navbar px-0 channel-navbar navbar-light fixed-top navbar-expand shadow"
Expand Down Expand Up @@ -279,9 +282,20 @@ footer {
text-align: center;
}
.error-message {
color: red;
margin-top: 10px;
.error-container {
text-align: center;
padding: 20px;
}
.error-image {
width: 300px;
margin-bottom: 20px;
}
.error-text {
color: black; /* Black text color */
font-size: 16px;
}
</style>
34 changes: 23 additions & 11 deletions zimui/src/pages/HomePage.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<script setup lang="ts">
import { onMounted, toRef, Ref, ref, watch} from 'vue'
import { RouteParams, useRoute } from 'vue-router'
import errimageData from '../assets/Error.jpg'
const errimage = ref(errimageData)
import { useMainStore } from '../stores/main'
const main = useMainStore()
Expand All @@ -25,29 +28,38 @@ onMounted(async () => {
topic.value = main.channelData.rootSlug
}
} catch (error) {
//console.error('Error fetching channel data:', error)
// Handle error here, for example, show an error message to the user
main.setErrorMessage('An error occured, Please try again.')
}
})
import TopicHome from '../components/TopicHome.vue'
</script>

<template>
<div v-if="main.errorMessage" class="error-message">
{{ main.errorMessage }}
</div>

<div v-else class="d-flex flex-column h-100">
<div v-if="main.errorMessage" class="error-container">
<img :src="errimage" class="error-image" />
<p class="error-text">{{ main.errorMessage }}</p>
</div>
<div v-else class="d-flex flex-column h-100">
<div class="flex-fill flex-shrink-0">
<TopicHome v-if="main.channelData" :slug="topic" />
</div>
</div>
</div>
</template>

<style scoped>
.error-message {
color: red;
margin-top: 10px;
.error-container {
text-align: center;
padding: 20px;
}
.error-image {
width: 300px;
margin-bottom: 20px;
}
.error-text {
color: black; /* Black text color */
font-size: 16px;
}
</style>
62 changes: 32 additions & 30 deletions zimui/src/stores/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,57 @@ export type RootState = {
channelData: Channel | null
isLoading: boolean
errorMessage: string
error: AxiosError<object> | null
}
export const useMainStore = defineStore('main', {
state: () =>
({
channelData: null,
isLoading: false,
errorMessage: '',
error: null,
}) as RootState,
getters: {},
actions: {
async fetchChannel() {
this.isLoading = true
this.errorMessage = ''
try {
const response = await axios.get('./channel.json')
this.isLoading = false
this.channelData = response.data as Channel
} catch (error) {
if (error instanceof AxiosError) {
this.isLoading = false
this.channelData = null
this.handleAxiosError(error);
}

return axios
.get('./channel.json')
.then((response) => {
this.isLoading = false;
this.channelData = response.data as Channel;
return this.channelData; // Return the fetched channel data
})
.catch((error) => {
this.isLoading = false
this.channelData = null
if (error instanceof AxiosError) {
this.handleAxiosError(error);
}
else {
this.errorMessage = 'Failed to load channel data';
}
this.error = error as AxiosError<object> // Set axios error to store
return null
}
});
},
async fetchTopic(slug: string) {
this.isLoading = true
this.errorMessage = ''
try {
const response = await axios.get('./topics/' + slug + '.json')
this.isLoading = false
return response.data as Topic
} catch (error: unknown) {
if (error instanceof AxiosError) {
this.isLoading = false
this.channelData = null
this.handleAxiosError(error);
}
else{
return axios
.get('./topics/' + slug + '.json')
.then((response) => {
this.isLoading = false;
return response.data as Topic;
})
.catch((error) => {
this.isLoading = false
this.channelData = null
if (error instanceof AxiosError) {
this.handleAxiosError(error);
}
else {
this.errorMessage = 'Failed to load node ' + slug + ' data';
}
this.error = error as AxiosError<object> // Set axios error to store
return null
}
});
},
handleAxiosError(error: AxiosError<object>) {
this.isLoading = false;
Expand All @@ -82,8 +82,10 @@ export const useMainStore = defineStore('main', {
} else {
this.errorMessage = 'An Unexpected error occured';
}
this.error = error;
}
},
setErrorMessage(message: string) {
this.errorMessage = message
},
},
})

0 comments on commit f324b3c

Please sign in to comment.