New store module for setting the document title. Each route changes the document title to its name

This commit is contained in:
2019-10-22 22:52:24 +02:00
parent 0fdaf5bd4e
commit 001c243f95
3 changed files with 46 additions and 11 deletions

View File

@@ -0,0 +1,37 @@
const capitalize = (string) => {
return string.includes(' ') ?
string.split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1).replace('_', ' ')).join(' ')
: string.charAt(0).toUpperCase() + string.slice(1)
}
const setDocumentTitle = (state) => {
document.title = `${state.emoji} ${state.titlePrefix} | ${capitalize(state.title)}`
}
export default {
namespaced: true,
state: {
emoji: '🍕',
titlePrefix: 'request',
title: undefined
},
getters: {},
mutations: {
SET_EMOJI: (state, emoji) => {
state.emoji = emoji
setDocumentTitle(state)
},
SET_TITLE: (state, title) => {
state.title = title
setDocumentTitle(state)
}
},
actions: {
updateEmoji({ commit }, emoji) {
commit('SET_EMOJI', emoji)
},
updateTitle({ commit }, title) {
commit('SET_TITLE', title)
}
}
}