-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathApiConfigForm.tsx
More file actions
116 lines (100 loc) · 3.23 KB
/
ApiConfigForm.tsx
File metadata and controls
116 lines (100 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import React, { useCallback, useState, useEffect } from 'react'
import GluuCommitDialog from 'Routes/Apps/Gluu/GluuCommitDialog'
import { FormGroup } from 'Components'
import { useNavigate } from 'react-router-dom'
import spec from '../../../../../configApiSpecs.yaml'
import { API_CONFIG_WRITE } from 'Utils/PermChecker'
import { useCedarling } from '@/cedarling'
import GluuCommitFooter from 'Routes/Apps/Gluu/GluuCommitFooter'
import JsonPropertyBuilderConfigApi from './JsonPropertyBuilderConfigApi'
import { toast } from 'react-toastify'
import type { ApiAppConfiguration, JsonPatch } from './types'
interface ApiConfigFormProps {
configuration: ApiAppConfiguration
onSubmit: (patches: JsonPatch[], message: string) => Promise<void>
}
interface SpecSchema {
components: {
schemas: {
ApiAppConfiguration: {
properties: Record<string, unknown>
}
}
}
}
const { properties: schema } = (spec as unknown as SpecSchema).components?.schemas
?.ApiAppConfiguration ?? { properties: {} }
const ApiConfigForm: React.FC<ApiConfigFormProps> = ({ configuration, onSubmit }) => {
const { hasCedarPermission, authorize } = useCedarling()
const navigate = useNavigate()
const [modal, setModal] = useState(false)
const [patches, setPatches] = useState<JsonPatch[]>([])
const operations = patches
useEffect(() => {
const authorizePermissions = async () => {
try {
await authorize([API_CONFIG_WRITE])
} catch (error) {
console.error('Error authorizing API config permissions:', error)
}
}
authorizePermissions()
}, [authorize])
const toggle = useCallback(() => {
if (patches?.length > 0) {
setModal((prev) => !prev)
} else {
toast.error('No changes to update')
}
}, [patches])
const submitForm = useCallback(
async (userMessage: string) => {
toggle()
await onSubmit(patches, userMessage)
},
[toggle, onSubmit, patches],
)
function generateLabel(name: string): string {
const result = name.replace(/([A-Z])/g, ' $1')
return result.charAt(0).toUpperCase() + result.slice(1)
}
const patchHandler = (patch: JsonPatch) => {
setPatches((existingPatches) => [...existingPatches, patch])
}
const handleBack = () => {
navigate('/home/dashboard')
}
return (
<>
{Object.keys(configuration).map((propKey) => (
<JsonPropertyBuilderConfigApi
key={propKey}
propKey={propKey}
propValue={configuration[propKey as keyof ApiAppConfiguration]}
lSize={6}
handler={patchHandler}
schema={schema[propKey] as { type?: string; items?: { type?: string; enum?: string[] } }}
doc_category="config_api_properties"
/>
))}
<FormGroup row></FormGroup>
{hasCedarPermission(API_CONFIG_WRITE) && (
<GluuCommitFooter
saveHandler={toggle}
hideButtons={{ back: false }}
backButtonLabel="Back"
backButtonHandler={handleBack}
/>
)}
{hasCedarPermission(API_CONFIG_WRITE) && (
<GluuCommitDialog
handler={toggle}
modal={modal}
operations={operations}
onAccept={submitForm}
/>
)}
</>
)
}
export default ApiConfigForm