Merge pull request #1991 from Human-Connection/Locale_save_by_change_LocalSwitch

Locale save by change local switch
This commit is contained in:
mattwr18 2019-10-24 10:02:46 +02:00 committed by GitHub
commit a38731fcae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View File

@ -37,6 +37,8 @@ import Dropdown from '~/components/Dropdown'
import find from 'lodash/find'
import orderBy from 'lodash/orderBy'
import locales from '~/locales'
import { mapGetters, mapMutations } from 'vuex'
import { localeMutation } from '~/graphql/User.js'
export default {
components: {
@ -64,15 +66,44 @@ export default {
})
return routes
},
...mapGetters({
currentUser: 'auth/user',
}),
},
methods: {
changeLanguage(locale, toggleMenu) {
this.$i18n.set(locale)
this.updateUserLocale()
toggleMenu()
},
matcher(locale) {
return locale === this.$i18n.locale()
},
...mapMutations({
setCurrentUser: 'auth/SET_USER',
}),
async updateUserLocale() {
try {
await this.$apollo.mutate({
mutation: localeMutation(),
variables: {
id: this.currentUser.id,
locale: this.$i18n.locale(),
},
update: (store, { data: { UpdateUser } }) => {
const { locale } = UpdateUser
this.setCurrentUser({
...this.currentUser,
locale,
})
},
})
this.$toast.success(this.$t('contribution.success'))
} catch (err) {
this.$toast.error(err.message)
}
},
},
}
</script>

View File

@ -153,3 +153,14 @@ export const checkSlugAvailableQuery = gql`
}
}
`
export const localeMutation = () => {
return gql`
mutation($id: ID!, $locale: String) {
UpdateUser(id: $id, locale: $locale) {
id
locale
}
}
`
}