Update client config after upload, import or index

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer
2019-11-16 23:22:50 +01:00
parent 64868c81b3
commit aebbb17a53
7 changed files with 17 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
import Api from "common/api"; import Event from "pubsub-js";
class Config { class Config {
/** /**
@@ -11,15 +11,14 @@ class Config {
this.values = values; this.values = values;
// this.setValues(JSON.parse(this.storage.getItem(this.storage_key))); this.subscriptionId = Event.subscribe('config.updated', (ev, data) => this.setValues(data));
// this.setValues(values);
} }
setValues(values) { setValues(values) {
if(!values) return; if (!values) return;
for(let key in values) { for (let key in values) {
if(values.hasOwnProperty(key)) { if (values.hasOwnProperty(key)) {
this.setValue(key, values[key]); this.setValue(key, values[key]);
} }
} }
@@ -52,14 +51,6 @@ class Config {
return this; return this;
} }
pullFromServer() {
return Api.get("config").then(
(result) => {
this.setValues(result.data);
}
);
}
} }
export default Config; export default Config;

View File

@@ -27,6 +27,7 @@
</template> </template>
<script> <script>
import Alert from "common/alert";
import Event from "pubsub-js"; import Event from "pubsub-js";
export default { export default {
@@ -55,12 +56,12 @@
this.$api.post('import').then(function () { this.$api.post('import').then(function () {
ctx.busy = false; ctx.busy = false;
ctx.completed = 100; ctx.completed = 100;
this.fileName = ''; ctx.fileName = '';
}).catch(function () { }).catch(function () {
this.$alert.error("Import failed"); Alert.error("Import failed");
ctx.busy = false; ctx.busy = false;
ctx.completed = 0; ctx.completed = 0;
this.fileName = ''; ctx.fileName = '';
}); });
}, },
handleEvent(ev, data) { handleEvent(ev, data) {

View File

@@ -27,6 +27,7 @@
</template> </template>
<script> <script>
import Alert from "common/alert";
import Event from "pubsub-js"; import Event from "pubsub-js";
export default { export default {
@@ -55,12 +56,12 @@
this.$api.post('index').then(function () { this.$api.post('index').then(function () {
ctx.busy = false; ctx.busy = false;
ctx.completed = 100; ctx.completed = 100;
this.fileName = ''; ctx.fileName = '';
}).catch(function () { }).catch(function () {
this.$alert.error("Indexing failed"); Alert.error("Indexing failed");
ctx.busy = false; ctx.busy = false;
ctx.completed = 0; ctx.completed = 0;
this.fileName = ''; ctx.fileName = '';
}); });
}, },
handleEvent(ev, data) { handleEvent(ev, data) {

View File

@@ -53,18 +53,4 @@ describe("common/config", () => {
const result = config.getValue("city"); const result = config.getValue("city");
assert.equal(result, "Berlin"); assert.equal(result, "Berlin");
}); });
});
it("should pull from server", async() => {
mock.onGet("config").reply(200, {fromServer: "yes"});
const storage = window.localStorage;
const values = {name: "testConfig", year: "2300"};
const config = new Config(storage, values);
const result = config.getValues();
assert.equal(result.name, "testConfig");
assert.equal(config.values.fromServer, undefined);
await config.pullFromServer();
assert.equal(config.values.fromServer, "yes");
mock.reset();
});
});

View File

@@ -58,6 +58,7 @@ func Import(router *gin.RouterGroup, conf *config.Config) {
event.Success(fmt.Sprintf("import completed in %d s", elapsed)) event.Success(fmt.Sprintf("import completed in %d s", elapsed))
event.Publish("import.completed", event.Data{"path": path, "seconds": elapsed}) event.Publish("import.completed", event.Data{"path": path, "seconds": elapsed})
event.Publish("config.updated", event.Data(conf.ClientConfig()))
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("import completed in %d s", elapsed)}) c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("import completed in %d s", elapsed)})
}) })

View File

@@ -45,6 +45,7 @@ func Index(router *gin.RouterGroup, conf *config.Config) {
event.Success(fmt.Sprintf("indexing completed in %d s", elapsed)) event.Success(fmt.Sprintf("indexing completed in %d s", elapsed))
event.Publish("index.completed", event.Data{"path": path, "seconds": elapsed}) event.Publish("index.completed", event.Data{"path": path, "seconds": elapsed})
event.Publish("config.updated", event.Data(conf.ClientConfig()))
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("indexing completed in %d s", elapsed)}) c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("indexing completed in %d s", elapsed)})
}) })

View File

@@ -30,7 +30,7 @@ func wsReader(ws *websocket.Conn) {
func wsWriter(ws *websocket.Conn, conf *config.Config) { func wsWriter(ws *websocket.Conn, conf *config.Config) {
pingTicker := time.NewTicker(10 * time.Second) pingTicker := time.NewTicker(10 * time.Second)
s := event.Subscribe("index.*", "alert.*") s := event.Subscribe("index.*", "upload.*", "import.*", "alert.*", "config.*")
defer func() { defer func() {
pingTicker.Stop() pingTicker.Stop()