created Fipamo API Library, moved all API urls to API class

pull/20/head
Ro 5 years ago
parent ca6e9e488b
commit 36d6631df9

@ -1,10 +1,10 @@
import ApiUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from './utils/APIUtils';
import FipamoApi, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON, API_LOGIN } from '../libraries/FipamoAPI';
import DataUitls from './utils/DataUtils';
import * as DataEvent from './events/DataEvent';
import DashManager from './controllers/DashManager';
import Notfications from './ui/Notifications';
const api = new ApiUtils();
const api = new FipamoApi();
const data = new DataUitls();
const notify = new Notfications();
@ -25,7 +25,7 @@ export default class Base {
.getElementById('login-btn')
.addEventListener('click', e => this.handleLogin(e));
} else {
let manager = new DashManager();
new DashManager();
}
}
//--------------------------
@ -36,7 +36,7 @@ export default class Base {
e.preventDefault();
let authForm = data.formDataToJSON(document.getElementById('login'));
api.request(
'/api/v1/auth/login',
API_LOGIN,
DataEvent.AUTH_STATUS,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,

@ -22,6 +22,11 @@ export default class NavActions {
resolve(navData);
});
}
removeItem(id) {
document.getElementById('nav-pages').removeChild(document.getElementById(id));
}
//--------------------------
// event handlers
//--------------------------

