Skip to content
This repository has been archived by the owner on Mar 14, 2021. It is now read-only.

PART 3: Make usage of Suspense #fallback #3

Open
wants to merge 3 commits into
base: part2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<template>
<router-view v-slot="{ Component }">
<Suspense>
<Suspense timeout="0">
<template #default>
<component :is="Component" />
</template>
<template #fallback>
<Loading />
</template>
</Suspense>
</router-view>
</template>

<script>
import Loading from './components/Loading.vue';

export default {
name: 'App',
components: { Loading },
}
</script>
11 changes: 3 additions & 8 deletions src/pages/[city].vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<template>
<loading v-if="isLoading" />
<success
v-else-if="isSuccess"
v-if="isSuccess"
:city="weatherData.city"
:weather="weatherData.weather"
:temperature="weatherData.temperature"
Expand All @@ -14,14 +13,12 @@ import { ref, computed } from 'vue';

// Components
import Success from '../components/Success.vue';
import Loading from '../components/Loading.vue';
import Error from '../components/Error.vue';

// Services
import { getCurrentWeather } from '../services/openWeather.js';

const STATES = {
LOADING: 'LOADING',
SUCCESS: 'SUCCESS',
ERROR: 'ERRROR',
};
Expand All @@ -30,7 +27,6 @@ export default {
name: 'City',
components: {
Success,
Loading,
Error,
},
props: {
Expand All @@ -40,10 +36,9 @@ export default {
},
},
async setup(props) {
const state = ref(STATES.LOADING);
const state = ref(null);
const weatherData = ref({});

const isLoading = computed(() => state.value === STATES.LOADING);
const isSuccess = computed(() => state.value === STATES.SUCCESS);
const isError = computed(() => state.value === STATES.ERROR);

Expand All @@ -62,7 +57,7 @@ export default {
state.value = STATES.ERROR;
}

return { state, weatherData, isLoading, isSuccess, isError };
return { state, weatherData, isSuccess, isError };
},
};
</script>
9 changes: 8 additions & 1 deletion src/services/openWeather.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ const WEATHER_TYPE_EMOJI_MAP = {
TORNADO: '🌪',
};

function asyncSetTimeout() {
return new Promise(resolve => setTimeout(resolve, 2000));
}

export async function getCurrentWeather(city) {
const path = '/weather';
const response = await fetch(`${BASE_URL}${path}?q=${city}&appid=${API_KEY}&units=metric`);
const [ response ] = await Promise.all([
fetch(`${BASE_URL}${path}?q=${city}&appid=${API_KEY}&units=metric`),
asyncSetTimeout(),
]);

return response.json();
}
Expand Down