mirror of
https://git.rwth-aachen.de/acs/public/villas/web/
synced 2025-03-09 00:00:01 +01:00
removed AppDispatcher and flux data managers
Signed-off-by: Andrii Podriez <andrey5577990@gmail.com>
This commit is contained in:
parent
bde429abb5
commit
3266bc98fc
4 changed files with 0 additions and 412 deletions
|
@ -21,7 +21,6 @@ import SwaggerUI from 'swagger-ui-react'
|
|||
import 'swagger-ui-react/swagger-ui.css'
|
||||
import '../styles/swagger-ui.css';
|
||||
import RestAPI from './api/rest-api';
|
||||
import RestDataManager from './data-managers/rest-data-manager';
|
||||
|
||||
class APIBrowser extends React.Component {
|
||||
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
/**
|
||||
* This file is part of VILLASweb.
|
||||
*
|
||||
* VILLASweb is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* VILLASweb is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with VILLASweb. If not, see <http://www.gnu.org/licenses/>.
|
||||
******************************************************************************/
|
||||
|
||||
import { Dispatcher } from 'flux';
|
||||
|
||||
class AppDispatcher extends Dispatcher {
|
||||
dispatch(payload) {
|
||||
if (this.isDispatching()) {
|
||||
// try again later
|
||||
var self = this;
|
||||
|
||||
setTimeout(function() {
|
||||
self.dispatch(payload);
|
||||
}, 1);
|
||||
} else {
|
||||
// do actual dispatch
|
||||
super.dispatch(payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new AppDispatcher();
|
|
@ -1,146 +0,0 @@
|
|||
/**
|
||||
* This file is part of VILLASweb.
|
||||
*
|
||||
* VILLASweb is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* VILLASweb is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with VILLASweb. If not, see <http://www.gnu.org/licenses/>.
|
||||
******************************************************************************/
|
||||
|
||||
import { ReduceStore } from 'flux/utils';
|
||||
|
||||
import AppDispatcher from './app-dispatcher';
|
||||
import NotificationsDataManager from '../common/data-managers/notifications-data-manager';
|
||||
import NotificationsFactory from "./data-managers/notifications-factory";
|
||||
|
||||
class ArrayStore extends ReduceStore {
|
||||
constructor(type, dataManager) {
|
||||
super(AppDispatcher);
|
||||
|
||||
this.type = type;
|
||||
this.dataManager = dataManager;
|
||||
}
|
||||
|
||||
getInitialState() {
|
||||
return [];
|
||||
}
|
||||
|
||||
updateElements(state, newElements) {
|
||||
// search for existing element to update
|
||||
state.forEach((element, index, array) => {
|
||||
newElements = newElements.filter((updateElement, newIndex) => {
|
||||
if (element.id === updateElement.id) {
|
||||
// update each property
|
||||
for (var key in updateElement) {
|
||||
if (updateElement.hasOwnProperty(key)) {
|
||||
array[index][key] = updateElement[key];
|
||||
}
|
||||
}
|
||||
|
||||
// remove updated element from update list
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// all elements still in the list will just be added
|
||||
state = state.concat(newElements);
|
||||
|
||||
// announce change to listeners
|
||||
this.__emitChange();
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
reduce(state, action) {
|
||||
switch (action.type) {
|
||||
case this.type + '/start-load':
|
||||
if (Array.isArray(action.data)) {
|
||||
action.data.forEach((id) => {
|
||||
this.dataManager.load(id, action.token,action.param);
|
||||
});
|
||||
} else {
|
||||
this.dataManager.load(action.data, action.token,action.param);
|
||||
}
|
||||
return state;
|
||||
|
||||
case this.type + '/loaded':
|
||||
if (Array.isArray(action.data)) {
|
||||
return this.updateElements(state, action.data);
|
||||
} else {
|
||||
return this.updateElements(state, [action.data]);
|
||||
}
|
||||
|
||||
case this.type + '/load-error':
|
||||
if (action.error && !action.error.handled && action.error.response) {
|
||||
|
||||
NotificationsDataManager.addNotification(NotificationsFactory.LOAD_ERROR(action.error.response.body.message));
|
||||
}
|
||||
return super.reduce(state, action);
|
||||
|
||||
case this.type + '/start-add':
|
||||
this.dataManager.add(action.data, action.token,action.param);
|
||||
return state;
|
||||
|
||||
case this.type + '/added':
|
||||
if(typeof action.data.managedexternally !== "undefined" && action.data.managedexternally === true ) return state;
|
||||
return this.updateElements(state, [action.data]);
|
||||
|
||||
case this.type + '/add-error':
|
||||
|
||||
return state;
|
||||
|
||||
|
||||
case this.type + '/start-remove':
|
||||
this.dataManager.remove(action.data, action.token,action.param);
|
||||
return state;
|
||||
|
||||
case this.type + '/removed':
|
||||
if (action.original) {
|
||||
return state.filter((item) => {
|
||||
return (item !== action.original);
|
||||
});
|
||||
} else {
|
||||
return state.filter((item) => {
|
||||
return (item.id !== action.data);
|
||||
});
|
||||
}
|
||||
|
||||
case this.type + '/remove-error':
|
||||
if (action.error && !action.error.handled && action.error.response) {
|
||||
NotificationsDataManager.addNotification(NotificationsFactory.DELETE_ERROR(action.error.response.body.message));
|
||||
}
|
||||
return super.reduce(state, action);
|
||||
|
||||
case this.type + '/start-edit':
|
||||
if(action.id){
|
||||
this.dataManager.update(action.data, action.token,action.param,action.id);
|
||||
}
|
||||
else{
|
||||
this.dataManager.update(action.data, action.token,action.param);
|
||||
}
|
||||
return state;
|
||||
|
||||
case this.type + '/edited':
|
||||
return this.updateElements(state, [action.data]);
|
||||
|
||||
case this.type + '/edit-error':
|
||||
return state;
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default ArrayStore;
|
|
@ -1,229 +0,0 @@
|
|||
/**
|
||||
* This file is part of VILLASweb.
|
||||
*
|
||||
* VILLASweb is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* VILLASweb is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with VILLASweb. If not, see <http://www.gnu.org/licenses/>.
|
||||
******************************************************************************/
|
||||
|
||||
import RestAPI from '../api/rest-api';
|
||||
import AppDispatcher from '../app-dispatcher';
|
||||
|
||||
const API_URL = '/api/v2';
|
||||
|
||||
class RestDataManager {
|
||||
constructor(type, url, keyFilter) {
|
||||
this.url = url;
|
||||
this.type = type;
|
||||
this.keyFilter = keyFilter;
|
||||
this.onLoad = null;
|
||||
}
|
||||
|
||||
makeURL(part) {
|
||||
return API_URL + part;
|
||||
}
|
||||
|
||||
filterKeys(object) {
|
||||
// don't change anything if no filter is set
|
||||
if (this.keyFilter == null || Array.isArray(this.keyFilter) === false) {
|
||||
return object;
|
||||
}
|
||||
|
||||
// remove all keys not in the filter
|
||||
Object.keys(object).filter(key => {
|
||||
return this.keyFilter.indexOf(key) === -1;
|
||||
}).forEach(key => {
|
||||
delete object[key];
|
||||
});
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
requestURL(form, id, param, object = null){
|
||||
switch(form){
|
||||
case 'load/add':
|
||||
if (param === null){
|
||||
if(id != null){
|
||||
return this.makeURL(this.url + '/' + id);
|
||||
}
|
||||
else {
|
||||
return this.makeURL(this.url);
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(id != null){
|
||||
return this.makeURL(this.url + '/' + id + param);
|
||||
}
|
||||
else {
|
||||
return this.makeURL(this.url + param)
|
||||
}
|
||||
}
|
||||
case 'remove/update':
|
||||
if(id !== null){
|
||||
return this.makeURL(this.url + '/' + id);
|
||||
|
||||
}
|
||||
else if(param === null){
|
||||
return this.makeURL(this.url + '/' + object.id);
|
||||
}
|
||||
else{
|
||||
return this.makeURL(this.url + '/' + object.id + param);
|
||||
}
|
||||
default:
|
||||
console.log("something went wrong");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
load(id, token = null,param = null) {
|
||||
if (id != null) {
|
||||
// load single object
|
||||
RestAPI.get(this.requestURL('load/add',id,param), token).then(response => {
|
||||
let data;
|
||||
if (response.hasOwnProperty(this.type)) {
|
||||
data = this.filterKeys(response[this.type]);
|
||||
}else{
|
||||
// loaded file
|
||||
data = response;
|
||||
}
|
||||
|
||||
AppDispatcher.dispatch({
|
||||
type: this.type + 's/loaded',
|
||||
data: data,
|
||||
token: token
|
||||
});
|
||||
|
||||
if (this.onLoad != null) {
|
||||
this.onLoad(data, token);
|
||||
}
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
type: this.type + 's/load-error',
|
||||
error: error
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// load all objects
|
||||
RestAPI.get(this.requestURL('load/add',id,param), token).then(response => {
|
||||
const data = response[this.type + 's'].map(element => {
|
||||
return this.filterKeys(element);
|
||||
});
|
||||
|
||||
AppDispatcher.dispatch({
|
||||
type: this.type + 's/loaded',
|
||||
data: data,
|
||||
token: token,
|
||||
});
|
||||
|
||||
if (this.onLoad != null) {
|
||||
this.onLoad(data, token);
|
||||
}
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
type: this.type + 's/load-error',
|
||||
error: error
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
add(object, token = null, param = null, subObjects = null) {
|
||||
var obj = {};
|
||||
obj[this.type] = this.filterKeys(object);
|
||||
RestAPI.post(this.requestURL('load/add',null,param), obj, token).then(response => {
|
||||
AppDispatcher.dispatch({
|
||||
type: this.type + 's/added',
|
||||
data: response[this.type],
|
||||
token: token
|
||||
});
|
||||
|
||||
// check if POST is done for import of object and issue dispatches of sub-objects
|
||||
if (subObjects !== null){
|
||||
// there are sub-objects to be added for an import
|
||||
for (let objectType of subObjects){
|
||||
let type = Object.keys(objectType) // type can be dashboards, configs, widgets, ...
|
||||
type = type[0];
|
||||
for (let newObj of objectType[type]){
|
||||
|
||||
// set the ID of the object that the sub-object shall be associated with
|
||||
if(type === "configs" || type === "dashboards"){
|
||||
// the main object is a scenario
|
||||
newObj.scenarioID = response[this.type].id
|
||||
} else if (type === "widgets") {
|
||||
// the main object is a dashboard
|
||||
newObj.dashboardID = response[this.type].id
|
||||
} else if (type === "signals") {
|
||||
// the main object is a component configuration
|
||||
newObj.configID = response[this.type].id
|
||||
}
|
||||
|
||||
// iterate over all objects of type 'type' add issue add dispatch
|
||||
AppDispatcher.dispatch({
|
||||
type: type + '/start-add',
|
||||
data: newObj,
|
||||
token: token
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
type: this.type + 's/add-error',
|
||||
error: error
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
remove(object, token = null, param = null) {
|
||||
RestAPI.delete(this.requestURL('remove/update',null,param,object), token).then(response => {
|
||||
AppDispatcher.dispatch({
|
||||
type: this.type + 's/removed',
|
||||
data: response[this.type],
|
||||
original: object,
|
||||
token: token
|
||||
});
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
type: this.type + 's/remove-error',
|
||||
error: error
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
update(object, token = null, param = null, id = null) {
|
||||
var obj = {};
|
||||
obj[this.type] = this.filterKeys(object);
|
||||
|
||||
RestAPI.put(this.requestURL('remove/update',id,param,object), obj, token).then(response => {
|
||||
AppDispatcher.dispatch({
|
||||
type: this.type + 's/edited',
|
||||
data: response[this.type]
|
||||
});
|
||||
}).catch(error => {
|
||||
AppDispatcher.dispatch({
|
||||
type: this.type + 's/edit-error',
|
||||
error: error
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
export default RestDataManager;
|
Loading…
Add table
Reference in a new issue