blog.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. export const state = () => ({
  2. item: {},
  3. isSaving: false
  4. })
  5. export const actions = {
  6. async createBlog(_, blogData) {
  7. try {
  8. commit('setIsSaving', true)
  9. console.log('instructor/blog.js actions createBlog call axios.$post->blogData')
  10. const blog = await this.$axios.$post('/api/v1/blogs', blogData)
  11. console.log('instructor/blog.js actions createBlog done axios.$post->blogData')
  12. commit('setIsSaving', false)
  13. return blog
  14. } catch (error) {
  15. commit('setIsSaving', false)
  16. return error
  17. }
  18. },
  19. async fetchBlogById({commit}, blogId) {
  20. try {
  21. console.log('instructor/blog.js actions fetchBlogById call axios.$get->blogId')
  22. const blog = await this.$axios.$get(`/api/v1/blogs/${blogId}`)
  23. console.log('instructor/blog.js actions fetchBlogById done axios.$get->blogId')
  24. console.log('courses.js mutations call setBlog')
  25. commit('setBlog', blog)
  26. console.log('courses.js mutations done setBlog')
  27. } catch (error) {
  28. return error
  29. }
  30. },
  31. async updateBlog({commit, state}, {data, id}) {
  32. try {
  33. console.log('instructor/blog.js actions updateBlog call axios.$patch-> Id, data')
  34. const blog = await this.$axios.$patch(`/api/v1/blogs/${id}`, data)
  35. console.log('instructor/blog.js actions updateBlog done axios.$patch-> Id, data')
  36. console.log('courses.js mutations call setBlog')
  37. commit('setBlog', blog)
  38. console.log('courses.js mutations done setBlog')
  39. return state.item
  40. } catch (error) {
  41. return error
  42. }
  43. }
  44. }
  45. export const mutations = {
  46. setBlog(state, blog) {
  47. state.item = blog
  48. },
  49. setIsSaving(state, isSaving) {
  50. state.isSaving = isSaving
  51. }
  52. }