Convert environment store to TS

This commit is contained in:
Andrew Kingston 2025-01-02 15:59:26 +00:00
parent 717b71ceca
commit ffb47bf3ee
No known key found for this signature in database
3 changed files with 81 additions and 72 deletions

View File

@ -10,7 +10,8 @@
let deleteDialog
const save = async data => {
await environment.updateVariable(data)
const { name, ...rest } = data
await environment.updateVariable(name, rest)
editVariableModal.hide()
}
</script>

View File

@ -1,71 +0,0 @@
import { writable, get } from "svelte/store"
import { API } from "@/api"
import { Constants } from "@budibase/frontend-core"
import { licensing } from "@/stores/portal"
export function createEnvironmentStore() {
const { subscribe, update } = writable({
variables: [],
status: {},
})
async function checkStatus() {
const status = await API.checkEnvironmentVariableStatus()
update(store => {
store.status = status
return store
})
}
async function loadVariables() {
if (get(licensing).environmentVariablesEnabled) {
const envVars = await API.fetchEnvironmentVariables()
const mappedVars = envVars.variables.map(name => ({ name }))
update(store => {
store.variables = mappedVars
return store
})
}
}
async function createVariable(data) {
await API.createEnvironmentVariable(data)
let mappedVar = { name: data.name }
update(store => {
store.variables = [mappedVar, ...store.variables]
return store
})
}
async function deleteVariable(varName) {
await API.deleteEnvironmentVariable(varName)
update(store => {
store.variables = store.variables.filter(
envVar => envVar.name !== varName
)
return store
})
}
async function updateVariable(data) {
await API.updateEnvironmentVariable(data)
}
async function upgradePanelOpened() {
await API.publishEvent(
Constants.EventPublishType.ENV_VAR_UPGRADE_PANEL_OPENED
)
}
return {
subscribe,
checkStatus,
loadVariables,
createVariable,
deleteVariable,
updateVariable,
upgradePanelOpened,
}
}
export const environment = createEnvironmentStore()

View File

@ -0,0 +1,79 @@
import { get } from "svelte/store"
import { API } from "@/api"
import { licensing } from "@/stores/portal"
import { BudiStore } from "../BudiStore"
import {
CreateEnvironmentVariableRequest,
EventPublishType,
StatusEnvironmentVariableResponse,
UpdateEnvironmentVariableRequest,
} from "@budibase/types"
type EnvVar = {
name: string
}
interface EnvironmentState {
variables: EnvVar[]
status: StatusEnvironmentVariableResponse
}
class EnvironmentStore extends BudiStore<EnvironmentState> {
constructor() {
super({
variables: [],
status: {
encryptionKeyAvailable: false,
},
})
}
async checkStatus() {
const status = await API.checkEnvironmentVariableStatus()
this.update(store => {
store.status = status
return store
})
}
async loadVariables() {
if (get(licensing).environmentVariablesEnabled) {
const envVars: string[] = (await API.fetchEnvironmentVariables())
.variables
const mappedVars = envVars.map(name => ({ name }))
this.update(store => {
store.variables = mappedVars
return store
})
}
}
async createVariable(data: CreateEnvironmentVariableRequest) {
await API.createEnvironmentVariable(data)
let mappedVar = { name: data.name }
this.update(state => {
state.variables = [mappedVar, ...state.variables]
return state
})
}
async deleteVariable(name: string) {
await API.deleteEnvironmentVariable(name)
this.update(state => {
state.variables = state.variables.filter(envVar => envVar.name !== name)
return state
})
}
async updateVariable(name: string, data: UpdateEnvironmentVariableRequest) {
await API.updateEnvironmentVariable(name, data)
}
async upgradePanelOpened() {
await API.publishEvent(
EventPublishType.ENVIRONMENT_VARIABLE_UPGRADE_PANEL_OPENED
)
}
}
export const environment = new EnvironmentStore()