muita coisa

This commit is contained in:
Gabriel Pastori
2023-11-05 00:41:04 -03:00
parent e0f6d28a88
commit 558545e469
23 changed files with 3005 additions and 2824 deletions

View File

@@ -2,10 +2,18 @@
import axios from 'axios'
import { defineStore } from 'pinia'
export const useAppStore = defineStore('app', {
getters: {
validConnection: (state) => state.connection.valid,
instances: (state) => state.instancesList,
getInstance: (state) => (instanceName) => state.instancesList.find(
(instance) => instance.instance.instanceName === instanceName
),
getInstanceApiKey: (state) => (instance) => {
return state.getInstance(instance).instance.apiKey ||
state.instancesKeys[instance]
},
},
state: () => ({
connection: {
@@ -13,6 +21,7 @@ export const useAppStore = defineStore('app', {
host: null,
globalApiKey: null,
},
instancesKeys: {},
instancesList: [],
}),
@@ -29,9 +38,8 @@ export const useAppStore = defineStore('app', {
url: '/instance/fetchInstances'
})
this.connection.valid = true
this.connection.host = host
this.connection.globalApiKey = globalApiKey
this.saveConnection({ host, globalApiKey })
this.instancesList = responde.data
} catch (e) {
this.connection.valid = false
@@ -39,10 +47,31 @@ 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}`
})
} catch (e) {
this.connection.valid = false
throw e.response?.data?.response?.message || e.response || e
}
},
async reconnect() {
try {
const { host, globalApiKey } = this.connection
const responde = await axios({
const response = await axios({
method: 'GET',
baseURL: host,
headers: {
@@ -52,14 +81,48 @@ export const useAppStore = defineStore('app', {
url: '/instance/fetchInstances'
})
this.connection.valid = true
this.connection.host = host
this.connection.globalApiKey = globalApiKey
this.instancesList = responde.data
this.saveConnection({ host, globalApiKey })
this.instancesList = response.data
} catch (e) {
this.connection.valid = false
throw e.response?.data?.response?.message || e.response || e
}
},
setInstanceStatus(instance, status) {
const index = this.instancesList.findIndex(
(instance) => instance.instance.instanceName === instance
)
this.instancesList[index].instance.status = status
},
addInstanceKey({ instance, key }) {
this.instancesKeys[instance] = key
},
saveConnection({ host, globalApiKey }) {
this.connection = {
valid: true,
host,
globalApiKey,
}
if (typeof window !== 'undefined') {
window.localStorage.setItem('connection', JSON.stringify({
host,
globalApiKey,
}))
}
},
async loadConnection() {
if (typeof window !== 'undefined') {
const connection = window.localStorage.getItem('connection')
if (connection) {
this.connection = JSON.parse(connection)
return this.reconnect()
}
}
}
}
})