| 1 |
const $ = window.jQuery || jQuery; |
| 2 |
|
| 3 |
const CourseCurriculum = { |
| 4 |
toggleAllSections( context ) { |
| 5 |
const hidden = context.getters.isHiddenAllSections; |
| 6 |
|
| 7 |
if ( hidden ) { |
| 8 |
context.commit( 'OPEN_ALL_SECTIONS' ); |
| 9 |
} else { |
| 10 |
context.commit( 'CLOSE_ALL_SECTIONS' ); |
| 11 |
} |
| 12 |
|
| 13 |
LP.Request( { |
| 14 |
type: 'hidden-sections', |
| 15 |
hidden: context.getters.hiddenSections, |
| 16 |
} ); |
| 17 |
}, |
| 18 |
|
| 19 |
updateSectionsOrder( context, order ) { |
| 20 |
LP.Request( { |
| 21 |
type: 'sort-sections', |
| 22 |
order: JSON.stringify( order ), |
| 23 |
} ).then( |
| 24 |
function( response ) { |
| 25 |
const result = response.body; |
| 26 |
const order_sections = result.data; |
| 27 |
|
| 28 |
context.commit( 'SORT_SECTION', order_sections ); |
| 29 |
}, |
| 30 |
function( error ) { |
| 31 |
console.error( error ); |
| 32 |
} |
| 33 |
); |
| 34 |
}, |
| 35 |
|
| 36 |
toggleSection( context, section ) { |
| 37 |
if ( section.open ) { |
| 38 |
context.commit( 'CLOSE_SECTION', section ); |
| 39 |
} else { |
| 40 |
context.commit( 'OPEN_SECTION', section ); |
| 41 |
} |
| 42 |
|
| 43 |
LP.Request( { |
| 44 |
type: 'hidden-sections', |
| 45 |
hidden: context.getters.hiddenSections, |
| 46 |
} ); |
| 47 |
}, |
| 48 |
|
| 49 |
updateSection( context, section ) { |
| 50 |
context.commit( 'UPDATE_SECTION_REQUEST', section.id ); |
| 51 |
|
| 52 |
LP.Request( { |
| 53 |
type: 'update-section', |
| 54 |
section: JSON.stringify( section ), |
| 55 |
} ).then( function() { |
| 56 |
context.commit( 'UPDATE_SECTION_SUCCESS', section.id ); |
| 57 |
} ) |
| 58 |
.catch( function() { |
| 59 |
context.commit( 'UPDATE_SECTION_FAILURE', section.id ); |
| 60 |
} ); |
| 61 |
}, |
| 62 |
|
| 63 |
removeSection( context, payload ) { |
| 64 |
context.commit( 'REMOVE_SECTION', payload.index ); |
| 65 |
|
| 66 |
LP.Request( { |
| 67 |
type: 'remove-section', |
| 68 |
section_id: payload.section.id, |
| 69 |
} ).then( |
| 70 |
function( response ) { |
| 71 |
const result = response.body; |
| 72 |
}, |
| 73 |
function( error ) { |
| 74 |
console.error( error ); |
| 75 |
} |
| 76 |
); |
| 77 |
}, |
| 78 |
|
| 79 |
newSection( context, name ) { |
| 80 |
const newSection = { |
| 81 |
type: 'new-section', |
| 82 |
section_name: name, |
| 83 |
temp_id: LP.uniqueId(), |
| 84 |
}; |
| 85 |
context.commit( 'ADD_NEW_SECTION', { |
| 86 |
id: newSection.temp_id, |
| 87 |
items: [], |
| 88 |
open: false, |
| 89 |
title: newSection.section_name, |
| 90 |
} ); |
| 91 |
|
| 92 |
LP.Request( newSection ).then( |
| 93 |
function( response ) { |
| 94 |
const result = response.body; |
| 95 |
|
| 96 |
if ( result.success ) { |
| 97 |
const section = $.extend( {}, result.data, { open: true } ); |
| 98 |
context.commit( 'ADD_NEW_SECTION', section ); |
| 99 |
} |
| 100 |
}, |
| 101 |
function( error ) { |
| 102 |
console.error( error ); |
| 103 |
} |
| 104 |
); |
| 105 |
}, |
| 106 |
|
| 107 |
updateSectionItem( context, payload ) { |
| 108 |
context.commit( 'UPDATE_SECTION_ITEM_REQUEST', payload.item.id ); |
| 109 |
|
| 110 |
LP.Request( { |
| 111 |
type: 'update-section-item', |
| 112 |
section_id: payload.section_id, |
| 113 |
item: JSON.stringify( payload.item ), |
| 114 |
|
| 115 |
} ).then( |
| 116 |
function( response ) { |
| 117 |
context.commit( 'UPDATE_SECTION_ITEM_SUCCESS', payload.item.id ); |
| 118 |
|
| 119 |
const result = response.body; |
| 120 |
if ( result.success ) { |
| 121 |
const item = result.data; |
| 122 |
|
| 123 |
context.commit( 'UPDATE_SECTION_ITEM', { section_id: payload.section_id, item } ); |
| 124 |
} |
| 125 |
}, |
| 126 |
function( error ) { |
| 127 |
context.commit( 'UPDATE_SECTION_ITEM_FAILURE', payload.item.id ); |
| 128 |
console.error( error ); |
| 129 |
} |
| 130 |
); |
| 131 |
}, |
| 132 |
|
| 133 |
removeSectionItem( context, payload ) { |
| 134 |
const id = payload.item.id; |
| 135 |
context.commit( 'REMOVE_SECTION_ITEM', payload ); |
| 136 |
payload.item.temp_id = 0; |
| 137 |
LP.Request( { |
| 138 |
type: 'remove-section-item', |
| 139 |
section_id: payload.section_id, |
| 140 |
item_id: id, |
| 141 |
} ).then( |
| 142 |
function( rs ) { |
| 143 |
const { data, success } = rs.body; |
| 144 |
|
| 145 |
if ( success ) { |
| 146 |
context.commit( 'REMOVE_SECTION_ITEM', payload ); |
| 147 |
} else { |
| 148 |
alert( data ); |
| 149 |
payload.oldId = id; |
| 150 |
context.commit( 'REMOVE_SECTION_ITEM', payload ); |
| 151 |
} |
| 152 |
context.commit( 'REMOVE_SECTION_ITEM', payload ); |
| 153 |
} |
| 154 |
); |
| 155 |
}, |
| 156 |
|
| 157 |
deleteSectionItem( context, payload ) { |
| 158 |
const id = payload.item.id; |
| 159 |
context.commit( 'REMOVE_SECTION_ITEM', payload ); |
| 160 |
payload.item.temp_id = 0; |
| 161 |
LP.Request( { |
| 162 |
type: 'delete-section-item', |
| 163 |
section_id: payload.section_id, |
| 164 |
item_id: id, |
| 165 |
} ).then( |
| 166 |
function( rs ) { |
| 167 |
const { data, success } = rs.body; |
| 168 |
|
| 169 |
if ( success ) { |
| 170 |
context.commit( 'REMOVE_SECTION_ITEM', payload ); |
| 171 |
} else { |
| 172 |
alert( data ); |
| 173 |
payload.oldId = id; |
| 174 |
context.commit( 'REMOVE_SECTION_ITEM', payload ); |
| 175 |
} |
| 176 |
} |
| 177 |
); |
| 178 |
}, |
| 179 |
|
| 180 |
newSectionItem( context, payload ) { |
| 181 |
context.commit( 'APPEND_EMPTY_ITEM_TO_SECTION', payload ); |
| 182 |
//context.commit('UPDATE_SECTION_ITEMS', {section_id: payload.section_id, items: result.data}); |
| 183 |
LP.Request( { |
| 184 |
type: 'new-section-item', |
| 185 |
section_id: payload.section_id, |
| 186 |
item: JSON.stringify( payload.item ), |
| 187 |
} ).then( |
| 188 |
function( response ) { |
| 189 |
const result = response.body; |
| 190 |
|
| 191 |
if ( result.success ) { |
| 192 |
// context.commit('UPDATE_SECTION_ITEMS', {section_id: payload.section_id, items: result.data}); |
| 193 |
const items = {}; |
| 194 |
$.each( result.data, function( i, a ) { |
| 195 |
items[ a.old_id ? a.old_id : a.id ] = a; |
| 196 |
} ); |
| 197 |
|
| 198 |
context.commit( 'UPDATE_ITEM_SECTION_BY_ID', { |
| 199 |
section_id: payload.section_id, |
| 200 |
items, |
| 201 |
} ); |
| 202 |
} |
| 203 |
}, |
| 204 |
function( error ) { |
| 205 |
console.error( error ); |
| 206 |
} |
| 207 |
); |
| 208 |
}, |
| 209 |
|
| 210 |
updateSectionItems( { state }, payload ) { |
| 211 |
LP.Request( { |
| 212 |
type: 'update-section-items', |
| 213 |
section_id: payload.section_id, |
| 214 |
items: JSON.stringify( payload.items ), |
| 215 |
last_section: state.sections[ state.sections.length - 1 ] === ( payload.section_id ), |
| 216 |
} ).then( |
| 217 |
function( response ) { |
| 218 |
const result = response.body; |
| 219 |
|
| 220 |
if ( result.success ) { |
| 221 |
// console.log(result); |
| 222 |
} |
| 223 |
}, |
| 224 |
function( error ) { |
| 225 |
console.error( error ); |
| 226 |
} |
| 227 |
); |
| 228 |
}, |
| 229 |
}; |
| 230 |
|
| 231 |
export default CourseCurriculum; |
| 232 |
|