blog.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. function separateBlogs(blogs) {
  2. const published = []
  3. const drafts = []
  4. blogs.forEach(blog => {
  5. blog.status === 'active' ? drafts.push(blog) : published.push(blog)
  6. })
  7. return {published, drafts}
  8. }
  9. export const state = () => ({
  10. items: {
  11. drafts: [],
  12. published: []
  13. },
  14. item: {},
  15. isSaving: false
  16. })
  17. export const actions = {
  18. async createBlog({commit}, blogData) {
  19. commit('setIsSaving', true)
  20. console.log('instructor/blog.js actions createBlog call axios.$post->blogData')
  21. const blog = await this.$axios.$post('/api/v1/blogs', blogData)
  22. if(blog.isAxiosError === true){
  23. commit('setIsSaving', false)
  24. console.log(blog.data)
  25. return Error('')
  26. }
  27. console.log('instructor/blog.js actions createBlog done axios.$post->blogData')
  28. commit('setIsSaving', false)
  29. return blog
  30. },
  31. async fetchBlogById({commit}, blogId) {
  32. console.log('instructor/blog.js actions fetchBlogById call axios.$get->blogId')
  33. const blog = await this.$axios.$get(`/api/v1/blogs/${blogId}`)
  34. if(blog.isAxiosError === true){
  35. console.log(blog.data)
  36. return Error('')
  37. }
  38. console.log('instructor/blog.js actions fetchBlogById done axios.$get->blogId')
  39. console.log('courses.js mutations call setBlog')
  40. commit('setBlog', blog)
  41. console.log('courses.js mutations done setBlog')
  42. },
  43. async fetchUserBlogs({commit, state}) {
  44. const blogs = await this.$axios.$get('/api/v1/blogs/me')
  45. debugger;
  46. if(blogs.isAxiosError === true){
  47. console.log(blog.data)
  48. return Error('')
  49. }
  50. const { published, drafts } = separateBlogs(blogs)
  51. commit('setBlogs', {resource: 'drafts', items: drafts})
  52. commit('setBlogs', {resource: 'published', items: published})
  53. return { published, drafts }
  54. },
  55. async updateBlog({commit, state}, {data, id}) {
  56. console.log('instructor/blog.js actions updateBlog call axios.$patch-> Id, data')
  57. const blog = await this.$axios.$patch(`/api/v1/blogs/${id}`, data)
  58. if(blog.isAxiosError === true){
  59. console.log(blog.data)
  60. return Error('')
  61. }
  62. console.log('instructor/blog.js actions updateBlog done axios.$patch-> Id, data')
  63. console.log('courses.js mutations call setBlog')
  64. commit('setBlog', blog)
  65. console.log('courses.js mutations done setBlog')
  66. return state.item
  67. }
  68. }
  69. export const mutations = {
  70. setBlog(state, blog) {
  71. state.item = blog
  72. },
  73. setBlogs(state, {resource, items}) {
  74. state.items[resource] = items
  75. },
  76. setIsSaving(state, isSaving) {
  77. state.isSaving = isSaving
  78. }
  79. }