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.
214 lines
8.6 KiB
214 lines
8.6 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}`,
|
|
}
|
|
});
|
|
},
|
|
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.cover_id ? courseObject.cover_id : null,
|
|
content: courseObject.blocks.map((block) => {
|
|
if (block.type === 'text') {
|
|
return {
|
|
'type': 'text',
|
|
'data': {
|
|
'id': block.data.id ? block.data.id : null,
|
|
'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,
|
|
'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,
|
|
'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,
|
|
'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,
|
|
'title': block.data.title,
|
|
'url': block.data.video_url,
|
|
}
|
|
}
|
|
}
|
|
}),
|
|
};
|
|
|
|
console.log(courseJson);
|
|
|
|
let request;
|
|
if (isAdding) {
|
|
return api.addCourse(courseJson, accessToken);
|
|
} else {
|
|
return api.updateCourse(courseObject.id, courseJson, accessToken);
|
|
}
|
|
},
|
|
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,
|
|
price: courseJSON.price,
|
|
is_paid: 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,
|
|
cover_id: courseJSON.cover.id,
|
|
cover_url: courseJSON.cover.image,
|
|
content: courseJSON.content.map((contentItem) => {
|
|
if (contentItem.type === 'text') {
|
|
return {
|
|
'type': 'text',
|
|
'data': {
|
|
'id': contentItem.data.id ? contentItem.data.id : null,
|
|
'title': contentItem.data.title,
|
|
'text': contentItem.data.txt,
|
|
}
|
|
}
|
|
} else if (contentItem.type === 'image') {
|
|
return {
|
|
'type': 'image',
|
|
'data': {
|
|
'id': contentItem.data.id ? contentItem.data.id : null,
|
|
'title': contentItem.data.title,
|
|
'image_id': contentItem.data.image.id,
|
|
'image_url': contentItem.data.image.image,
|
|
}
|
|
}
|
|
} else if (contentItem.type === 'image-text') {
|
|
return {
|
|
'type': 'image-text',
|
|
'data': {
|
|
'id': contentItem.data.id ? contentItem.data.id : null,
|
|
'title': contentItem.data.title,
|
|
'image_id': contentItem.data.image.id,
|
|
'image_url': contentItem.data.image.image,
|
|
'text': contentItem.data.txt,
|
|
}
|
|
}
|
|
} else if (contentItem.type === 'images') {
|
|
return {
|
|
'type': 'images',
|
|
'data': {
|
|
'id': contentItem.data.id ? contentItem.data.id : null,
|
|
'title': contentItem.data.title,
|
|
'images': contentItem.data.images.map((galleryImage) => {
|
|
return {
|
|
'id': galleryImage.id ? galleryImage.id : null,
|
|
'image_id': galleryImage.image.id,
|
|
'image_url': galleryImage.image.url,
|
|
}
|
|
}),
|
|
}
|
|
}
|
|
} else if (contentItem.type === 'video') {
|
|
return {
|
|
'type': 'video',
|
|
'data': {
|
|
'id': contentItem.data.id ? contentItem.data.id : null,
|
|
'title': contentItem.data.title,
|
|
'video_url': contentItem.data.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}`,
|
|
}
|
|
});
|
|
},
|
|
}; |