@ -1,7 +1,12 @@
import ApiUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from '../utils/APIUtils';
import FipamoAPI, {
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
API_GET_NAV,
API_NAV_SYNC
} from '../../libraries/FipamoAPI';
import StringUtils from '../utils/StringUtils';
import * as DataEvent from '../events/DataEvent';
const api = new ApiUtils();
const api = new FipamoAPI();
export default class PostActions {
//--------------------------
// constructor
@ -72,7 +77,7 @@ export default class PostActions {
}
updateNav(add, id, post) {
api.request('/api/settings/nav', DataEvent.SETTINGS_LOADED)
api.request(API_GET_NAV, DataEvent.SETTINGS_LOADED)
.then(response => {
let menu = JSON.parse(response.request['response']);
let item = {
@ -91,7 +96,7 @@ export default class PostActions {
}
}
api.request(
'/api/settings/nav-sync',
API_NAV_SYNC,
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,

@ -1,9 +1,13 @@
import ApiUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from '../../../src/com/utils/APIUtils';
import FipamoAPI, {
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
API_NAV_SYNC
} from '../../libraries/FipamoAPI';
import NavActions from '../actions/NavActions';
import * as DataEvent from '../events/DataEvent';
import Notifications from '../ui/Notifications';
const notify = new Notifications();
const api = new ApiUtils();
const api = new FipamoAPI();
export default class NavIndex {
//--------------------------
// constructor
@ -19,7 +23,7 @@ export default class NavIndex {
onUpdate: () => {
new NavActions().syncMenu().then(data => {
api.request(
'/api/v1/settings/nav-sync',
API_NAV_SYNC,
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
@ -49,6 +53,22 @@ export default class NavIndex {
case 'remove-item':
id = e.target.getAttribute('data-id');
new NavActions().removeItem(id);
new NavActions().syncMenu().then(data => {
api.request(
API_NAV_SYNC,
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
).then(response => {
let r = JSON.parse(response.request['response']);
if (r.type == DataEvent.MENU_UPDATED) {
notify.alert(r.message, true);
} else {
notify.alert(r.message, true);
}
});
});
break;
case 'edit-item':
window.location = '/@/dashboard/pages/edit/' + e.target.getAttribute('data-id');

@ -1,16 +1,20 @@
//TOOLS
import ApiUtils, {
import FipamoAPI, {
REQUEST_TYPE_POST,
CONTENT_TYPE_FORM,
CONTENT_TYPE_JSON
} from '../utils/APIUtils';
CONTENT_TYPE_JSON,
API_NEW_PAGE,
API_EDIT_PAGE,
API_DELETE_PAGE,
API_IMAGE_UPLOAD
} from '../../libraries/FipamoAPI';
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 ApiUtils();
const api = new FipamoAPI();
const notify = new Notfications();
export default class PostEditor {
//--------------------------
@ -23,7 +27,6 @@ export default class PostEditor {
this.postID = null;
this.postUUID = null;
this.postLayout = null;
api.authStatus();
if (document.getElementById('post-edit-index').getAttribute('data-index')) {
this.postID = document.getElementById('post-edit-index').getAttribute('data-index');
this.postUUID = document.getElementById('post-edit-index').getAttribute('data-uuid');
@ -131,9 +134,7 @@ export default class PostEditor {
case EditorEvent.EDITOR_SAVE:
case EditorEvent.EDITOR_UPDATE:
var apiUrl = '';
e === EditorEvent.EDITOR_SAVE
? (apiUrl = '/api/v1/page/write/new')
: (apiUrl = '/api/v1/page/write');
e === EditorEvent.EDITOR_SAVE ? (apiUrl = API_NEW_PAGE) : (apiUrl = API_EDIT_PAGE);
new PageActions()
.collectInfo(document.getElementById('featured-image-upload').files[0])
@ -174,7 +175,7 @@ export default class PostEditor {
if (confirm("AYE! You know you're deleting this post, right?")) {
let id = { id: this.postUUID };
api.request(
'/api/v1/page/delete',
API_DELETE_PAGE,
DataEvent.API_PAGE_DELETE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
@ -238,7 +239,7 @@ export default class PostEditor {
}
}
handleImageUpload(type, files) {
let url = '/api/v1/page/add-post-image';
let url = API_IMAGE_UPLOAD;
let eventType = DataEvent.API_IMAGES_UPLOAD;
let self = this;
var imageData = new FormData();

@ -1,13 +1,17 @@
import SettingsActions from '../actions/SettingsActions';
import ApiUtils, {
import FipamoAPI, {
REQUEST_TYPE_POST,
CONTENT_TYPE_FORM,
CONTENT_TYPE_JSON
} from '../../../src/com/utils/APIUtils';
CONTENT_TYPE_JSON,
API_SETTINGS_SYNC,
API_UPLOAD_AVATAR,
API_UPLOAD_BACKGROUND,
API_PUBLISH_PAGES
} from '../../libraries/FipamoAPI';
import * as DataEvent from '../../../src/com/events/DataEvent';
import Mailer from '../actions/Mailer';
import Notifications from '../ui/Notifications';
const api = new ApiUtils();
const api = new FipamoAPI();
const notify = new Notifications();
const mailer = new Mailer();
export default class SettingsIndex {
@ -28,7 +32,7 @@ export default class SettingsIndex {
.getInfo()
.then(data => {
api.request(
'/api/v1/settings/sync',
API_SETTINGS_SYNC,
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
@ -154,9 +158,7 @@ export default class SettingsIndex {
handleImageUpload(type, files) {
let url = '';
let eventType = DataEvent.API_IMAGES_UPLOAD;
type == 'avatar-upload'
? (url = '/api/v1/settings/add-avatar')
: (url = '/api/v1/settings/add-feature-background');
type == 'avatar-upload' ? (url = API_UPLOAD_AVATAR) : (url = API_UPLOAD_BACKGROUND);
var imageData = new FormData();
for (var i = 0; i < files.length; i++) {
var file = files[i];
@ -189,7 +191,7 @@ export default class SettingsIndex {
e.stopPropagation();
let task = { task: 'publish all pages' };
api.request(
'/api/v1/settings/publish-pages',
API_PUBLISH_PAGES,
DataEvent.API_RENDER_PAGES,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,

@ -4,7 +4,19 @@ export const REQUEST_TYPE_PUT = 'PUT';
export const REQUEST_TYPE_DELETE = 'DELETE';
export const CONTENT_TYPE_JSON = 'json';
export const CONTENT_TYPE_FORM = 'x-www-form-urlencoded';
import * as DataEvent from '../events/DataEvent';
export const API_STATUS = '/api/v1/auth/status';
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
@ -12,6 +24,8 @@ export default class APIUtils {
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/v1/auth/status').then(r => {
let response = JSON.parse(r.request['response']);
if (response.type === DataEvent.API_REQUEST_GOOD) {

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save