123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- <template>
- <div class='route_container thread_new'>
- <div class='h1'>Post new thread</div>
- <div class='thread_meta_info'>
- <div class='thread_meta_info__text'>Enter the thread title and the category to post it in</div>
- <select-button v-model='selectedCategory' :options='categories'></select-button>
- <fancy-input
- placeholder='Thread title'
- v-model='name'
- :error='errors.name'
- class='thread_meta_info__title'
- large='true'
- width='15rem'
- ></fancy-input>
-
- <button
- class='thread_meta_info__add_poll button button--thin_text'
- v-if='!showPoll'
- @click='togglePoll(true)'
- >Add poll</button>
- <transition name='slide'>
- <div class='thread_meta_info__poll' v-if='showPoll'>
- <div class='thread_meta_info__poll__top_bar'>
- <fancy-input
- class='thread_meta_info__poll__question'
- v-model='pollQuestion'
- placeholder='Poll question'
- width='20rem'
- :large='true'
- :error='errors.pollQuestion'
- ></fancy-input>
- <button class='button button--thin_text button--borderless' @click='removePoll'>
- Remove poll
- </button>
- </div>
- <div>
- <div class='thread_meta_info__poll__answer' v-for='(pollAnswer, $index) in pollAnswers'>
- <fancy-input
- v-model='pollAnswer.answer'
- width='15rem'
- :large='true'
- :placeholder='"Answer " + ($index+1)'
- ></fancy-input>
- <span @click='removePollAnswer($index)' title='Remove answer'>×</span>
- </div>
- <div>
- <fancy-input
- v-model='newPollAnswer'
- placeholder='Option/answer for poll'
- style='display: inline-block; margin-right: 0.5rem;'
- width='15rem'
- :large='true'
- :error='errors.pollAnswer'
- ></fancy-input>
- <button class='button button--thin_text' @click='addPollAnswer'>Add answer</button>
- </div>
- </div>
- </div>
- </transition>
- </div>
- <div class='editor' :class='{"editor--focus": focusInput}'>
- <div class='editor__input'>
- <div class='editor__format_bar editor__format_bar--editor'>
- editor
- </div>
- <input-editor-core
- v-model='editor'
- :error='errors.content'
- @mentions='setMentions'
- @focus='setFocusInput(true)'
- @blur='setFocusInput(false)'
- ></input-editor-core>
- </div>
- <div class='editor__preview'>
- <div class='editor__format_bar editor__format_bar--preview'>
- preview
- </div>
- <input-editor-preview :value='editor' :mentions='mentions'></input-editor-preview>
- </div>
- </div>
- <loading-button class='button--green submit' :loading='loading' @click='postThread'>Post thread</loading-button>
- </div>
- </template>
- <script>
- import InputEditorCore from '../InputEditorCore'
- import InputEditorPreview from '../InputEditorPreview'
- import FancyInput from '../FancyInput'
- import SelectButton from '../SelectButton'
- import LoadingButton from '../LoadingButton'
- import AjaxErrorHandler from '../../assets/js/errorHandler'
- import logger from '../../assets/js/logger'
-
- export default {
- name: 'ThreadNew',
- components: {
- InputEditorCore,
- InputEditorPreview,
- SelectButton,
- FancyInput,
- LoadingButton
- },
- data () {
- return {
- selectedCategory: this.$store.state.category.selectedCategory,
- editor: '',
- mentions: [],
- name: '',
- loading: false,
- focusInput: false,
- errors: {
- content: '',
- name: '',
- pollQuestion: '',
- pollAnswer: ''
- },
- showPoll: false,
- pollQuestion: '',
- newPollAnswer: '',
- pollAnswers: []
- }
- },
- computed: {
- categories () {
- return this.$store.getters.categoriesWithoutAll
- }
- },
- methods: {
- togglePoll (val) {
- if(val !== undefined) {
- this.showPoll = val
- } else {
- this.showPoll = !this.showPoll
- }
- },
- addPollAnswer () {
- if(!this.newPollAnswer.trim().length) return
- this.pollAnswers.push({ answer: this.newPollAnswer })
- this.newPollAnswer = ''
- },
- removePollAnswer ($index) {
- this.pollAnswers.splice($index, 1)
- },
- removePoll () {
- this.pollQuestion = ''
- this.pollAnswers = []
- this.newPollAnswer = ''
- this.togglePoll()
- },
- setErrors (errors) {
- errors.forEach(error => {
- this.errors[error.name] = error.error
- })
- },
- clearErrors () {
- this.errors.content = ''
- this.errors.name = ''
- this.errors.pollQuestion = ''
- this.errors.pollAnswer = ''
- },
- hasDuplicates (array, cb) {
- if(cb) array = array.map(cb)
-
- return array.length !== (new Set(array)).size
- },
- postThread () {
- let thread
- let errors = []
- this.clearErrors()
- if(!this.editor.trim().length) {
- errors.push({name: 'content', error: 'Cannot be blank'})
- } if(!this.name.trim().length) {
- errors.push({name: 'name', error: 'Cannot be blank'})
- } if(this.showPoll && !this.pollQuestion.trim().length) {
- errors.push({name: 'pollQuestion', error: 'Cannot be blank'})
- } if (this.showPoll && this.pollAnswers.length < 2) {
- errors.push({name: 'pollAnswer', error: 'You need at least 2 answers'})
- } if (this.showPoll && this.hasDuplicates(this.pollAnswers, i => i.answer)) {
- errors.push({name: 'pollAnswer', error: 'Your answers can\'t contain any duplicates'})
- } if(errors.length) {
- this.setErrors(errors)
- return
- }
- this.loading = true
- this.axios.post('/api/v1/thread', {
- name: this.name,
- category: this.selectedCategory
- }).then(res => {
- thread = res.data
- let ajax = []
- ajax.push(
- this.axios.post('/api/v1/post', {
- threadId: res.data.id,
- content: this.editor,
- mentions: this.mentions
- })
- )
- if(this.showPoll) {
- ajax.push(
- this.axios.post('/api/v1/poll', {
- question: this.pollQuestion,
- answers: this.pollAnswers.map(a => a.answer),
- threadId: res.data.id
- })
- )
- }
- return Promise.all(ajax)
- }).then(res => {
- this.loading = false
- this.$router.push(`/thread/${thread.slug}/${thread.id}/0`)
- }).catch(e => {
- this.loading = false
- AjaxErrorHandler(this.$store)(e, (error, errors) => {
- let path = error.path
- if(this.errors[path] !== undefined) {
- this.errors[path] = error.message
- } else {
- errors.push(error.message)
- }
- })
- })
- },
- setFocusInput (val) {
- this.focusInput = val
- },
- setMentions (mentions) {
- this.mentions = mentions
- }
- },
- watch: {
- '$store.state.username' (username) {
- if(!username) {
- this.$router.push('/')
- }
- }
- },
- mounted () {
- this.$store.dispatch('setTitle', 'new thread')
- logger('threadNew')
- },
- beforeRouteEnter (to, from, next) {
- next(vm => {
- if(!vm.$store.state.username) {
- vm.$store.commit('setAccountModalState', true);
- next('/')
- }
- })
- }
- }
- </script>
- <style lang='scss'>
- @import '../../assets/scss/variables.scss';
- .thread_new {
- margin-top: 1rem;
- }
- .thread_meta_info {
- background-color: #fff;
- @extend .shadow_border;
- border-radius: 0.25rem;
- padding: 1rem;
- margin: 1rem 0;
- @at-root #{&}__title {
- margin: 0 0.5rem;
- margin-top: 0.5rem;
- display: inline-block;
- }
- @at-root #{&}__add_poll {
- margin-top: 0.5rem;
- }
- @at-root #{&}__text {
- margin-bottom: 0.5rem;
- }
- @at-root #{&}__poll {
- border-top: thin solid $color__gray--primary;
- margin-top: 1rem;
- padding-top: 0.75rem;
- position: relative;
- @at-root #{&}__top_bar {
- display: flex;
- justify-content: space-between;
- align-items: baseline;
- }
- @at-root #{&}__answer {
- display: flex;
- align-items: baseline;
- & > span {
- opacity: 0;
- pointer-events: none;
- transition: all 0.1s;
- font-size: 1.5rem;
- margin-left: 0.5rem;
- cursor: pointer;
- @include user-select(none);
- }
- &:hover > span {
- opacity: 1;
- pointer-events: all;
- }
- }
- }
- }
- .submit {
- margin-top: 1rem;
- }
- .editor {
- display: flex;
- background-color: #fff;
- border-radius: 0.25rem;
- box-shadow: 0 0 0.3rem rgba(175, 175, 175, 0.5);
- transition: all 0.2s;
- @at-root #{&}--focus {
- box-shadow: 0 0 0.3rem rgba(175, 175, 175, 1);
- }
- @at-root #{&}__format_bar {
- height: 2.5rem;
- background-color: $color__gray--primary;
- display: flex;
- padding-right: 1rem;
- padding-bottom: 0.25rem;
- justify-content: flex-end;
- align-items: center;
- font-variant: small-caps;
- @at-root #{&}--preview {
- border-radius: 0 0.25rem 0 0;
- }
- @at-root #{&}--editor {
- border-radius: 0.25rem 0 0 0;
- }
- }
- @at-root #{&}__input {
- width: 50%;
- position: relative;
- .input_editor_core__format_bar {
- left: 0rem;
- }
- .input_editor_core textarea {
- height: 14rem;
- }
- }
- @at-root #{&}__preview {
- border-left: 1px solid $color__gray--darker;
- width: 50%;
- .input_editor_preview__markdownHTML {
- height: 14.2rem;
- }
- }
- }
- @media (max-width: 420px) {
- .thread_meta_info {
- @at-root #{&}__title.fancy_input {
- margin: 0;
- margin-top: 0.5rem;
- }
- @at-root #{&}__poll__top_bar .button {
- position: absolute;
- bottom: 0;
- right: 0;
- }
- @at-root #{&}__poll__question {
- width: 100%;
- > div, input {
- width: 100% !important;
- }
- }
- }
- .editor {
- flex-direction: column;
- overflow-x: hidden;
- @at-root #{&}__input, #{&}__preview {
- border: 0;
- width: 100%;
- }
- }
- }
- </style>
|