Skip to content

Commit

Permalink
Rewrite news to a composable.
Browse files Browse the repository at this point in the history
  • Loading branch information
francois2metz committed May 22, 2024
1 parent c84af32 commit 67534d3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 48 deletions.
50 changes: 18 additions & 32 deletions js/news.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reactive } from 'vue';
import { ref } from 'vue';
import newsFiles from '../news/*.md';

const news = Object.values(newsFiles)
Expand All @@ -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 };
}
17 changes: 7 additions & 10 deletions js/news_info.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@
</sub-sidebar>
</template>

<script>
import news from './news';
<script setup>
import { onMounted } from 'vue';
import { useNews } from './news';
import SubSidebar from './sub_sidebar';
export default {
components: { SubSidebar },
const { news, markAsRead } = useNews();
mixins: [news],
mounted() {
this.markAsRead();
}
};
onMounted(() => {
markAsRead();
});
</script>
9 changes: 6 additions & 3 deletions js/sidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<v-list-item @click="display('news')">
<template v-slot:prepend>
<v-badge
:model-value="hasNews"
:model-value="hasUnreadNews"
dot
>
<v-icon>{{ mdiNewspaperVariantOutline }}</v-icon>
Expand Down Expand Up @@ -149,7 +149,7 @@ import {
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import { tilesUrl } from '../config.json';
import news from './news';
import { useNews } from './news';
import logo from 'data-url:../icons/indoorequal.svg';

dayjs.extend(relativeTime);
Expand All @@ -168,7 +168,10 @@ const fetchReplicationStatus = async function() {
}

export default {
mixins: [news],
setup() {
const { hasUnreadNews } = useNews();
return { hasUnreadNews};
},

props: {
menu: {
Expand Down
9 changes: 6 additions & 3 deletions js/toolbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
v-bind="props"
>
<v-badge
:model-value="hasNews"
:model-value="hasUnreadNews"
dot
>
<img
Expand Down Expand Up @@ -71,15 +71,18 @@
<script>
import { mdiPencilOutline } from '@mdi/js';
import GeocoderInput from './geocoder';
import news from './news';
import { useNews } from './news';
import equalLogo from 'data-url:../icons/equal.svg';
export default {
components: {
GeocoderInput
},
mixins: [news],
setup() {
const { hasUnreadNews } = useNews();
return { hasUnreadNews};
},
props: {
mapBounds: {
Expand Down

0 comments on commit 67534d3

Please sign in to comment.