-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathConfigApiPage.tsx
More file actions
75 lines (63 loc) · 2.3 KB
/
ConfigApiPage.tsx
File metadata and controls
75 lines (63 loc) · 2.3 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
import React, { useState } from 'react'
import ApiConfigForm from './ApiConfigForm'
import { Card } from 'Components'
import GluuLoader from 'Routes/Apps/Gluu/GluuLoader'
import applicationStyle from 'Routes/Apps/Gluu/styles/applicationstyle'
import SetTitle from 'Utils/SetTitle'
import { useTranslation } from 'react-i18next'
import { useGetConfigApiProperties, usePatchConfigApiProperties } from 'JansConfigApi'
import { useConfigApiActions } from './hooks'
import { toast } from 'react-toastify'
import type { JsonPatch } from './types'
function ConfigApiPage(): JSX.Element {
const { t } = useTranslation()
const { logConfigApiUpdate } = useConfigApiActions()
const [errorMessage, setErrorMessage] = useState<string | null>(null)
SetTitle(t('titles.config_api_configuration'))
const { data: configuration, isLoading, error } = useGetConfigApiProperties()
const patchConfigMutation = usePatchConfigApiProperties()
const handleSubmit = async (patches: JsonPatch[], message: string) => {
try {
setErrorMessage(null)
const updatedConfig = await patchConfigMutation.mutateAsync({
data: patches,
})
try {
if (updatedConfig) {
await logConfigApiUpdate(updatedConfig, message, { requestBody: patches })
}
} catch (auditError) {
console.error('Error logging audit:', auditError)
}
toast.success(t('messages.success_in_saving'))
} catch (err) {
console.error('Error updating config:', err)
const errorMsg = err instanceof Error ? err.message : t('messages.error_in_saving')
setErrorMessage(errorMsg)
toast.error(errorMsg)
}
}
const loading = patchConfigMutation.isPending || isLoading
if (error) {
return (
<Card style={applicationStyle.mainCard}>
<div className="p-4 text-danger">
{t('messages.error_in_loading')}: {String(error)}
</div>
</Card>
)
}
return (
<GluuLoader blocking={loading}>
<Card style={applicationStyle.mainCard}>
{configuration && <ApiConfigForm configuration={configuration} onSubmit={handleSubmit} />}
{errorMessage && (
<div className="alert alert-danger mt-3" role="alert">
{errorMessage}
</div>
)}
</Card>
</GluuLoader>
)
}
export default ConfigApiPage