Implement save modal

This commit is contained in:
Adria Navarro 2025-03-18 09:45:42 +01:00
parent dfdae89133
commit 8b9db07858
4 changed files with 44 additions and 15 deletions

View File

@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import { oauth2 } from "@/stores/builder" import { oauth2 } from "@/stores/builder"
import type { OAuth2Config } from "@/types" import type { OAuth2Config, UpsertOAuth2Config } from "@/types"
import { import {
Body, Body,
Divider, Divider,
@ -19,8 +19,6 @@
let errors: Record<string, string> = {} let errors: Record<string, string> = {}
let hasBeenSubmitted = false let hasBeenSubmitted = false
$: isNew = !config
$: data = (config as Partial<OAuth2Config>) ?? {} $: data = (config as Partial<OAuth2Config>) ?? {}
const requiredString = (errorMessage: string) => const requiredString = (errorMessage: string) =>
@ -41,7 +39,7 @@
url: requiredString("Url is required.").url(), url: requiredString("Url is required.").url(),
clientId: requiredString("Client ID is required."), clientId: requiredString("Client ID is required."),
clientSecret: requiredString("Client secret is required."), clientSecret: requiredString("Client secret is required."),
}) satisfies ZodType<OAuth2Config> }) satisfies ZodType<UpsertOAuth2Config>
const validationResult = validator.safeParse(config) const validationResult = validator.safeParse(config)
errors = {} errors = {}
@ -67,7 +65,13 @@
} }
try { try {
await oauth2.create(validationResult.data) if (!config) {
await oauth2.create(validationResult.data)
notifications.success("Settings created.")
} else {
await oauth2.edit(config.id, validationResult.data)
notifications.success("Settings saved.")
}
} catch (e: any) { } catch (e: any) {
notifications.error(e.message) notifications.error(e.message)
return keepOpen return keepOpen

View File

@ -1,14 +1,9 @@
import { API } from "@/api" import { API } from "@/api"
import { BudiStore } from "@/stores/BudiStore" import { BudiStore } from "@/stores/BudiStore"
import { CreateOAuth2Config } from "@/types" import { OAuth2Config, UpsertOAuth2Config } from "@/types"
interface Config {
id: string
name: string
}
interface OAuth2StoreState { interface OAuth2StoreState {
configs: Config[] configs: OAuth2Config[]
loading: boolean loading: boolean
error?: string error?: string
} }
@ -48,11 +43,16 @@ export class OAuth2Store extends BudiStore<OAuth2StoreState> {
} }
} }
async create(config: CreateOAuth2Config) { async create(config: UpsertOAuth2Config) {
await API.oauth2.create(config) await API.oauth2.create(config)
await this.fetch() await this.fetch()
} }
async edit(id: string, config: UpsertOAuth2Config) {
await API.oauth2.update(id, config)
await this.fetch()
}
async delete(id: string) { async delete(id: string) {
await API.oauth2.delete(id) await API.oauth2.delete(id)
await this.fetch() await this.fetch()

View File

@ -1,3 +1,8 @@
import { UpsertOAuth2ConfigRequest } from "@budibase/types" import {
UpsertOAuth2ConfigRequest,
OAuth2ConfigResponse,
} from "@budibase/types"
export interface CreateOAuth2Config extends UpsertOAuth2ConfigRequest {} export interface OAuth2Config extends OAuth2ConfigResponse {}
export interface UpsertOAuth2Config extends UpsertOAuth2ConfigRequest {}

View File

@ -11,6 +11,10 @@ export interface OAuth2Endpoints {
create: ( create: (
config: UpsertOAuth2ConfigRequest config: UpsertOAuth2ConfigRequest
) => Promise<UpsertOAuth2ConfigResponse> ) => Promise<UpsertOAuth2ConfigResponse>
update: (
id: string,
config: UpsertOAuth2ConfigRequest
) => Promise<UpsertOAuth2ConfigResponse>
delete: (id: string) => Promise<void> delete: (id: string) => Promise<void>
} }
@ -43,6 +47,22 @@ export const buildOAuth2Endpoints = (API: BaseAPIClient): OAuth2Endpoints => ({
}) })
}, },
/**
* Updates an existing OAuth2 configuration.
* @param name the name of the row action
* @param tableId the ID of the table
*/
update: async (id, config) => {
return await API.put<UpsertOAuth2ConfigRequest, UpsertOAuth2ConfigResponse>(
{
url: `/api/oauth2/${id}`,
body: {
...config,
},
}
)
},
/** /**
* Deletes an OAuth2 configuration by its id. * Deletes an OAuth2 configuration by its id.
* @param id the ID of the OAuth2 config * @param id the ID of the OAuth2 config