chore: refactor notifications component to use ts and setup

This commit is contained in:
kolaente 2022-05-08 12:17:02 +02:00
parent 3e7f598ee8
commit 315da424ec
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B

View file

@ -46,60 +46,59 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import {defineComponent} from 'vue' import {computed, onMounted, onUnmounted, ref} from 'vue'
import NotificationService from '@/services/notification' import NotificationService from '@/services/notification'
import User from '@/components/misc/user.vue' import User from '@/components/misc/user.vue'
import names from '@/models/constants/notificationNames.json' import names from '@/models/constants/notificationNames.json'
import {closeWhenClickedOutside} from '@/helpers/closeWhenClickedOutside' import {closeWhenClickedOutside} from '@/helpers/closeWhenClickedOutside'
import {mapState} from 'vuex' import {useStore} from 'vuex'
import {useRouter} from 'vue-router'
const LOAD_NOTIFICATIONS_INTERVAL = 10000 const LOAD_NOTIFICATIONS_INTERVAL = 10000
export default defineComponent({ const store = useStore()
name: 'notifications', const router = useRouter()
components: {User},
data() { const allNotifications = ref([])
return { const showNotifications = ref(false)
notificationService: new NotificationService(), const popup = ref(null)
allNotifications: [],
showNotifications: false, const unreadNotifications = computed(() => {
interval: null, return notifications.value.filter(n => n.readAt === null).length
} })
}, const notifications = computed(() => {
mounted() { return allNotifications.value ? allNotifications.value.filter(n => n.name !== '') : []
this.loadNotifications() })
document.addEventListener('click', this.hidePopup) const userInfo = computed(() => store.state.auth.info)
this.interval = setInterval(this.loadNotifications, LOAD_NOTIFICATIONS_INTERVAL)
}, let interval: number
beforeUnmount() {
document.removeEventListener('click', this.hidePopup) onMounted(() => {
clearInterval(this.interval) loadNotifications()
}, document.addEventListener('click', hidePopup)
computed: { interval = setInterval(loadNotifications, LOAD_NOTIFICATIONS_INTERVAL)
unreadNotifications() { })
return this.notifications.filter(n => n.readAt === null).length
}, onUnmounted(() => {
notifications() { document.removeEventListener('click', hidePopup)
return this.allNotifications ? this.allNotifications.filter(n => n.name !== '') : [] clearInterval(interval)
}, })
...mapState({
userInfo: state => state.auth.info, async function loadNotifications() {
}),
},
methods: {
hidePopup(e) {
if (this.showNotifications) {
closeWhenClickedOutside(e, this.$refs.popup, () => this.showNotifications = false)
}
},
async loadNotifications() {
// We're recreating the notification service here to make sure it uses the latest api user token // We're recreating the notification service here to make sure it uses the latest api user token
const notificationService = new NotificationService() const notificationService = new NotificationService()
this.allNotifications = await notificationService.getAll() allNotifications.value = await notificationService.getAll()
}, }
to(n, index) {
function hidePopup(e) {
if (showNotifications.value) {
closeWhenClickedOutside(e, popup.value, () => showNotifications.value = false)
}
}
function to(n, index) {
const to = { const to = {
name: '', name: '',
params: {}, params: {},
@ -126,15 +125,14 @@ export default defineComponent({
return async () => { return async () => {
if (to.name !== '') { if (to.name !== '') {
this.$router.push(to) router.push(to)
} }
n.read = true n.read = true
this.allNotifications[index] = await this.notificationService.update(n) const notificationService = new NotificationService()
allNotifications.value[index] = await notificationService.update(n)
}
} }
},
},
})
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>