Skip to content

Instantly share code, notes, and snippets.

@noodle71
Last active January 28, 2021 10:40
Show Gist options
  • Save noodle71/8faeaf5a93da01450f6736cd43711011 to your computer and use it in GitHub Desktop.
Save noodle71/8faeaf5a93da01450f6736cd43711011 to your computer and use it in GitHub Desktop.
Activeboards utils
(async function(){
const wu = require('@devo/webutils');
await load();
const EMPTY = {"name":"Empty","description":"","settings":"{\"extra\":{\"favourites\":{\"widgets\":\"Line\",\"inputs\":\"Input\",\"containers\":\"Grid\"},\"config\":{\"theme\":{}},\"autoRefreshPeriod\":null},\"type\":\"container\",\"subtype\":\"Grid\",\"settings\":{\"layout\":{},\"header\":false},\"children\":null,\"date\":null}"};
async function load(){
const all = await getIndex();
const summary = all.map(({id, name, owner}) => ({id, name, owner: owner.email}));
window.abUtils = {
all, // [Property] List of all ABs
summary, // [Property] List (with less info) of all ABs
load, // [Method] Reload AB list <example: abUtils.load()>
create, // [Method] Create AB from config <example: abUtils.create()>
mine: getMine(summary), // [Property] List of ABs which belong to you
deleteById, // [Method] Delete an AB <example: abUtils.deleteById(abId)>
getIndex, // [Method] Promise to get the list of ABs <example: abUtils.getIndex()>
getById, // [Method] Promise to get an AB by ID <example: abUtils.getById(abId)>
getRawJSONById, // [Method] Get Raw Json by ID <example: abUtils.getRawJSONById(abId)>
updateById // [Method] Update an AB with a config <example: abUtils.updateById(abId, rawJson)>
};
}
async function getById(id){
return await wu.ajax.fetch({url:`devo/redada/dashboard/${id}`});
}
async function getRawJSONById(id){
const ab = await getById(id);
console.log(JSON.stringify(ab.object.settings));
}
async function create(config = EMPTY){
const ab = await wu.ajax.fetch({
url:`devo/redada/dashboard`,
type: 'POST', data: JSON.stringify(config),
dataType: 'json',
contentType: 'application/json; charset=UTF-8'});
console.log(ab.msg);
}
async function deleteById(id){
const ab = await wu.ajax.fetch({url:`devo/redada/dashboard/${id}`, type: 'DELETE'});
console.log(ab.msg);
}
async function updateById(id, config){
const r = await getById(id);
const {name, description, version} = r.object;
const data = JSON.stringify(Object.assign({name, description, version}, {settings: JSON.stringify(config)}));
const ab = await wu.ajax.fetch({
url:`devo/redada/dashboard/${id}`,
type: 'PUT', data,
dataType: 'json',
contentType: 'application/json; charset=UTF-8'});
console.log(ab.msg);
}
async function getIndex(){
const r = await wu.ajax.fetch({url:'devo/redada/dashboard'});
return r.object;
}
function getMine(abs){
const myEmail = window.lt.user.getEmail();
return abs.filter(({owner}) => (owner === myEmail));
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment