ThreadNew.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <template>
  2. <div class='route_container thread_new'>
  3. <div class='h1'>Post new thread</div>
  4. <div class='thread_meta_info'>
  5. <div class='thread_meta_info__text'>Enter the thread title and the category to post it in</div>
  6. <select-button v-model='selectedCategory' :options='categories'></select-button>
  7. <fancy-input
  8. placeholder='Thread title'
  9. v-model='name'
  10. :error='errors.name'
  11. class='thread_meta_info__title'
  12. large='true'
  13. width='15rem'
  14. ></fancy-input>
  15. <button
  16. class='thread_meta_info__add_poll button button--thin_text'
  17. v-if='!showPoll'
  18. @click='togglePoll(true)'
  19. >Add poll</button>
  20. <transition name='slide'>
  21. <div class='thread_meta_info__poll' v-if='showPoll'>
  22. <div class='thread_meta_info__poll__top_bar'>
  23. <fancy-input
  24. class='thread_meta_info__poll__question'
  25. v-model='pollQuestion'
  26. placeholder='Poll question'
  27. width='20rem'
  28. :large='true'
  29. :error='errors.pollQuestion'
  30. ></fancy-input>
  31. <button class='button button--thin_text button--borderless' @click='removePoll'>
  32. Remove poll
  33. </button>
  34. </div>
  35. <div>
  36. <div class='thread_meta_info__poll__answer' v-for='(pollAnswer, $index) in pollAnswers'>
  37. <fancy-input
  38. v-model='pollAnswer.answer'
  39. width='15rem'
  40. :large='true'
  41. :placeholder='"Answer " + ($index+1)'
  42. ></fancy-input>
  43. <span @click='removePollAnswer($index)' title='Remove answer'>&times;</span>
  44. </div>
  45. <div>
  46. <fancy-input
  47. v-model='newPollAnswer'
  48. placeholder='Option/answer for poll'
  49. style='display: inline-block; margin-right: 0.5rem;'
  50. width='15rem'
  51. :large='true'
  52. :error='errors.pollAnswer'
  53. ></fancy-input>
  54. <button class='button button--thin_text' @click='addPollAnswer'>Add answer</button>
  55. </div>
  56. </div>
  57. </div>
  58. </transition>
  59. </div>
  60. <div class='editor' :class='{"editor--focus": focusInput}'>
  61. <div class='editor__input'>
  62. <div class='editor__format_bar editor__format_bar--editor'>
  63. editor
  64. </div>
  65. <input-editor-core
  66. v-model='editor'
  67. :error='errors.content'
  68. @mentions='setMentions'
  69. @focus='setFocusInput(true)'
  70. @blur='setFocusInput(false)'
  71. ></input-editor-core>
  72. </div>
  73. <div class='editor__preview'>
  74. <div class='editor__format_bar editor__format_bar--preview'>
  75. preview
  76. </div>
  77. <input-editor-preview :value='editor' :mentions='mentions'></input-editor-preview>
  78. </div>
  79. </div>
  80. <loading-button class='button--green submit' :loading='loading' @click='postThread'>Post thread</loading-button>
  81. </div>
  82. </template>
  83. <script>
  84. import InputEditorCore from '../InputEditorCore'
  85. import InputEditorPreview from '../InputEditorPreview'
  86. import FancyInput from '../FancyInput'
  87. import SelectButton from '../SelectButton'
  88. import LoadingButton from '../LoadingButton'
  89. import AjaxErrorHandler from '../../assets/js/errorHandler'
  90. import logger from '../../assets/js/logger'
  91. export default {
  92. name: 'ThreadNew',
  93. components: {
  94. InputEditorCore,
  95. InputEditorPreview,
  96. SelectButton,
  97. FancyInput,
  98. LoadingButton
  99. },
  100. data () {
  101. return {
  102. selectedCategory: this.$store.state.category.selectedCategory,
  103. editor: '',
  104. mentions: [],
  105. name: '',
  106. loading: false,
  107. focusInput: false,
  108. errors: {
  109. content: '',
  110. name: '',
  111. pollQuestion: '',
  112. pollAnswer: ''
  113. },
  114. showPoll: false,
  115. pollQuestion: '',
  116. newPollAnswer: '',
  117. pollAnswers: []
  118. }
  119. },
  120. computed: {
  121. categories () {
  122. return this.$store.getters.categoriesWithoutAll
  123. }
  124. },
  125. methods: {
  126. togglePoll (val) {
  127. if(val !== undefined) {
  128. this.showPoll = val
  129. } else {
  130. this.showPoll = !this.showPoll
  131. }
  132. },
  133. addPollAnswer () {
  134. if(!this.newPollAnswer.trim().length) return
  135. this.pollAnswers.push({ answer: this.newPollAnswer })
  136. this.newPollAnswer = ''
  137. },
  138. removePollAnswer ($index) {
  139. this.pollAnswers.splice($index, 1)
  140. },
  141. removePoll () {
  142. this.pollQuestion = ''
  143. this.pollAnswers = []
  144. this.newPollAnswer = ''
  145. this.togglePoll()
  146. },
  147. setErrors (errors) {
  148. errors.forEach(error => {
  149. this.errors[error.name] = error.error
  150. })
  151. },
  152. clearErrors () {
  153. this.errors.content = ''
  154. this.errors.name = ''
  155. this.errors.pollQuestion = ''
  156. this.errors.pollAnswer = ''
  157. },
  158. hasDuplicates (array, cb) {
  159. if(cb) array = array.map(cb)
  160. return array.length !== (new Set(array)).size
  161. },
  162. postThread () {
  163. let thread
  164. let errors = []
  165. this.clearErrors()
  166. if(!this.editor.trim().length) {
  167. errors.push({name: 'content', error: 'Cannot be blank'})
  168. } if(!this.name.trim().length) {
  169. errors.push({name: 'name', error: 'Cannot be blank'})
  170. } if(this.showPoll && !this.pollQuestion.trim().length) {
  171. errors.push({name: 'pollQuestion', error: 'Cannot be blank'})
  172. } if (this.showPoll && this.pollAnswers.length < 2) {
  173. errors.push({name: 'pollAnswer', error: 'You need at least 2 answers'})
  174. } if (this.showPoll && this.hasDuplicates(this.pollAnswers, i => i.answer)) {
  175. errors.push({name: 'pollAnswer', error: 'Your answers can\'t contain any duplicates'})
  176. } if(errors.length) {
  177. this.setErrors(errors)
  178. return
  179. }
  180. this.loading = true
  181. this.axios.post('/api/v1/thread', {
  182. name: this.name,
  183. category: this.selectedCategory
  184. }).then(res => {
  185. thread = res.data
  186. let ajax = []
  187. ajax.push(
  188. this.axios.post('/api/v1/post', {
  189. threadId: res.data.id,
  190. content: this.editor,
  191. mentions: this.mentions
  192. })
  193. )
  194. if(this.showPoll) {
  195. ajax.push(
  196. this.axios.post('/api/v1/poll', {
  197. question: this.pollQuestion,
  198. answers: this.pollAnswers.map(a => a.answer),
  199. threadId: res.data.id
  200. })
  201. )
  202. }
  203. return Promise.all(ajax)
  204. }).then(res => {
  205. this.loading = false
  206. this.$router.push(`/thread/${thread.slug}/${thread.id}/0`)
  207. }).catch(e => {
  208. this.loading = false
  209. AjaxErrorHandler(this.$store)(e, (error, errors) => {
  210. let path = error.path
  211. if(this.errors[path] !== undefined) {
  212. this.errors[path] = error.message
  213. } else {
  214. errors.push(error.message)
  215. }
  216. })
  217. })
  218. },
  219. setFocusInput (val) {
  220. this.focusInput = val
  221. },
  222. setMentions (mentions) {
  223. this.mentions = mentions
  224. }
  225. },
  226. watch: {
  227. '$store.state.username' (username) {
  228. if(!username) {
  229. this.$router.push('/')
  230. }
  231. }
  232. },
  233. mounted () {
  234. this.$store.dispatch('setTitle', 'new thread')
  235. logger('threadNew')
  236. },
  237. beforeRouteEnter (to, from, next) {
  238. next(vm => {
  239. if(!vm.$store.state.username) {
  240. vm.$store.commit('setAccountModalState', true);
  241. next('/')
  242. }
  243. })
  244. }
  245. }
  246. </script>
  247. <style lang='scss'>
  248. @import '../../assets/scss/variables.scss';
  249. .thread_new {
  250. margin-top: 1rem;
  251. }
  252. .thread_meta_info {
  253. background-color: #fff;
  254. @extend .shadow_border;
  255. border-radius: 0.25rem;
  256. padding: 1rem;
  257. margin: 1rem 0;
  258. @at-root #{&}__title {
  259. margin: 0 0.5rem;
  260. margin-top: 0.5rem;
  261. display: inline-block;
  262. }
  263. @at-root #{&}__add_poll {
  264. margin-top: 0.5rem;
  265. }
  266. @at-root #{&}__text {
  267. margin-bottom: 0.5rem;
  268. }
  269. @at-root #{&}__poll {
  270. border-top: thin solid $color__gray--primary;
  271. margin-top: 1rem;
  272. padding-top: 0.75rem;
  273. position: relative;
  274. @at-root #{&}__top_bar {
  275. display: flex;
  276. justify-content: space-between;
  277. align-items: baseline;
  278. }
  279. @at-root #{&}__answer {
  280. display: flex;
  281. align-items: baseline;
  282. & > span {
  283. opacity: 0;
  284. pointer-events: none;
  285. transition: all 0.1s;
  286. font-size: 1.5rem;
  287. margin-left: 0.5rem;
  288. cursor: pointer;
  289. @include user-select(none);
  290. }
  291. &:hover > span {
  292. opacity: 1;
  293. pointer-events: all;
  294. }
  295. }
  296. }
  297. }
  298. .submit {
  299. margin-top: 1rem;
  300. }
  301. .editor {
  302. display: flex;
  303. background-color: #fff;
  304. border-radius: 0.25rem;
  305. box-shadow: 0 0 0.3rem rgba(175, 175, 175, 0.5);
  306. transition: all 0.2s;
  307. @at-root #{&}--focus {
  308. box-shadow: 0 0 0.3rem rgba(175, 175, 175, 1);
  309. }
  310. @at-root #{&}__format_bar {
  311. height: 2.5rem;
  312. background-color: $color__gray--primary;
  313. display: flex;
  314. padding-right: 1rem;
  315. padding-bottom: 0.25rem;
  316. justify-content: flex-end;
  317. align-items: center;
  318. font-variant: small-caps;
  319. @at-root #{&}--preview {
  320. border-radius: 0 0.25rem 0 0;
  321. }
  322. @at-root #{&}--editor {
  323. border-radius: 0.25rem 0 0 0;
  324. }
  325. }
  326. @at-root #{&}__input {
  327. width: 50%;
  328. position: relative;
  329. .input_editor_core__format_bar {
  330. left: 0rem;
  331. }
  332. .input_editor_core textarea {
  333. height: 14rem;
  334. }
  335. }
  336. @at-root #{&}__preview {
  337. border-left: 1px solid $color__gray--darker;
  338. width: 50%;
  339. .input_editor_preview__markdownHTML {
  340. height: 14.2rem;
  341. }
  342. }
  343. }
  344. @media (max-width: 420px) {
  345. .thread_meta_info {
  346. @at-root #{&}__title.fancy_input {
  347. margin: 0;
  348. margin-top: 0.5rem;
  349. }
  350. @at-root #{&}__poll__top_bar .button {
  351. position: absolute;
  352. bottom: 0;
  353. right: 0;
  354. }
  355. @at-root #{&}__poll__question {
  356. width: 100%;
  357. > div, input {
  358. width: 100% !important;
  359. }
  360. }
  361. }
  362. .editor {
  363. flex-direction: column;
  364. overflow-x: hidden;
  365. @at-root #{&}__input, #{&}__preview {
  366. border: 0;
  367. width: 100%;
  368. }
  369. }
  370. }
  371. </style>