From ffb47bf3eee435d1be04fe359ce87b5b11e57ee2 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 2 Jan 2025 15:59:26 +0000 Subject: [PATCH] Convert environment store to TS --- .../_components/EditVariableColumn.svelte | 3 +- .../builder/src/stores/portal/environment.js | 71 ----------------- .../builder/src/stores/portal/environment.ts | 79 +++++++++++++++++++ 3 files changed, 81 insertions(+), 72 deletions(-) delete mode 100644 packages/builder/src/stores/portal/environment.js create mode 100644 packages/builder/src/stores/portal/environment.ts diff --git a/packages/builder/src/pages/builder/portal/settings/environment/_components/EditVariableColumn.svelte b/packages/builder/src/pages/builder/portal/settings/environment/_components/EditVariableColumn.svelte index 8f60b3f3ca..00ff1804d9 100644 --- a/packages/builder/src/pages/builder/portal/settings/environment/_components/EditVariableColumn.svelte +++ b/packages/builder/src/pages/builder/portal/settings/environment/_components/EditVariableColumn.svelte @@ -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() } diff --git a/packages/builder/src/stores/portal/environment.js b/packages/builder/src/stores/portal/environment.js deleted file mode 100644 index 232f314cad..0000000000 --- a/packages/builder/src/stores/portal/environment.js +++ /dev/null @@ -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() diff --git a/packages/builder/src/stores/portal/environment.ts b/packages/builder/src/stores/portal/environment.ts new file mode 100644 index 0000000000..2269ba48ab --- /dev/null +++ b/packages/builder/src/stores/portal/environment.ts @@ -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 { + 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()