edit.vue 4.0 KB

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