From 6a9e1c54d406f28238e93f65595f1bfc2b9f0ba4 Mon Sep 17 00:00:00 2001 From: Gabriel Pastori <58153955+gabrielpastori1@users.noreply.github.com> Date: Mon, 6 Nov 2023 18:44:35 -0300 Subject: [PATCH] multi-connections and basic settings --- src/components/instance/InstanceBody.vue | 4 +- src/components/instance/settings/Options.vue | 187 +++++++++++++++++++ src/components/modal/CreateInstance.vue | 2 - src/components/modal/Settings.vue | 93 ++++++++- src/services/instanceSettingsController.js | 33 ++++ src/store/app.js | 64 +++++-- src/views/Instance.vue | 6 +- 7 files changed, 363 insertions(+), 26 deletions(-) create mode 100644 src/components/instance/settings/Options.vue diff --git a/src/components/instance/InstanceBody.vue b/src/components/instance/InstanceBody.vue index d2d0898..54930b1 100644 --- a/src/components/instance/InstanceBody.vue +++ b/src/components/instance/InstanceBody.vue @@ -21,6 +21,7 @@ + + diff --git a/src/components/modal/CreateInstance.vue b/src/components/modal/CreateInstance.vue index b0bf5d5..3fd5aa7 100644 --- a/src/components/modal/CreateInstance.vue +++ b/src/components/modal/CreateInstance.vue @@ -2,7 +2,6 @@ @@ -48,7 +47,6 @@ - + @@ -31,12 +35,27 @@ + + Cancel + + - Cancel + + Cancel + @@ -44,6 +63,60 @@ + + + +

Conexões salvas

+ + + + {{ + conect.host.replace(/https?:\/\//, "") + }} + + + + + + +
+
@@ -65,12 +138,15 @@ export default { AppStore: useAppStore(), }), methods: { - async save() { + removeConnection(connection) { + this.AppStore.removeConnection(connection); + }, + async save(connection) { try { this.loading = true; this.error = false; - await this.AppStore.setConnection(this.connection); + await this.AppStore.setConnection(connection || this.connection); this.dialog = false; } catch (e) { this.error = e.message?.message || e.message || e; @@ -80,10 +156,15 @@ export default { }, open() { this.dialog = true; - this.connection = this.AppStore.connection; + this.connection = Object.assign({}, this.AppStore.connection); }, }, + computed: { + connectionsList() { + return this.AppStore.connectionsList; + }, + }, emits: ["close"], }; diff --git a/src/services/instanceSettingsController.js b/src/services/instanceSettingsController.js index e876320..ec88167 100644 --- a/src/services/instanceSettingsController.js +++ b/src/services/instanceSettingsController.js @@ -1,6 +1,32 @@ import http from "../http-common"; +const findOptions = async (instanceName) => { + return await http + .get("/settings/find/:instance", { + params: { + instance: instanceName + } + }) + .then((r) => r.data) + .catch((error) => { + throw error.response?.data || error.response || error; + }); +} + +const setOptions = async (instanceName, data) => { + return await http + .post("/settings/set/:instance", data, { + params: { + instance: instanceName + } + }) + .then((r) => r.data) + .catch((error) => { + throw error.response?.data || error.response || error; + }); +} + const findWebhook = async (instanceName) => { return await http .get("/webhook/find/:instance", { @@ -115,7 +141,14 @@ const setTypebot = async (instanceName, data) => { }); } + + export default { + options: { + get: findOptions, + set: setOptions, + }, + webhook: { get: findWebhook, set: setWebhook, diff --git a/src/store/app.js b/src/store/app.js index c7e36c6..ded82d0 100644 --- a/src/store/app.js +++ b/src/store/app.js @@ -23,6 +23,9 @@ export const useAppStore = defineStore('app', { }, instancesKeys: {}, instancesList: [], + + // lista de "contatos" de conexoes + connectionsList: [], }), actions: { @@ -51,16 +54,16 @@ export const useAppStore = defineStore('app', { async loadInstance(instanceName) { try { const { host, globalApiKey } = this.connection; - - const response = await axios({ - method: 'GET', - baseURL: host, - headers: { - 'Content-Type': 'application/json', - 'apikey': globalApiKey - }, - url: `/instance/fetchInstances?instanceName=${instanceName}` - }) + return this.reconnect() + // const response = await axios({ + // method: 'GET', + // baseURL: host, + // headers: { + // 'Content-Type': 'application/json', + // 'apikey': globalApiKey + // }, + // url: `/instance/fetchInstances?instanceName=${instanceName}` + // }) } catch (e) { this.connection.valid = false @@ -71,6 +74,9 @@ export const useAppStore = defineStore('app', { async reconnect() { try { const { host, globalApiKey } = this.connection + if (!host || !globalApiKey) { + throw new Error('Invalid connection') + } const response = await axios({ method: 'GET', baseURL: host, @@ -101,28 +107,54 @@ export const useAppStore = defineStore('app', { this.instancesKeys[instance] = key }, + removeConnection({ host }) { + const currentKey = this.connectionsList.findIndex( + (item) => item.host === host + ) + if (currentKey !== -1) + this.connectionsList.splice(currentKey, 1) + + this.saveLocalStorage() + }, saveConnection({ host, globalApiKey }) { this.connection = { valid: true, host, globalApiKey, } + + const currentKey = this.connectionsList.findIndex( + (item) => item.host === host + ) + if (currentKey === -1) { + this.connectionsList.push({ host, globalApiKey }) + } else { + this.connectionsList[currentKey] = { host, globalApiKey } + } + + this.saveLocalStorage() + }, + + saveLocalStorage() { if (typeof window !== 'undefined') { - window.localStorage.setItem('connection', JSON.stringify({ - host, - globalApiKey, - })) + window.localStorage.setItem('connection', JSON.stringify(this.connection)) + window.localStorage.setItem('connectionsList', JSON.stringify(this.connectionsList)) } }, async loadConnection() { if (typeof window !== 'undefined') { + const connectionsList = window.localStorage.getItem('connectionsList') + if (connectionsList) { + this.connectionsList = JSON.parse(connectionsList || '[]') + } + const connection = window.localStorage.getItem('connection') if (connection) { - this.connection = JSON.parse(connection) + this.connection = JSON.parse(connection || '{}') return this.reconnect() } } - } + }, } }) diff --git a/src/views/Instance.vue b/src/views/Instance.vue index a3189e6..501fc5c 100644 --- a/src/views/Instance.vue +++ b/src/views/Instance.vue @@ -39,7 +39,11 @@ export default { } }, }, - watch: {}, + watch: { + instance(val, oldVal) { + if (!val && oldVal) this.$router.push("/"); + }, + }, computed: { instance() {