Commit 4dd09045 authored by Stefan Dähling's avatar Stefan Dähling
Browse files

add upload mas to frontend

parent fe86c211
Pipeline #341478 passed with stages
in 59 seconds
......@@ -113,7 +113,19 @@ func (fe *Frontend) handleMAS(w http.ResponseWriter, r *http.Request) (cmapErr,
httpErr = httpreply.CMAPError(w, cmapErr.Error())
}
} else if r.Method == "POST" {
var body []byte
body, cmapErr = ioutil.ReadAll(r.Body)
if cmapErr == nil {
var masSpec schemas.MASSpec
cmapErr = json.Unmarshal(body, &masSpec)
if cmapErr == nil {
_, httpErr = amsclient.PostMAS(masSpec)
} else {
httpErr = httpreply.JSONUnmarshalError(w)
}
} else {
httpErr = httpreply.InvalidBodyError(w)
}
} else {
httpErr = httpreply.MethodNotAllowed(w)
cmapErr = errors.New("Error: Method not allowed on path /api/ams/mas")
......
......@@ -70,6 +70,9 @@ func (fe *Frontend) handleAPI(w http.ResponseWriter, r *http.Request) {
resvalid, cmapErr, httpErr = fe.handleDF(w, r, respath)
case "logger":
resvalid, cmapErr, httpErr = fe.handleLogging(w, r, respath)
case "overview":
resvalid = true
cmapErr, httpErr = fe.handleOverview(w, r)
default:
cmapErr = errors.New("Resource not found")
}
......@@ -87,6 +90,17 @@ func (fe *Frontend) handleAPI(w http.ResponseWriter, r *http.Request) {
}
}
// handleOverview is the handler for requests to path /api/overview
func (fe *Frontend) handleOverview(w http.ResponseWriter, r *http.Request) (cmapErr, httpErr error) {
if r.Method == "GET" {
cmapErr, httpErr = fe.handleMAS(w, r)
} else {
httpErr = httpreply.MethodNotAllowed(w)
cmapErr = errors.New("Error: Method not allowed on path /api/overview")
}
return
}
// listen opens a http server listening and serving request
func (fe *Frontend) listen() (err error) {
mux := http.NewServeMux()
......
......@@ -87,6 +87,15 @@ body {
margin: 1em 0em 5em 0em;
}
.contentbox {
margin: 0em 0em 5em 0em;
}
textarea {
width: 100%;
height: 20em;
}
.footer {
display: none;
position: fixed;
......@@ -98,7 +107,7 @@ body {
}
hr {
margin: 0em 1em;
margin: 0em 1em 1em 1em;
}
table {
......
......@@ -42,12 +42,21 @@ function updateSidebar(){
function contentOverview(mass) {
$(".contenttitle").replaceWith("<h2 class=\"contenttitle\">Overview</h2>");
clearContent();
$(".content").append("<table id=\"mass\"></table>");
$("#mass").append("<tr><th>MASs:</th><th>"+mass.length.toString()+"</th></tr>");
$(".content").append("<hr>");
$(".content").append("<div class=\"contentbox\" id=\"startbox\"></div>")
$("#startbox").append("<table id=\"startmas\"></table>");
$("#startmas").append("<tr><th><h3>Start new MAS:</h3></th></tr>")
$("#startbox").append("<hr>");
$("#startbox").append("<tr><th><input type=\"file\" id=\"inputMAS\" value=\"Import\" accept=\".json\"/></th><th><button id=\"uploadMAS\">Upload</button></th></tr>")
$("#inputMAS").change(inputMAS);
$("#uploadMAS").click(uploadMAS);
$("#startbox").append("<tr><th><textarea id=\"newMAS\"></textarea></th></tr>")
$(".content").append("<div class=\"contentbox\" id=\"massbox\"></div>")
$("#massbox").append("<table id=\"mass\"></table>");
$("#mass").append("<tr><th><h3>MASs:</h3></th><th>"+mass.length.toString()+"</th></tr>");
$("#massbox").append("<hr>");
for (let i of mass) {
let masID = "MAS"+i.id.toString()
$(".content").append("<table id=\""+masID+"\"></table>");
$("#massbox").append("<table id=\""+masID+"\"></table>");
$("#"+masID).append("<tr><th>"+masID+"</th></tr>");
$("#"+masID).append("<tr><th></th><th>Name:</th><th>"+i.config.name+"</th></tr>");
$("#"+masID).append("<tr><th></th><th>Agents per agency:</th><th>"+i.config.agentsperagency.toString()+"</th></tr>");
......@@ -58,15 +67,32 @@ function contentOverview(mass) {
}
}
// callback for inputMAS input button
function inputMAS() {
let files = this.files;
if (files.length <= 0) {
return false;
}
let fr = new FileReader();
fr.onload = function(e) {
var result = JSON.parse(e.target.result);
var formatted = JSON.stringify(result, null, 2);
$("#newMAS").val(formatted);
}
fr.readAsText(files.item(0));
}
// callback for uploadMAS button
function uploadMAS() {
let masconfig = $("#newMAS").val();
request.post("/api/ams/mas", masconfig)
sideoverview()
}
// request info about mas and call function to show content
function contentAMS(masID){
// fetch('/api/ams/mas/'+masID.toString())
// .then(response => response.json())
// .then(masInfo => {
// $(".contenttitle").replaceWith("<h2 class=\"contenttitle\">MAS"+masID.toString()+"</h2>");
// contentMasInfo(masInfo);
// console.log(masInfo);
// })
request.get('/api/ams/mas/'+masID.toString(),showAMSContent)
}
......
......@@ -4,4 +4,19 @@ function get(url, callback) {
.then(json => {callback(json)})
}
export {get};
\ No newline at end of file
function post(url, data) {
fetch(url, {
method: 'post',
headers: {'Content-Type' : 'application/json'},
body: data
})
.then ( (text) => {
// log response text
console.log (text);
})
.catch ((error) => {
console.log ("Error: ", error)
})
}
export {get, post};
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment