course.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. export const state = () => ({
  2. items : [],
  3. item : []
  4. })
  5. export const actions = {
  6. async fetchInstructorCourses({commit}) {
  7. try{
  8. console.log('courses.js actions call fetchInstructorCourses')
  9. const courses = await this.$axios.$get('/api/v1/products/user-products')
  10. console.log('courses.js actions done fetchInstructorCourses')
  11. console.log('courses.js mutations call setCourses')
  12. commit('setCourses', courses)
  13. console.log('courses.js mutations done setCourses')
  14. return courses
  15. }catch(error){
  16. return error
  17. }
  18. },
  19. async createCourse(_, courseData) {
  20. try {
  21. console.log('instructor/course.js actions createCourse call axios.$post->products')
  22. const result = await this.$axios.$post('/api/v1/products', courseData)
  23. console.log('instructor/course.js actions createCourse done axios.$post->products')
  24. } catch (error) {
  25. return error
  26. }
  27. },
  28. async fetchCourseById({commit, state}, courseId){
  29. try {
  30. console.log('instructor/course.js actions fetchCourseById call axios.$get->courseId')
  31. const course = await this.$axios.$get(`/api/v1/products/${courseId}`)
  32. console.log('instructor/course.js actions fetchCourseById done axios.$get->courseId')
  33. console.log('courses.js mutations call setCourse')
  34. commit('setCourse', course)
  35. console.log('courses.js mutations done setCourse')
  36. return state.item
  37. } catch (error) {
  38. return error
  39. }
  40. },
  41. updateLine({commit}, {index, value, field}) {
  42. commit('setLineValue', {index, value, field})
  43. // Surprise commit for next lectures (:
  44. },
  45. updateCourseValue({commit}, {value, field}) {
  46. commit('setCourseValue', {value, field})
  47. },
  48. async updateCourse({state, commit}) {
  49. try {
  50. console.log('instructor/course.js actions updateCourse call axios.$patch->course')
  51. const course = await this.$axios.$patch(`/api/v1/products/${state.item._id}`, state.item)
  52. console.log('instructor/course.js actions updateCourse done axios.$patch->course')
  53. console.log('courses.js mutations call setCourse')
  54. commit('setCourse', course)
  55. console.log('courses.js mutations done setCourse')
  56. return state.item
  57. } catch (error) {
  58. return error
  59. }
  60. },
  61. }
  62. export const mutations = {
  63. setCourses(state, courses) {
  64. state.items = courses
  65. },
  66. setCourse(state, course) {
  67. state.item = course
  68. },
  69. addLine(state, field) {
  70. console.log('course.js mutations call addLine params > ', field)
  71. state.item[field].push({value: ''})
  72. console.log('course.js mutations done addLine params')
  73. },
  74. removeLine(state, {field, index}) {
  75. console.log('course.js mutations call removeLine params > ', field, ':', index)
  76. state.item[field].splice(index, 1)
  77. console.log('course.js mutations done removeLine params')
  78. },
  79. setLineValue(state, {index, value, field}) {
  80. console.log('course.js mutations call setLineValue params > ', {index, value, field})
  81. state.item[field][index].value = value
  82. console.log('course.js mutations done setLineValue params')
  83. },
  84. setCourseValue(state, {value, field}){
  85. state.item[field] = value
  86. }
  87. }