You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
375 lines
14 KiB
375 lines
14 KiB
import axios from 'axios';
|
|
import moment from 'moment';
|
|
|
|
axios.defaults.headers.post['Content-Type'] = 'application/json';
|
|
axios.defaults.headers.post['Accept'] = 'application/json';
|
|
|
|
export const api = {
|
|
getCategories: (accessToken) => {
|
|
return axios.get('/api/v1/categories/', {
|
|
headers: {
|
|
'Authorization': `Token ${accessToken}`,
|
|
}
|
|
});
|
|
},
|
|
uploadImage: (imageData, accessToken) => {
|
|
return axios.post('/api/v1/image-objects/', {
|
|
image: imageData,
|
|
}, {
|
|
headers: {
|
|
'Authorization': `Token ${accessToken}`,
|
|
}
|
|
});
|
|
},
|
|
removeImage: (imageId, accessToken) => {
|
|
return axios.delete(`/api/v1/image-objects/${imageId}/`, {
|
|
headers: {
|
|
'Authorization': `Token ${accessToken}`,
|
|
}
|
|
});
|
|
},
|
|
loadCourse: (courseId, accessToken) => {
|
|
return axios.get(`/api/v1/courses/${courseId}/`, {
|
|
headers: {
|
|
'Authorization': `Token ${accessToken}`,
|
|
}
|
|
});
|
|
},
|
|
saveCourse: (courseObject, accessToken) => {
|
|
const isAdding = (!courseObject.hasOwnProperty('id') || !courseObject.hasOwnProperty('id'));
|
|
|
|
let deferredStart = null;
|
|
if (courseObject.is_deferred) {
|
|
let deferredStartTime = moment(courseObject.time.value, "HH:mm");
|
|
let deferredStartDate = moment(courseObject.date).hour(deferredStartTime.hour());
|
|
deferredStart = deferredStartDate.format();
|
|
}
|
|
|
|
const courseJson = {
|
|
title: courseObject.title,
|
|
short_description: courseObject.short_description,
|
|
category: courseObject.category,
|
|
price: courseObject.is_paid ? courseObject.price : 0,
|
|
deferred_start_at: deferredStart,
|
|
duration: courseObject.duration,
|
|
is_featured: courseObject.is_featured,
|
|
url: courseObject.url,
|
|
cover: courseObject.coverImageId ? courseObject.coverImageId : null,
|
|
gallery: {
|
|
gallery_images: courseObject.gallery && courseObject.gallery.images ? courseObject.gallery.images : []
|
|
},
|
|
content: courseObject.content.map((block, index) => {
|
|
if (block.type === 'text') {
|
|
return {
|
|
'type': 'text',
|
|
'data': {
|
|
'id': block.data.id ? block.data.id : null,
|
|
'position': ++index,
|
|
'title': block.data.title,
|
|
'txt': block.data.text,
|
|
}
|
|
}
|
|
} else if (block.type === 'image') {
|
|
return {
|
|
'type': 'image',
|
|
'data': {
|
|
'id': block.data.id ? block.data.id : null,
|
|
'position': ++index,
|
|
'title': block.data.title,
|
|
'img': block.data.image_id,
|
|
}
|
|
}
|
|
} else if (block.type === 'image-text') {
|
|
return {
|
|
'type': 'image-text',
|
|
'data': {
|
|
'id': block.data.id ? block.data.id : null,
|
|
'position': ++index,
|
|
'title': block.data.title,
|
|
'img': block.data.image_id,
|
|
'txt': block.data.text,
|
|
}
|
|
}
|
|
} else if (block.type === 'images') {
|
|
return {
|
|
'type': 'images',
|
|
'data': {
|
|
'id': block.data.id ? block.data.id : null,
|
|
'position': ++index,
|
|
'title': block.data.title,
|
|
'images': block.data.images.map((galleryImage) => {
|
|
return {
|
|
'id': galleryImage.id ? galleryImage.id : null,
|
|
'img': galleryImage.img,
|
|
}
|
|
}),
|
|
}
|
|
}
|
|
} else if (block.type === 'video') {
|
|
return {
|
|
'type': 'video',
|
|
'data': {
|
|
'id': block.data.id ? block.data.id : null,
|
|
'position': ++index,
|
|
'title': block.data.title,
|
|
'url': block.data.video_url,
|
|
}
|
|
}
|
|
}
|
|
}),
|
|
};
|
|
|
|
if (isAdding) {
|
|
return api.addCourse(courseJson, accessToken);
|
|
} else {
|
|
return api.updateCourse(courseObject.id, courseJson, accessToken);
|
|
}
|
|
},
|
|
saveLesson: (lessonObject, accessToken) => {
|
|
const isAdding = (!lessonObject.hasOwnProperty('id') || !lessonObject.hasOwnProperty('id'));
|
|
|
|
const lessonJson = {
|
|
title: lessonObject.title,
|
|
short_description: lessonObject.short_description,
|
|
course: lessonObject.course_id,
|
|
content: lessonObject.content.map((block, index) => {
|
|
if (block.type === 'text') {
|
|
return {
|
|
'type': 'text',
|
|
'data': {
|
|
'id': block.data.id ? block.data.id : null,
|
|
'position': ++index,
|
|
'title': block.data.title,
|
|
'txt': block.data.text,
|
|
}
|
|
}
|
|
} else if (block.type === 'image') {
|
|
return {
|
|
'type': 'image',
|
|
'data': {
|
|
'id': block.data.id ? block.data.id : null,
|
|
'position': ++index,
|
|
'title': block.data.title,
|
|
'img': block.data.image_id,
|
|
}
|
|
}
|
|
} else if (block.type === 'image-text') {
|
|
return {
|
|
'type': 'image-text',
|
|
'data': {
|
|
'id': block.data.id ? block.data.id : null,
|
|
'position': ++index,
|
|
'title': block.data.title,
|
|
'img': block.data.image_id,
|
|
'txt': block.data.text,
|
|
}
|
|
}
|
|
} else if (block.type === 'images') {
|
|
return {
|
|
'type': 'images',
|
|
'data': {
|
|
'id': block.data.id ? block.data.id : null,
|
|
'position': ++index,
|
|
'title': block.data.title,
|
|
'images': block.data.images.map((galleryImage) => {
|
|
return {
|
|
'id': galleryImage.id ? galleryImage.id : null,
|
|
'img': galleryImage.img,
|
|
}
|
|
}),
|
|
}
|
|
}
|
|
} else if (block.type === 'video') {
|
|
return {
|
|
'type': 'video',
|
|
'data': {
|
|
'id': block.data.id ? block.data.id : null,
|
|
'position': ++index,
|
|
'title': block.data.title,
|
|
'url': block.data.video_url,
|
|
}
|
|
}
|
|
}
|
|
}),
|
|
};
|
|
|
|
if (isAdding) {
|
|
return api.addLesson(lessonJson, accessToken);
|
|
} else {
|
|
return api.updateLesson(lessonObject.id, lessonJson, accessToken);
|
|
}
|
|
},
|
|
convertLessonJson: (lessonJSON) => {
|
|
return {
|
|
id: lessonJSON.id,
|
|
title: lessonJSON.title,
|
|
short_description: lessonJSON.short_description,
|
|
content: api.convertContentResponse(lessonJSON.content)
|
|
}
|
|
},
|
|
convertCourseJson: (courseJSON) => {
|
|
let isDeferred = false;
|
|
let deferredDate = '';
|
|
let deferredTime = '';
|
|
if (courseJSON.deferred_start_at) {
|
|
let deferredDateTime = moment(courseJSON.deferred_start_at);
|
|
isDeferred = true;
|
|
deferredDate = deferredDateTime.format('MM-DD-YYYY');
|
|
deferredTime = deferredDateTime.format('HH:mm');
|
|
}
|
|
return {
|
|
id: courseJSON.id,
|
|
title: courseJSON.title,
|
|
short_description: courseJSON.short_description,
|
|
category: courseJSON.category.id ? courseJSON.category.id : courseJSON.category,
|
|
price: parseFloat(courseJSON.price),
|
|
is_paid: parseFloat(courseJSON.price) > 0,
|
|
is_deferred: isDeferred,
|
|
date: deferredDate,
|
|
time: deferredTime ? {title: deferredTime, value: deferredTime} : null,
|
|
duration: courseJSON.duration,
|
|
is_featured: courseJSON.is_featured,
|
|
url: courseJSON.url,
|
|
coverImageId: courseJSON.cover && courseJSON.cover.id ? courseJSON.cover.id : null,
|
|
coverImage: courseJSON.cover && courseJSON.cover.image ? courseJSON.cover.image : null,
|
|
content: api.convertContentResponse(courseJSON.content),
|
|
}
|
|
},
|
|
convertContentResponse: (contentJson) => {
|
|
return contentJson.sort((a, b) => {
|
|
if (a.position < b.position) {
|
|
return -1;
|
|
}
|
|
if (a.position > b.position) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}).map((contentItem) => {
|
|
if (contentItem.type === 'text') {
|
|
return {
|
|
'type': 'text',
|
|
'data': {
|
|
'id': contentItem.id ? contentItem.id : null,
|
|
'title': contentItem.title,
|
|
'text': contentItem.txt,
|
|
}
|
|
}
|
|
} else if (contentItem.type === 'image') {
|
|
return {
|
|
'type': 'image',
|
|
'data': {
|
|
'id': contentItem.id ? contentItem.id : null,
|
|
'title': contentItem.title,
|
|
'image_id': contentItem.img.id,
|
|
'image_url': contentItem.img.image,
|
|
}
|
|
}
|
|
} else if (contentItem.type === 'image-text') {
|
|
return {
|
|
'type': 'image-text',
|
|
'data': {
|
|
'id': contentItem.id ? contentItem.id : null,
|
|
'title': contentItem.title,
|
|
'image_id': contentItem.img.id,
|
|
'image_url': contentItem.img.image,
|
|
'text': contentItem.txt,
|
|
}
|
|
}
|
|
} else if (contentItem.type === 'images') {
|
|
return {
|
|
'type': 'images',
|
|
'data': {
|
|
'id': contentItem.id ? contentItem.id : null,
|
|
'title': contentItem.title,
|
|
'images': contentItem.gallery_images.map((galleryImage) => {
|
|
return {
|
|
'id': galleryImage.id ? galleryImage.id : null,
|
|
'img': galleryImage.img.id,
|
|
'src': galleryImage.img.image,
|
|
}
|
|
}),
|
|
}
|
|
}
|
|
} else if (contentItem.type === 'video') {
|
|
return {
|
|
'type': 'video',
|
|
'data': {
|
|
'id': contentItem.id ? contentItem.id : null,
|
|
'title': contentItem.title,
|
|
'video_url': contentItem.url,
|
|
}
|
|
}
|
|
}
|
|
});
|
|
},
|
|
addCourse: (courseJson, accessToken) => {
|
|
return axios.post('/api/v1/courses/', courseJson, {
|
|
headers: {
|
|
'Authorization': `Token ${accessToken}`,
|
|
}
|
|
});
|
|
},
|
|
updateCourse: (courseId, courseJson, accessToken) => {
|
|
return axios.put(`/api/v1/courses/${courseId}/`, courseJson, {
|
|
headers: {
|
|
'Authorization': `Token ${accessToken}`,
|
|
}
|
|
});
|
|
},
|
|
getCourseLessons: (courseId, accessToken) => {
|
|
return axios.get(`/api/v1/lessons/?course=${courseId}`, {
|
|
headers: {
|
|
'Authorization': `Token ${accessToken}`,
|
|
}
|
|
});
|
|
},
|
|
removeCourseLesson: (lessonId, accessToken) => {
|
|
return axios.delete(`/api/v1/lessons/${lessonId}/`, {
|
|
headers: {
|
|
'Authorization': `Token ${accessToken}`,
|
|
}
|
|
});
|
|
},
|
|
addLesson: (lessonJson, accessToken) => {
|
|
return axios.post('/api/v1/lessons/', lessonJson, {
|
|
headers: {
|
|
'Authorization': `Token ${accessToken}`,
|
|
}
|
|
});
|
|
},
|
|
updateLesson: (lessonId, lessonJson, accessToken) => {
|
|
return axios.put(`/api/v1/lessons/${lessonId}/`, lessonJson, {
|
|
headers: {
|
|
'Authorization': `Token ${accessToken}`,
|
|
}
|
|
});
|
|
},
|
|
removeContentBlock: (blockData, accessToken) => {
|
|
let removeUrl;
|
|
switch(blockData.type) {
|
|
case 'text':
|
|
removeUrl = `/api/v1/texts/${blockData.data.id}/`;
|
|
break;
|
|
case 'image':
|
|
removeUrl = `/api/v1/images/${blockData.data.id}/`;
|
|
break;
|
|
case 'image-text':
|
|
removeUrl = `/api/v1/image-texts/${blockData.data.id}/`;
|
|
break;
|
|
case 'images':
|
|
removeUrl = `/api/v1/galleries/${blockData.data.id}/`;
|
|
break;
|
|
case 'video':
|
|
removeUrl = `/api/v1/videos/${blockData.data.id}/`;
|
|
break;
|
|
}
|
|
if (!removeUrl) {
|
|
return;
|
|
}
|
|
return axios.delete(removeUrl, {
|
|
headers: {
|
|
'Authorization': `Token ${accessToken}`,
|
|
}
|
|
});
|
|
},
|
|
}; |