-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathApiConfigForm.js
More file actions
123 lines (104 loc) · 3.48 KB
/
ApiConfigForm.js
File metadata and controls
123 lines (104 loc) · 3.48 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
117
118
119
120
121
122
123
import React, { useCallback, useState, useEffect } from 'react'
import GluuCommitDialog from 'Routes/Apps/Gluu/GluuCommitDialog'
import { FormGroup } from 'Components'
import { useDispatch, useSelector } from 'react-redux'
import { useNavigate } from 'react-router-dom'
import spec from '../../../../../configApiSpecs.yaml'
import { buildPayload, API_CONFIG_WRITE } from 'Utils/PermChecker'
import { useCedarling } from '@/cedarling'
import GluuCommitFooter from 'Routes/Apps/Gluu/GluuCommitFooter'
import { patchApiConfigConfiguration } from 'Plugins/auth-server/redux/features/configApiSlice'
import JsonPropertyBuilderConfigApi from './JsonPropertyBuilderConfigApi'
import { toast } from 'react-toastify'
const schema = spec.components.schemas.ApiAppConfiguration.properties
const ApiConfigForm = () => {
const { hasCedarPermission, authorize } = useCedarling()
const dispatch = useDispatch()
const navigate = useNavigate()
const [modal, setModal] = useState(false)
const [patches, setPatches] = useState([])
const [operations, setOperations] = useState([])
const configuration = useSelector((state) => state.configApiReducer.configuration)
const { permissions: cedarPermissions } = useSelector((state) => state.cedarPermissions)
const userAction = {}
// Permission initialization
useEffect(() => {
const authorizePermissions = async () => {
try {
await authorize([API_CONFIG_WRITE])
} catch (error) {
console.error('Error authorizing API config permissions:', error)
}
}
authorizePermissions()
}, [])
useEffect(() => {}, [cedarPermissions])
const toggle = useCallback(() => {
if (patches?.length > 0) {
setModal(!modal)
} else toast.error('No changes to update')
}, [modal])
const submitForm = useCallback((userMessage) => {
toggle()
handleSubmit(userMessage)
}, [])
const handleSubmit = (message) => {
if (patches.length) {
const postBody = {}
postBody['requestBody'] = patches
buildPayload(userAction, message, postBody)
dispatch(patchApiConfigConfiguration({ action: userAction }))
}
}
function generateLabel(name) {
const result = name.replace(/([A-Z])/g, ' $1')
return result.toLowerCase()
}
const patchHandler = (patch) => {
setPatches((existingPatches) => [...existingPatches, patch])
const newPatches = patches
newPatches.push(patch)
setPatches(newPatches)
setOperations(newPatches)
}
const handleBack = () => {
navigate('/home/dashboard')
}
return (
<>
{Object.keys(configuration).map((propKey) => {
if (generateLabel(propKey)) {
return (
<JsonPropertyBuilderConfigApi
key={propKey}
propKey={propKey}
propValue={configuration[propKey]}
lSize={6}
handler={patchHandler}
schema={schema[propKey]}
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