Implement save modal
This commit is contained in:
parent
dfdae89133
commit
8b9db07858
|
@ -1,6 +1,6 @@
|
|||
<script lang="ts">
|
||||
import { oauth2 } from "@/stores/builder"
|
||||
import type { OAuth2Config } from "@/types"
|
||||
import type { OAuth2Config, UpsertOAuth2Config } from "@/types"
|
||||
import {
|
||||
Body,
|
||||
Divider,
|
||||
|
@ -19,8 +19,6 @@
|
|||
let errors: Record<string, string> = {}
|
||||
let hasBeenSubmitted = false
|
||||
|
||||
$: isNew = !config
|
||||
|
||||
$: data = (config as Partial<OAuth2Config>) ?? {}
|
||||
|
||||
const requiredString = (errorMessage: string) =>
|
||||
|
@ -41,7 +39,7 @@
|
|||
url: requiredString("Url is required.").url(),
|
||||
clientId: requiredString("Client ID is required."),
|
||||
clientSecret: requiredString("Client secret is required."),
|
||||
}) satisfies ZodType<OAuth2Config>
|
||||
}) satisfies ZodType<UpsertOAuth2Config>
|
||||
|
||||
const validationResult = validator.safeParse(config)
|
||||
errors = {}
|
||||
|
@ -67,7 +65,13 @@
|
|||
}
|
||||
|
||||
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) {
|
||||
notifications.error(e.message)
|
||||
return keepOpen
|
||||
|
|
|
@ -1,14 +1,9 @@
|
|||
import { API } from "@/api"
|
||||
import { BudiStore } from "@/stores/BudiStore"
|
||||
import { CreateOAuth2Config } from "@/types"
|
||||
|
||||
interface Config {
|
||||
id: string
|
||||
name: string
|
||||
}
|
||||
import { OAuth2Config, UpsertOAuth2Config } from "@/types"
|
||||
|
||||
interface OAuth2StoreState {
|
||||
configs: Config[]
|
||||
configs: OAuth2Config[]
|
||||
loading: boolean
|
||||
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 this.fetch()
|
||||
}
|
||||
|
||||
async edit(id: string, config: UpsertOAuth2Config) {
|
||||
await API.oauth2.update(id, config)
|
||||
await this.fetch()
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
await API.oauth2.delete(id)
|
||||
await this.fetch()
|
||||
|
|
|
@ -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 {}
|
||||
|
|
|
@ -11,6 +11,10 @@ export interface OAuth2Endpoints {
|
|||
create: (
|
||||
config: UpsertOAuth2ConfigRequest
|
||||
) => Promise<UpsertOAuth2ConfigResponse>
|
||||
update: (
|
||||
id: string,
|
||||
config: UpsertOAuth2ConfigRequest
|
||||
) => Promise<UpsertOAuth2ConfigResponse>
|
||||
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.
|
||||
* @param id the ID of the OAuth2 config
|
||||
|
|
Loading…
Reference in New Issue