split fipamo api into two parts to handle admin and non admin requests

pull/20/head
Ro 5 years ago
parent 957a389b8b
commit 27338da86c

@ -1,9 +1,9 @@
import FipamoAPI from '../../libraries/FipamoAPI';
import FipamoAdminAPI from '../../libraries/FipamoAdminAPI';
import NavActions from '../actions/NavActions';
import * as DataEvent from '../events/DataEvent';
import Notifications from '../ui/Notifications';
const notify = new Notifications();
const api = new FipamoAPI();
const admin = new FipamoAdminAPI();
export default class NavIndex {
//--------------------------
// constructor
@ -18,7 +18,7 @@ export default class NavIndex {
Sortable.create(document.getElementById('nav-pages'), {
onUpdate: () => {
new NavActions().syncMenu().then(data => {
api.syncNav(data).then(r => {
admin.syncNav(data).then(r => {
if (r.type == DataEvent.MENU_UPDATED) {
notify.alert(r.message, true);
} else {
@ -44,7 +44,7 @@ export default class NavIndex {
new NavActions().removeItem(id);
new NavActions().syncMenu().then(data => {
data.remove = e.target.getAttribute('data-uuid');
api.syncNav(data).then(r => {
admin.syncNav(data).then(r => {
if (r.type == DataEvent.MENU_UPDATED) {
notify.alert(r.message, true);
} else {

@ -1,16 +1,16 @@
//TOOLS
import FipamoAPI, {
import FipamoAdminAPI, {
TASK_PAGE_CREATE,
TASK_PAGE_EDIT,
TASK_PAGE_DELETE
} from '../../libraries/FipamoAPI';
} from '../../libraries/FipamoAdminAPI';
import * as DataEvent from '../events/DataEvent';
import PageActions from '../actions/PageActions';
import * as EditorEvent from '../events/EditorEvent';
//import TinyDatePicker from 'tiny-date-picker';
import TextEditor from '../ui/TextEditor';
import Notfications from '../ui/Notifications';
const api = new FipamoAPI();
const admin = new FipamoAdminAPI();
const notify = new Notfications();
export default class PostEditor {
//--------------------------
@ -134,7 +134,8 @@ export default class PostEditor {
new PageActions()
.collectInfo(document.getElementById('featured-image-upload').files[0])
.then(page => {
api.pageActions(task, page)
admin
.pageActions(task, page)
.then(r => {
if (
r.type === DataEvent.PAGE_ERROR ||
@ -162,7 +163,8 @@ export default class PostEditor {
}
if (confirm("AYE! You know you're deleting this post, right?")) {
let id = { id: this.postUUID };
api.pageActions(TASK_PAGE_DELETE, id)
admin
.pageActions(TASK_PAGE_DELETE, id)
.then(() => {
window.location = '/@/dashboard/page/list/';
})
@ -222,7 +224,8 @@ export default class PostEditor {
}
handleImageUpload(type, files) {
let self = this;
api.imageUpload(type, files)
admin
.imageUpload(type, files)
.then(r => {
if (r.type == DataEvent.POST_IMAGE_ADDED)
self.editor.notify(EditorEvent.EDITOR_UPLOAD_POST_IMAGE, r.url);

@ -1,9 +1,9 @@
import SettingsActions from '../actions/SettingsActions';
import FipamoAPI from '../../libraries/FipamoAPI';
import FipamoAdminAPI from '../../libraries/FipamoAdminAPI';
import * as DataEvent from '../../../src/com/events/DataEvent';
import Mailer from '../actions/Mailer';
import Notifications from '../ui/Notifications';
const api = new FipamoAPI();
const admin = new FipamoAdminAPI();
const notify = new Notifications();
const mailer = new Mailer();
export default class SettingsIndex {
@ -23,7 +23,7 @@ export default class SettingsIndex {
new SettingsActions()
.getInfo()
.then(data => {
api.syncSettings(data).then(r => {
admin.syncSettings(data).then(r => {
if (r.type == DataEvent.SETTINGS_UPDATED) {
notify.alert(r.message, true);
} else {
@ -141,7 +141,8 @@ export default class SettingsIndex {
}
}
handleImageUpload(type, files) {
api.imageUpload(type, files)
admin
.imageUpload(type, files)
.then(r => {
if (r.type == DataEvent.AVATAR_UPLOADED) {
notify.alert(r.message, true);
@ -159,7 +160,8 @@ export default class SettingsIndex {
e.preventDefault();
e.stopPropagation();
let task = { task: 'publish all pages' };
api.publishSite(task)
admin
.publishSite(task)
.then(r => {
notify.alert(r.message, true);
})

@ -2,42 +2,17 @@ export const REQUEST_TYPE_POST = 'POST';
export const REQUEST_TYPE_GET = 'GET';
export const REQUEST_TYPE_PUT = 'PUT';
export const REQUEST_TYPE_DELETE = 'DELETE';
export const TASK_PAGE_CREATE = 'createNewPage';
export const TASK_PAGE_EDIT = 'editPage';
export const TASK_PAGE_DELETE = 'deletePage';
export const CONTENT_TYPE_JSON = 'json';
export const CONTENT_TYPE_FORM = 'x-www-form-urlencoded';
export const API_STATUS = '/api/v1/auth/status';
export const API_INIT = '/api/v1/auth/init';
export const API_LOGIN = '/api/v1/auth/login';
export const API_GET_NAV = '/api/settings/nav';
export const API_NEW_PAGE = '/api/v1/page/write/new';
export const API_EDIT_PAGE = '/api/v1/page/write';
export const API_DELETE_PAGE = '/api/v1/page/delete';
export const API_IMAGE_UPLOAD = '/api/v1/page/add-post-image';
export const API_SETTINGS_SYNC = '/api/v1/settings/sync';
export const API_UPLOAD_AVATAR = '/api/v1/settings/add-avatar';
export const API_UPLOAD_BACKGROUND = '/api/v1/settings/add-feature-background';
export const API_PUBLISH_PAGES = '/api/v1/settings/publish-pages';
export const API_NAV_SYNC = '/api/v1/settings/nav-sync';
import * as DataEvent from '../com/events/DataEvent';
export default class APIUtils {
//--------------------------
// constructor
//--------------------------
constructor() {
this.percentComplete = 0;
this.token = null;
//checks backend to see if user is logged in
//and requests encrypted token for api calls
this._request(API_STATUS).then(response => {
if (response.type === DataEvent.API_REQUEST_GOOD) {
this.token = response.token;
} else {
//don't set token
}
});
}
constructor() {}
//--------------------------
// public
//--------------------------
@ -69,137 +44,6 @@ export default class APIUtils {
});
});
}
syncSettings(data) {
return new Promise((resolve, reject) => {
this._request(
API_SETTINGS_SYNC,
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then(result => {
resolve(result);
})
.catch(err => {
reject(err);
});
});
}
imageUpload(type, files) {
return new Promise((resolve, reject) => {
let url = '';
switch (type) {
case 'avatar-upload':
url = API_UPLOAD_AVATAR;
break;
case 'background-upload':
url = API_UPLOAD_BACKGROUND;
break;
default:
url = API_IMAGE_UPLOAD;
break;
}
var imageData = new FormData();
for (var i = 0; i < files.length; i++) {
var file = files[i];
// Check the file type.
if (!file.type.match('image.*')) {
continue;
}
if (type === 'avatar-upload') {
imageData.append('avatar_upload', file, file.name);
} else if (type === 'background-upload') {
imageData.append('background_upload', file, file.name);
} else {
imageData.append('post_image', file, file.name);
}
}
this._request(
url,
DataEvent.API_IMAGES_UPLOAD,
REQUEST_TYPE_POST,
CONTENT_TYPE_FORM,
imageData
)
.then(r => {
resolve(r);
})
.catch(err => {
reject(err);
});
});
}
publishSite(data) {
return new Promise((resolve, reject) => {
this._request(
API_PUBLISH_PAGES,
DataEvent.API_RENDER_PAGES,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then(result => {
resolve(result);
})
.catch(err => {
reject(err);
});
});
}
pageActions(task, data) {
let url, event, content;
switch (task) {
case TASK_PAGE_CREATE:
url = API_NEW_PAGE;
event = DataEvent.API_PAGE_WRITE;
content = CONTENT_TYPE_FORM;
break;
case TASK_PAGE_EDIT:
url = API_EDIT_PAGE;
event = DataEvent.API_PAGE_WRITE;
content = CONTENT_TYPE_FORM;
break;
case TASK_PAGE_DELETE:
url = API_DELETE_PAGE;
event = DataEvent.API_PAGE_DELETE;
content = CONTENT_TYPE_JSON;
break;
default:
break;
}
return new Promise((resolve, reject) => {
this._request(url, event, REQUEST_TYPE_POST, content, data)
.then(result => {
resolve(result);
})
.catch(err => {
reject(err);
});
});
}
syncNav(data) {
return new Promise((resolve, reject) => {
this._request(
API_NAV_SYNC,
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then(result => {
resolve(result);
})
.catch(err => {
reject(err);
});
});
}
//--------------------------
// private
//--------------------------
@ -225,15 +69,6 @@ export default class APIUtils {
}
};
if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) {
if (
eventType === DataEvent.API_PAGE_WRITE ||
eventType === DataEvent.API_IMAGES_UPLOAD ||
eventType === DataEvent.API_SETTINGS_WRITE ||
eventType === DataEvent.API_PAGE_DELETE ||
eventType === DataEvent.API_RENDER_PAGES
)
request.setRequestHeader('x-access-token', self.token);
switch (contentType) {
case CONTENT_TYPE_JSON:
request.setRequestHeader('Content-type', 'application/' + contentType);

@ -0,0 +1,229 @@
export const REQUEST_TYPE_POST = 'POST';
export const REQUEST_TYPE_GET = 'GET';
export const REQUEST_TYPE_PUT = 'PUT';
export const REQUEST_TYPE_DELETE = 'DELETE';
export const TASK_PAGE_CREATE = 'createNewPage';
export const TASK_PAGE_EDIT = 'editPage';
export const TASK_PAGE_DELETE = 'deletePage';
export const CONTENT_TYPE_JSON = 'json';
export const CONTENT_TYPE_FORM = 'x-www-form-urlencoded';
export const API_STATUS = '/api/v1/auth/status';
export const API_GET_NAV = '/api/settings/nav';
export const API_NEW_PAGE = '/api/v1/page/write/new';
export const API_EDIT_PAGE = '/api/v1/page/write';
export const API_DELETE_PAGE = '/api/v1/page/delete';
export const API_IMAGE_UPLOAD = '/api/v1/page/add-post-image';
export const API_SETTINGS_SYNC = '/api/v1/settings/sync';
export const API_UPLOAD_AVATAR = '/api/v1/settings/add-avatar';
export const API_UPLOAD_BACKGROUND = '/api/v1/settings/add-feature-background';
export const API_PUBLISH_PAGES = '/api/v1/settings/publish-pages';
export const API_NAV_SYNC = '/api/v1/settings/nav-sync';
import * as DataEvent from '../com/events/DataEvent';
export default class APIUtils {
//--------------------------
// constructor
//--------------------------
constructor() {
this.percentComplete = 0;
this.token = null;
//checks backend to see if user is logged in
//and requests encrypted token for api calls
this._request(API_STATUS).then(response => {
if (response.type === DataEvent.API_REQUEST_GOOD) {
this.token = response.token;
} else {
//don't set token
}
});
}
//--------------------------
// public
//--------------------------
syncSettings(data) {
return new Promise((resolve, reject) => {
this._request(
API_SETTINGS_SYNC,
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then(result => {
resolve(result);
})
.catch(err => {
reject(err);
});
});
}
imageUpload(type, files) {
return new Promise((resolve, reject) => {
let url = '';
switch (type) {
case 'avatar-upload':
url = API_UPLOAD_AVATAR;
break;
case 'background-upload':
url = API_UPLOAD_BACKGROUND;
break;
default:
url = API_IMAGE_UPLOAD;
break;
}
var imageData = new FormData();
for (var i = 0; i < files.length; i++) {
var file = files[i];
// Check the file type.
if (!file.type.match('image.*')) {
continue;
}
if (type === 'avatar-upload') {
imageData.append('avatar_upload', file, file.name);
} else if (type === 'background-upload') {
imageData.append('background_upload', file, file.name);
} else {
imageData.append('post_image', file, file.name);
}
}
this._request(
url,
DataEvent.API_IMAGES_UPLOAD,
REQUEST_TYPE_POST,
CONTENT_TYPE_FORM,
imageData
)
.then(r => {
resolve(r);
})
.catch(err => {
reject(err);
});
});
}
publishSite(data) {
return new Promise((resolve, reject) => {
this._request(
API_PUBLISH_PAGES,
DataEvent.API_RENDER_PAGES,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then(result => {
resolve(result);
})
.catch(err => {
reject(err);
});
});
}
pageActions(task, data) {
let url, event, content;
switch (task) {
case TASK_PAGE_CREATE:
url = API_NEW_PAGE;
event = DataEvent.API_PAGE_WRITE;
content = CONTENT_TYPE_FORM;
break;
case TASK_PAGE_EDIT:
url = API_EDIT_PAGE;
event = DataEvent.API_PAGE_WRITE;
content = CONTENT_TYPE_FORM;
break;
case TASK_PAGE_DELETE:
url = API_DELETE_PAGE;
event = DataEvent.API_PAGE_DELETE;
content = CONTENT_TYPE_JSON;
break;
default:
break;
}
return new Promise((resolve, reject) => {
this._request(url, event, REQUEST_TYPE_POST, content, data)
.then(result => {
resolve(result);
})
.catch(err => {
reject(err);
});
});
}
syncNav(data) {
return new Promise((resolve, reject) => {
this._request(
API_NAV_SYNC,
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then(result => {
resolve(result);
})
.catch(err => {
reject(err);
});
});
}
//--------------------------
// private
//--------------------------
_request(
requestURL,
eventType,
requestType = REQUEST_TYPE_GET,
contentType = CONTENT_TYPE_JSON,
requestData = null
) {
var self = this;
return new Promise(function (resolve, reject) {
var request = new XMLHttpRequest();
request.upload.onprogress = self.handleLoadProgress;
request.open(requestType, requestURL, true);
request.onload = () => {
if (request.status == 200) {
let response = JSON.parse(request['response']);
resolve(response);
} else {
let error = JSON.parse(request['response']);
reject(error);
}
};
if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) {
if (
eventType === DataEvent.API_PAGE_WRITE ||
eventType === DataEvent.API_IMAGES_UPLOAD ||
eventType === DataEvent.API_SETTINGS_WRITE ||
eventType === DataEvent.API_PAGE_DELETE ||
eventType === DataEvent.API_RENDER_PAGES
)
request.setRequestHeader('x-access-token', self.token);
switch (contentType) {
case CONTENT_TYPE_JSON:
request.setRequestHeader('Content-type', 'application/' + contentType);
request.send(JSON.stringify(requestData));
break;
case CONTENT_TYPE_FORM:
request.send(requestData);
break;
}
} else {
request.send();
}
});
}
//--------------------------
// event handlers
//--------------------------
handleLoadProgress(e) {
this.percentComplete = Math.ceil((e.loaded / e.total) * 100);
//pass element to display request progress
}
}
Loading…
Cancel
Save