diff --git a/curiosrc/web/api/config/config.go b/curiosrc/web/api/config/config.go index 6f9598f7fad..9accdf5a5ac 100644 --- a/curiosrc/web/api/config/config.go +++ b/curiosrc/web/api/config/config.go @@ -30,9 +30,26 @@ func Routes(r *mux.Router, deps *deps.Deps) { // At edit.html: r.Methods("GET").Path("/schema").HandlerFunc(getSch) r.Methods("GET").Path("/layers/{layer}").HandlerFunc(c.getLayer) + r.Methods("POST").Path("/addlayer").HandlerFunc(c.addLayer) r.Methods("POST").Path("/layers/{layer}").HandlerFunc(c.setLayer) r.Methods("GET").Path("/default").HandlerFunc(c.def) } + +func (c *cfg) addLayer(w http.ResponseWriter, r *http.Request) { + var layer struct { + Name string + } + apihelper.OrHTTPFail(w, json.NewDecoder(r.Body).Decode(&layer)) + ct, err := c.DB.Exec(context.Background(), `INSERT INTO harmony_config (title, config) VALUES ($1, $2)`, layer.Name, "") + apihelper.OrHTTPFail(w, err) + if ct != 1 { + w.WriteHeader(http.StatusConflict) + w.Write([]byte("Layer already exists")) + return + } + w.WriteHeader(200) +} + func getSch(w http.ResponseWriter, r *http.Request) { ref := jsonschema.Reflector{ Mapper: func(i reflect.Type) *jsonschema.Schema { diff --git a/curiosrc/web/static/config/index.html b/curiosrc/web/static/config/index.html index 769929e53a3..28a11f6d9e4 100644 --- a/curiosrc/web/static/config/index.html +++ b/curiosrc/web/static/config/index.html @@ -22,6 +22,7 @@ this.loadData(); this.message = this.readCookie('message'); this.eraseCookie('message'); + this.addName = ''; } static get styles() { return [css` @@ -46,6 +47,13 @@ `)} + + +
DELETE FROM curio.harmony_config WHERE title = 'my_layer_name';
+
`;
}
loadData() {
@@ -77,6 +85,26 @@
eraseCookie(name) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
}
+
+ updateName(e) {
+ this.addName = e.target.value;
+ }
+ addLayer() {
+ // get a name
+ var v = this.addName;
+ if (v === '') {
+ alert('Error: Layer name cannot be empty');
+ return;
+ }
+
+ axios.post('/api/config/addlayer', { name: v })
+ .then(response => {
+ window.location.href = '/config/edit.html?layer=' + v;
+ })
+ .catch(error => {
+ alert('Error adding layer:', error);
+ });
+ }
});