Get categories working with CreatePost

This commit is contained in:
Matt Rider 2019-06-26 15:59:57 -03:00
parent 46b83c540d
commit 07576d084c
6 changed files with 112 additions and 9 deletions

View File

@ -105,7 +105,7 @@ const permissions = shield(
Query: { Query: {
'*': deny, '*': deny,
findPosts: allow, findPosts: allow,
Category: isAdmin, Category: allow,
Tag: isAdmin, Tag: isAdmin,
Report: isModerator, Report: isModerator,
Notification: isAdmin, Notification: isAdmin,

View File

@ -62,7 +62,6 @@ export default {
} }
} }
session.close() session.close()
return post return post
}, },
}, },

View File

@ -0,0 +1,90 @@
<template>
<div>
<ds-flex :gutter="{ base: 'xx-small', md: 'small', lg: 'xx-small' }">
<div v-for="category in categories" :key="category.id">
<ds-flex-item>
<ds-button
size="small"
@click.prevent="toggleCategory(category.id)"
:primary="isActive(category.id)"
:disabled="isDisabled(category.id)"
>
<ds-icon :name="category.icon" />
{{ category.name }}
</ds-button>
</ds-flex-item>
</div>
</ds-flex>
<p class="small-info">
{{
$t('contribution.categories.infoSelectedNoOfMaxCategories', {
chosen: selectedCount,
max: selectedMax,
})
}}
</p>
</div>
</template>
<script>
import gql from 'graphql-tag'
export default {
data() {
return {
categories: null,
selectedMax: 3,
selectedCategoryIds: [],
}
},
computed: {
selectedCount() {
return this.selectedCategoryIds.length
},
reachedMaximum() {
return this.selectedCount >= this.selectedMax
},
},
watch: {
selectedCategoryIds(categoryIds) {
this.$emit('updateCategories', categoryIds)
},
},
methods: {
toggleCategory(id) {
const index = this.selectedCategoryIds.indexOf(id)
if (index > -1) {
this.selectedCategoryIds.splice(index)
} else {
this.selectedCategoryIds.push(id)
}
},
isActive(id) {
const activeCategory = this.selectedCategoryIds.find(categoryId => categoryId === id)
if (activeCategory) {
return true
}
return false
},
isDisabled(id) {
return !!(this.reachedMaximum && !this.isActive(id))
},
},
apollo: {
Category: {
query() {
return gql(`{
Category {
id
name
icon
}
}`)
},
result(result) {
this.categories = result.data.Category
},
},
},
}
</script>

View File

@ -7,6 +7,7 @@
<hc-editor :users="users" :value="form.content" @input="updateEditorContent" /> <hc-editor :users="users" :value="form.content" @input="updateEditorContent" />
</no-ssr> </no-ssr>
<ds-space margin-bottom="xxx-large" /> <ds-space margin-bottom="xxx-large" />
<hc-categories-select model="categories" @updateCategories="updateCategories" />
<ds-flex class="contribution-form-footer"> <ds-flex class="contribution-form-footer">
<ds-flex-item :width="{ base: '10%', sm: '10%', md: '10%', lg: '15%' }" /> <ds-flex-item :width="{ base: '10%', sm: '10%', md: '10%', lg: '15%' }" />
<ds-flex-item :width="{ base: '80%', sm: '30%', md: '30%', lg: '20%' }"> <ds-flex-item :width="{ base: '80%', sm: '30%', md: '30%', lg: '20%' }">
@ -50,10 +51,12 @@ import HcEditor from '~/components/Editor'
import orderBy from 'lodash/orderBy' import orderBy from 'lodash/orderBy'
import locales from '~/locales' import locales from '~/locales'
import PostMutations from '~/graphql/PostMutations.js' import PostMutations from '~/graphql/PostMutations.js'
import HcCategoriesSelect from '~/components/CategoriesSelect/CategoriesSelect'
export default { export default {
components: { components: {
HcEditor, HcEditor,
HcCategoriesSelect,
}, },
props: { props: {
contribution: { type: Object, default: () => {} }, contribution: { type: Object, default: () => {} },
@ -65,6 +68,7 @@ export default {
content: '', content: '',
language: null, language: null,
languageOptions: [], languageOptions: [],
categories: null,
}, },
formSchema: { formSchema: {
title: { required: true, min: 3, max: 64 }, title: { required: true, min: 3, max: 64 },
@ -106,22 +110,23 @@ export default {
}, },
methods: { methods: {
submit() { submit() {
const { title, content, language, categories } = this.form
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,
variables: { variables: {
id: this.id, id: this.id,
title: this.form.title, title,
content: this.form.content, content,
language: this.form.language ? this.form.language.value : this.$i18n.locale(), language: language ? language.value : this.$i18n.locale(),
categories,
}, },
}) })
.then(res => { .then(res => {
this.loading = false this.loading = false
this.$toast.success(this.$t('contribution.success')) this.$toast.success(this.$t('contribution.success'))
this.disabled = true this.disabled = true
const result = res.data[this.id ? 'UpdatePost' : 'CreatePost'] const result = res.data[this.id ? 'UpdatePost' : 'CreatePost']
this.$router.push({ this.$router.push({
@ -144,6 +149,9 @@ export default {
this.form.languageOptions.push({ label: locale.name, value: locale.code }) this.form.languageOptions.push({ label: locale.name, value: locale.code })
}) })
}, },
updateCategories(ids) {
this.form.categories = ids
},
}, },
apollo: { apollo: {
User: { User: {

View File

@ -3,14 +3,17 @@ import gql from 'graphql-tag'
export default () => { export default () => {
return { return {
CreatePost: gql` CreatePost: gql`
mutation($title: String!, $content: String!, $language: String) { mutation($title: String!, $content: String!, $language: String, $categories: [ID]) {
CreatePost(title: $title, content: $content, language: $language) { CreatePost(title: $title, content: $content, language: $language, categories: $categories) {
id id
title title
slug slug
content content
contentExcerpt contentExcerpt
language language
categories {
name
}
} }
} }
`, `,

View File

@ -331,6 +331,9 @@
"filterFollow": "Filter contributions from users I follow", "filterFollow": "Filter contributions from users I follow",
"filterALL": "View all contributions", "filterALL": "View all contributions",
"success": "Saved!", "success": "Saved!",
"languageSelectLabel": "Language" "languageSelectLabel": "Language",
"categories": {
"infoSelectedNoOfMaxCategories": "{chosen} of {max} categories selected"
}
} }
} }