Skip to content

Commit

Permalink
Refactor OnlineStatusIndicator to use custom hook useOnlineStatus.
Browse files Browse the repository at this point in the history
  • Loading branch information
swuecho committed Sep 30, 2024
1 parent a01badf commit 7697a01
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
16 changes: 2 additions & 14 deletions web/src/components/OnlineStatusIndicator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,11 @@
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
const isOnline = ref(navigator.onLine);
import { useOnlineStatus } from '../hooks/useOnlineStatus';
const updateOnlineStatus = () => {
isOnline.value = navigator.onLine;
};
const { isOnline } = useOnlineStatus();import { ref, onMounted, onUnmounted } from 'vue';
onMounted(() => {
window.addEventListener('online', updateOnlineStatus);
window.addEventListener('offline', updateOnlineStatus);
});
onUnmounted(() => {
window.removeEventListener('online', updateOnlineStatus);
window.removeEventListener('offline', updateOnlineStatus);
});
</script>

<style scoped>
Expand Down
22 changes: 22 additions & 0 deletions web/src/hooks/useOnlineStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// src/composables/useOnlineStatus.js
import { ref, onMounted, onUnmounted } from 'vue';

export function useOnlineStatus() {
const isOnline = ref(navigator.onLine);

const updateOnlineStatus = () => {
isOnline.value = navigator.onLine;
};

onMounted(() => {
window.addEventListener('online', updateOnlineStatus);
window.addEventListener('offline', updateOnlineStatus);
});

onUnmounted(() => {
window.removeEventListener('online', updateOnlineStatus);
window.removeEventListener('offline', updateOnlineStatus);
});

return { isOnline };
}

0 comments on commit 7697a01

Please sign in to comment.