From 67534d343b6e6dcd37d6816d4a011d40409be953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20de=20Metz?= Date: Wed, 22 May 2024 09:41:22 +0200 Subject: [PATCH] Rewrite news to a composable. --- js/news.js | 50 +++++++++++++++++------------------------------- js/news_info.vue | 17 +++++++--------- js/sidebar.vue | 9 ++++++--- js/toolbar.vue | 9 ++++++--- 4 files changed, 37 insertions(+), 48 deletions(-) diff --git a/js/news.js b/js/news.js index 9901e71..356e81a 100644 --- a/js/news.js +++ b/js/news.js @@ -1,4 +1,4 @@ -import { reactive } from 'vue'; +import { ref } from 'vue'; import newsFiles from '../news/*.md'; const news = Object.values(newsFiles) @@ -11,37 +11,23 @@ const news = Object.values(newsFiles) const LAST_READ_NEWS_AT_LOCAL_STORAGE = 'lastReadNewsAt'; -const state = reactive({ - hasNews: true, -}); - -export default { - data() { - return { - news, - internalState: state, - }; - }, +function initHasUnreadNews() { + const lastReadNewsAt = localStorage.getItem(LAST_READ_NEWS_AT_LOCAL_STORAGE); + if (lastReadNewsAt) { + const date = new Date(); + date.setTime(lastReadNewsAt); + return news[0].date > date; + } + return true; +} - created() { - const lastReadNewsAt = localStorage.getItem(LAST_READ_NEWS_AT_LOCAL_STORAGE); - if (lastReadNewsAt) { - const date = new Date(); - date.setTime(lastReadNewsAt); - state.hasNews = news[0].date > date; - } - }, +const hasUnreadNews = ref(initHasUnreadNews()); - computed: { - hasNews() { - return this.internalState.hasNews; - } - }, +function markAsRead() { + localStorage.setItem(LAST_READ_NEWS_AT_LOCAL_STORAGE, news[0].date.getTime()); + hasUnreadNews.value = false; +} - methods: { - markAsRead() { - localStorage.setItem(LAST_READ_NEWS_AT_LOCAL_STORAGE, news[0].date.getTime()); - state.hasNews = false; - } - } -}; +export function useNews() { + return { hasUnreadNews, news, markAsRead }; +} diff --git a/js/news_info.vue b/js/news_info.vue index 745a435..68488ae 100644 --- a/js/news_info.vue +++ b/js/news_info.vue @@ -21,17 +21,14 @@ - diff --git a/js/sidebar.vue b/js/sidebar.vue index fde0f36..9308a86 100644 --- a/js/sidebar.vue +++ b/js/sidebar.vue @@ -60,7 +60,7 @@