Create Posts with a specified language, or fallback

This commit is contained in:
Matt Rider 2019-06-10 17:12:00 -03:00
parent 14041ff396
commit 5986ab2070
4 changed files with 74 additions and 17 deletions

View File

@ -19,7 +19,7 @@ afterEach(async () => {
describe('CreatePost', () => { describe('CreatePost', () => {
const mutation = ` const mutation = `
mutation { mutation {
CreatePost(title: "I am a title", content: "Some content") { CreatePost(title: "I am a title", content: "Some content", language: "en") {
title title
content content
slug slug
@ -74,6 +74,22 @@ describe('CreatePost', () => {
await expect(client.request(mutation)).resolves.toMatchObject(expected) await expect(client.request(mutation)).resolves.toMatchObject(expected)
}) })
}) })
describe('language', () => {
it('allows a user to set the language of the post', async () => {
const createPostWithLanguageMutation = `
mutation {
CreatePost(title: "I am a title", content: "Some content", language: "en") {
language
}
}
`
const expected = { CreatePost: { language: 'en' } }
await expect(client.request(createPostWithLanguageMutation)).resolves.toEqual(
expect.objectContaining(expected),
)
})
})
}) })
}) })

View File

@ -15,29 +15,37 @@ type Post {
disabledBy: User @relation(name: "DISABLED", direction: "IN") disabledBy: User @relation(name: "DISABLED", direction: "IN")
createdAt: String createdAt: String
updatedAt: String updatedAt: String
language: String
relatedContributions: [Post]! @cypher( relatedContributions: [Post]!
statement: """ @cypher(
statement: """
MATCH (this)-[:TAGGED|CATEGORIZED]->(categoryOrTag)<-[:TAGGED|CATEGORIZED]-(post:Post) MATCH (this)-[:TAGGED|CATEGORIZED]->(categoryOrTag)<-[:TAGGED|CATEGORIZED]-(post:Post)
RETURN DISTINCT post RETURN DISTINCT post
LIMIT 10 LIMIT 10
""" """
) )
tags: [Tag]! @relation(name: "TAGGED", direction: "OUT") tags: [Tag]! @relation(name: "TAGGED", direction: "OUT")
categories: [Category]! @relation(name: "CATEGORIZED", direction: "OUT") categories: [Category]! @relation(name: "CATEGORIZED", direction: "OUT")
comments: [Comment]! @relation(name: "COMMENTS", direction: "IN") comments: [Comment]! @relation(name: "COMMENTS", direction: "IN")
commentsCount: Int! @cypher(statement: "MATCH (this)<-[:COMMENTS]-(r:Comment) WHERE NOT r.deleted = true AND NOT r.disabled = true RETURN COUNT(r)") commentsCount: Int!
@cypher(
statement: "MATCH (this)<-[:COMMENTS]-(r:Comment) WHERE NOT r.deleted = true AND NOT r.disabled = true RETURN COUNT(r)"
)
shoutedBy: [User]! @relation(name: "SHOUTED", direction: "IN") shoutedBy: [User]! @relation(name: "SHOUTED", direction: "IN")
shoutedCount: Int! @cypher(statement: "MATCH (this)<-[:SHOUTED]-(r:User) WHERE NOT r.deleted = true AND NOT r.disabled = true RETURN COUNT(DISTINCT r)") shoutedCount: Int!
@cypher(
statement: "MATCH (this)<-[:SHOUTED]-(r:User) WHERE NOT r.deleted = true AND NOT r.disabled = true RETURN COUNT(DISTINCT r)"
)
# Has the currently logged in user shouted that post? # Has the currently logged in user shouted that post?
shoutedByCurrentUser: Boolean! @cypher( shoutedByCurrentUser: Boolean!
statement: """ @cypher(
statement: """
MATCH (this)<-[:SHOUTED]-(u:User {id: $cypherParams.currentUserId}) MATCH (this)<-[:SHOUTED]-(u:User {id: $cypherParams.currentUserId})
RETURN COUNT(u) >= 1 RETURN COUNT(u) >= 1
""" """
) )
} }

View File

@ -6,6 +6,12 @@
<no-ssr> <no-ssr>
<hc-editor :users="users" :value="form.content" @input="updateEditorContent" /> <hc-editor :users="users" :value="form.content" @input="updateEditorContent" />
</no-ssr> </no-ssr>
<ds-select
model="language"
:options="form.languageOptions"
icon="globe"
:placeholder="locale"
/>
<div slot="footer" style="text-align: right"> <div slot="footer" style="text-align: right">
<ds-button :disabled="loading || disabled" ghost @click.prevent="$router.back()"> <ds-button :disabled="loading || disabled" ghost @click.prevent="$router.back()">
{{ $t('actions.cancel') }} {{ $t('actions.cancel') }}
@ -28,6 +34,7 @@
<script> <script>
import gql from 'graphql-tag' import gql from 'graphql-tag'
import HcEditor from '~/components/Editor' import HcEditor from '~/components/Editor'
import orderBy from 'lodash/orderBy'
export default { export default {
components: { components: {
@ -41,6 +48,18 @@ export default {
form: { form: {
title: '', title: '',
content: '', content: '',
language: null,
locales: orderBy(process.env.locales, 'name'),
languageOptions: [
{
label: 'Deutsch',
value: 'de',
},
{
label: 'English',
value: 'en',
},
],
}, },
formSchema: { formSchema: {
title: { required: true, min: 3, max: 64 }, title: { required: true, min: 3, max: 64 },
@ -67,11 +86,17 @@ export default {
}, },
}, },
}, },
computed: {
locale() {
let locale
locale = this.form.locales.find(this.returnLocaleName)
return locale.name
},
},
methods: { methods: {
submit() { submit() {
const postMutations = require('~/graphql/PostMutations.js').default(this) const postMutations = require('~/graphql/PostMutations.js').default(this)
this.loading = true this.loading = true
this.$apollo this.$apollo
.mutate({ .mutate({
mutation: this.id ? postMutations.UpdatePost : postMutations.CreatePost, mutation: this.id ? postMutations.UpdatePost : postMutations.CreatePost,
@ -79,6 +104,7 @@ export default {
id: this.id, id: this.id,
title: this.form.title, title: this.form.title,
content: this.form.content, content: this.form.content,
language: this.form.language ? this.form.language.value : this.$i18n.locale(),
}, },
}) })
.then(res => { .then(res => {
@ -103,6 +129,11 @@ export default {
// this.form.content = value // this.form.content = value
this.$refs.contributionForm.update('content', value) this.$refs.contributionForm.update('content', value)
}, },
returnLocaleName(locale) {
if (this.$i18n.locale() === locale.code) {
return locale
}
},
}, },
apollo: { apollo: {
User: { User: {

View File

@ -3,24 +3,26 @@ import gql from 'graphql-tag'
export default app => { export default app => {
return { return {
CreatePost: gql(` CreatePost: gql(`
mutation($title: String!, $content: String!) { mutation($title: String!, $content: String!, $language: String) {
CreatePost(title: $title, content: $content) { CreatePost(title: $title, content: $content, language: $language) {
id id
title title
slug slug
content content
contentExcerpt contentExcerpt
language
} }
} }
`), `),
UpdatePost: gql(` UpdatePost: gql(`
mutation($id: ID!, $title: String!, $content: String!) { mutation($id: ID!, $title: String!, $content: String!, $language: String) {
UpdatePost(id: $id, title: $title, content: $content) { UpdatePost(id: $id, title: $title, content: $content, language: $language) {
id id
title title
slug slug
content content
contentExcerpt contentExcerpt
language
} }
} }
`), `),