edit.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <!-- Finish handling of URL -->
  3. <div>
  4. <Header
  5. title="Write your blog"
  6. exitLink="/instructor/blogs">
  7. <!-- TODO: Check if blog status is active -->
  8. <template v-if="blog.status === 'active'" #actionMenu>
  9. <div class="full-page-takeover-header-button">
  10. <!-- TODO: Check blog validity before publishing -->
  11. <Modal
  12. @submitted="updateBlogStatus($event, 'published')"
  13. @opened="checkBlogValidity"
  14. openTitle="Publish"
  15. openBtnClass="button is-success is-medium is-inverted is-outlined"
  16. title="Review Details">
  17. <div>
  18. <div class="title">Once you publish blog you cannot change url to a blog.</div>
  19. <!-- Check for error -->
  20. <div v-if="!publishError">
  21. <div class="subtitle">This is how url to blog post will look like after publish:</div>
  22. <article class="message is-success">
  23. <div class="message-body">
  24. <!-- Get here actual slug -->
  25. <strong>{{getCurrentUrl()}}/blogs/{{slug}}</strong>
  26. </div>
  27. </article>
  28. </div>
  29. <article v-else class="message is-danger">
  30. <div class="message-body">
  31. {{publishError}}
  32. </div>
  33. </article>
  34. </div>
  35. </Modal>
  36. </div>
  37. </template>
  38. <template v-else #actionMenu>
  39. <div class="full-page-takeover-header-button">
  40. <Modal
  41. @submitted="updateBlogStatus($event, 'active')"
  42. openTitle="Unpublish"
  43. openBtnClass="button is-success is-medium is-inverted is-outlined"
  44. title="Unpublish Blog">
  45. <div>
  46. <div class="title">Unpublish blog so it's no longer displayed in blogs page</div>
  47. </div>
  48. </Modal>
  49. </div>
  50. </template>
  51. </Header>
  52. <div class="blog-editor-container">
  53. <div class="container">
  54. <editor
  55. @editorMounted="initBlogContent"
  56. @editorUpdated="updateBlog"
  57. :isSaving="isSaving"
  58. ref="editor"
  59. />
  60. </div>
  61. </div>
  62. </div>
  63. </template>
  64. <script>
  65. import Editor from '~/components/editor'
  66. import Header from '~/components/shared/Header'
  67. import Modal from '~/components/shared/Modal'
  68. import { mapState } from "vuex"
  69. import slugify from 'slugify'
  70. export default {
  71. layout: 'instructor',
  72. components: {
  73. Editor, Header, Modal
  74. },
  75. data() {
  76. return {
  77. publishError: '',
  78. slug: ''
  79. }
  80. },
  81. computed : {
  82. ...mapState({
  83. blog: ({instructor}) => instructor.blog.item,
  84. isSaving: ({instructor}) => instructor.blog.isSaving
  85. }),
  86. editor() {
  87. return this.$refs.editor
  88. }
  89. },
  90. async fetch({params, store}){
  91. await store.dispatch('instructor/blog/fetchBlogById', params.id)
  92. },
  93. methods: {
  94. // initBlogContent(editor) {
  95. // if (this.blog && this.blog.content) {
  96. // editor.setContent(this.blog.content)
  97. // }
  98. // }
  99. initBlogContent(initContent) {
  100. if (this.blog && this.blog.content) {
  101. initContent(this.blog.content)
  102. }
  103. },
  104. async updateBlog(blogData) {
  105. try{
  106. if (!this.isSaving) {
  107. await this.$store.dispatch('instructor/blog/updateBlog', {data: blogData, id: this.blog._id})
  108. this.$toasted.success('Blog Updated!', {duration: 2000})
  109. }
  110. }catch(error){
  111. this.$toasted.error('Blog cannot be saved!', {duration: 2000})
  112. }
  113. },
  114. async updateBlogStatus({closeModal}, status) {
  115. const blogContent = this.editor.getContent()
  116. // blogContent.status = 'published'
  117. blogContent.status = status
  118. const message = status === 'published' ? 'Blog has been published!' : 'Blog has been un-published!'
  119. try {
  120. await this.$store.dispatch('instructor/blog/updateBlog', {data: blogContent, id: this.blog._id})
  121. this.$toasted.success(message, {duration: 3000})
  122. closeModal()
  123. } catch (error) {
  124. this.$toasted.error('Blog cannot be published!', {duration: 3000})
  125. }
  126. },
  127. checkBlogValidity() {
  128. const title = this.editor.getNodeValueByName('title')
  129. this.publishError = ''
  130. this.slug = ''
  131. if (title && title.length > 24) {
  132. this.slug = this.slugify(title)
  133. } else {
  134. this.publishError = 'Cannot publish! Title needs to be longer than 24 characters!'
  135. }
  136. },
  137. getCurrentUrl() {
  138. // process.client will return true if we are in browser environment
  139. return process.client && window.location.origin
  140. },
  141. slugify(text) {
  142. return slugify(text, {
  143. replacement: '-',
  144. remove: null,
  145. lower: true
  146. })
  147. }
  148. }
  149. }
  150. </script>