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 @@