edit.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 #actionMenu>
  9. <div class="full-page-takeover-header-button">
  10. <!-- TODO: Check blog validity before publishing -->
  11. <Modal
  12. @submitted="publishBlog"
  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. openTitle="Unpublish"
  42. openBtnClass="button is-success is-medium is-inverted is-outlined"
  43. title="Unpublish Blog">
  44. <div>
  45. <div class="title">Unpublish blog so it's no longer displayed in blogs page</div>
  46. </div>
  47. </Modal>
  48. </div>
  49. </template> -->
  50. </Header>
  51. <div class="blog-editor-container">
  52. <div class="container">
  53. <editor
  54. @editorMounted="initBlogContent"
  55. @editorUpdated="updateBlog"
  56. :isSaving="isSaving"
  57. ref="editor"
  58. />
  59. </div>
  60. </div>
  61. </div>
  62. </template>
  63. <script>
  64. import Editor from '~/components/editor'
  65. import Header from '~/components/shared/Header'
  66. import Modal from '~/components/shared/Modal'
  67. import { mapState } from "vuex"
  68. import slugify from 'slugify'
  69. export default {
  70. layout: 'instructor',
  71. components: {
  72. Editor, Header, Modal
  73. },
  74. data() {
  75. return {
  76. publishError: '',
  77. slug: ''
  78. }
  79. },
  80. computed : {
  81. ...mapState({
  82. blog: ({instructor}) => instructor.blog.item,
  83. isSaving: ({instructor}) => instructor.blog.isSaving
  84. }),
  85. editor() {
  86. return this.$refs.editor
  87. }
  88. },
  89. async fetch({params, store}){
  90. await store.dispatch('instructor/blog/fetchBlogById', params.id)
  91. },
  92. methods: {
  93. // initBlogContent(editor) {
  94. // if (this.blog && this.blog.content) {
  95. // editor.setContent(this.blog.content)
  96. // }
  97. // }
  98. initBlogContent(initContent) {
  99. if (this.blog && this.blog.content) {
  100. initContent(this.blog.content)
  101. }
  102. },
  103. async updateBlog(blogData) {
  104. try{
  105. if (!this.isSaving) {
  106. await this.$store.dispatch('instructor/blog/updateBlog', {data: blogData, id: this.blog._id})
  107. this.$toasted.success('Blog Updated!', {duration: 2000})
  108. }
  109. }catch(error){
  110. this.$toasted.error('Blog cannot be saved!', {duration: 2000})
  111. }
  112. },
  113. publishBlog({closeModal}) {
  114. const blogContent = this.editor.getContent()
  115. debugger
  116. },
  117. checkBlogValidity() {
  118. const title = this.editor.getNodeValueByName('title')
  119. this.publishError = ''
  120. this.slug = ''
  121. if (title && title.length > 24) {
  122. this.slug = this.slugify(title)
  123. } else {
  124. this.publishError = 'Cannot publish! Title needs to be longer than 24 characters!'
  125. }
  126. },
  127. getCurrentUrl() {
  128. // process.client will return true if we are in browser environment
  129. return process.client && window.location.origin
  130. },
  131. slugify(text) {
  132. return slugify(text, {
  133. replacement: '-',
  134. remove: null,
  135. lower: true
  136. })
  137. }
  138. }
  139. }
  140. </script>