Add generateProfileLink

This commit is contained in:
Maxim Filippov 2018-12-13 05:00:01 +03:00
parent ae8f0f36d8
commit 24caa2cfcd
4 changed files with 31 additions and 1 deletions

View file

@ -1,3 +1,5 @@
import generateProfileLink from 'src/services/user_profile_link_generator/user_profile_link_generator'
const chatPanel = { const chatPanel = {
data () { data () {
return { return {
@ -9,6 +11,9 @@ const chatPanel = {
computed: { computed: {
messages () { messages () {
return this.$store.state.chat.messages return this.$store.state.chat.messages
},
userProfileLink (user) {
return generateProfileLink(user, user.name)
} }
}, },
methods: { methods: {

View file

@ -13,7 +13,7 @@
<img :src="message.author.avatar" /> <img :src="message.author.avatar" />
</span> </span>
<div class="chat-content"> <div class="chat-content">
<router-link class="chat-name" :to="{ name: 'user-profile', params: { name: message.author.username } }"> <router-link class="chat-name" :to="userProfileLink(message.author)">
{{message.author.username}} {{message.author.username}}
</router-link> </router-link>
<br> <br>

View file

@ -0,0 +1,10 @@
const generateProfileLink = (user, name = '') => {
const baseLinkParams = { name: 'user-profile' }
const { id } = user
return { ...baseLinkParams, params: (isExternal(user) ? { id } : { name }) }
}
const isExternal = ({screen_name}) => (screen_name.indexOf('@') > 0)
export default generateProfileLink

View file

@ -0,0 +1,15 @@
import generateProfileLink from 'src/services/user_profile_link_generator/user_profile_link_generator'
describe('generateProfileLink', () => {
it('returns obj for local user', () => {
const user = { screen_name: 'john' }
expect(generateProfileLink(user, 'jack')).to.eql({ name: 'user-profile', params: { name: 'jack' } })
})
it('returns obj for external user', () => {
const user = { screen_name: 'john@domain', id: 1 }
expect(generateProfileLink(user)).to.eql({ name: 'user-profile', params: { id: 1 } })
})
})