diff --git a/src/components/Configuration/JsonEditor.vue b/src/components/Configuration/JsonEditor.vue index f531549c..dc0f568b 100644 --- a/src/components/Configuration/JsonEditor.vue +++ b/src/components/Configuration/JsonEditor.vue @@ -1,11 +1,25 @@ @@ -30,6 +51,8 @@ import VJsoneditor from 'v-jsoneditor'; import { localStorageKeys } from '@/utils/defaults'; import configSchema from '@/utils/ConfigSchema.json'; +import JsonToYaml from '@/utils/JsonToYaml'; +import axios from 'axios'; export default { name: 'JsonEditor', @@ -43,6 +66,7 @@ export default { return { jsonData: this.config, errorMessages: [], + saveMode: 'file', options: { schema: configSchema, mode: 'tree', @@ -50,6 +74,9 @@ export default { name: 'config', onValidationError: this.validationErrors, }, + jsonParser: JsonToYaml, + responseText: '', + saveSuccess: undefined, }; }, computed: { @@ -59,6 +86,35 @@ export default { }, methods: { save() { + if (this.saveMode === 'local') { + this.saveConfigLocally(); + } else if (this.saveMode === 'file') { + this.writeConfigToDisk(); + } else { + this.$toasted.show('Please select a Save Mode: Local or File'); + } + }, + writeConfigToDisk() { + // 1. Convert JSON into YAML + const yaml = this.jsonParser(this.jsonData); + // 2. Prepare the request + const baseUrl = process.env.VUE_APP_DOMAIN || window.location.origin; + const endpoint = `${baseUrl}/config-manager/save`; + const headers = { 'Content-Type': 'text/plain' }; + const body = { config: yaml, timestamp: new Date() }; + const request = axios.post(endpoint, body, headers); + // 3. Make the request, and handle response + request.then((response) => { + this.saveSuccess = response.data.success || false; + this.responseText = response.data.message; + if (this.saveSuccess) this.carefullyClearLocalStorage(); + }) + .catch((error) => { + this.saveSuccess = false; + this.responseText = error; + }); + }, + saveConfigLocally() { const data = this.jsonData; if (data.sections) { localStorage.setItem(localStorageKeys.CONF_SECTIONS, JSON.stringify(data.sections)); @@ -74,6 +130,11 @@ export default { } this.$toasted.show('Changes saved succesfully'); }, + carefullyClearLocalStorage() { + localStorage.removeItem(localStorageKeys.PAGE_INFO); + localStorage.removeItem(localStorageKeys.APP_CONFIG); + localStorage.removeItem(localStorageKeys.CONF_SECTIONS); + }, validationErrors(errors) { const errorMessages = []; errors.forEach((error) => { @@ -105,6 +166,7 @@ export default {