Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions admin-ui/plugins/jans-lock/components/JansLock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const JansLock: React.FC = () => {
<JansLockConfiguration
lockConfig={lockConfiguration as Record<string, unknown>}
onUpdate={handleUpdate}
isSubmitting={patchMutation.isPending}
/>
) : null}
</CardBody>
Expand Down
42 changes: 35 additions & 7 deletions admin-ui/plugins/jans-lock/components/JansLockConfiguration.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useFormik } from 'formik'
import React, { useState, useEffect, useCallback } from 'react'
import React, { useState, useEffect, useCallback, useMemo } from 'react'
import { buildPayload, JANS_LOCK_WRITE } from 'Utils/PermChecker'
import { useCedarling } from '@/cedarling'
import { Row, Col, Form, FormGroup } from 'Components'
Expand All @@ -20,9 +20,14 @@ import { trimObjectStrings } from 'Utils/Util'
interface JansLockConfigurationProps {
lockConfig: Record<string, unknown>
onUpdate: (patches: PatchOperation[]) => void
isSubmitting?: boolean
}

const JansLockConfiguration: React.FC<JansLockConfigurationProps> = ({ lockConfig, onUpdate }) => {
const JansLockConfiguration: React.FC<JansLockConfigurationProps> = ({
lockConfig,
onUpdate,
isSubmitting,
}) => {
const { hasCedarPermission, authorize } = useCedarling()
const viewOnly = !hasCedarPermission(JANS_LOCK_WRITE)
const [modal, setModal] = useState(false)
Expand Down Expand Up @@ -54,6 +59,24 @@ const JansLockConfiguration: React.FC<JansLockConfigurationProps> = ({ lockConfi
formik.resetForm()
}, [formik])

const isFormDirty = useMemo(() => {
if (!lockConfig || !formik.values) {
return false
}
const trimmedValues = trimObjectStrings(
formik.values as unknown as Record<string, unknown>,
) as unknown as JansLockConfigFormValues
const patches = createPatchOperations(trimmedValues, lockConfig)
return patches.length > 0
}, [lockConfig, formik.values])

const isFormValid = useMemo(() => {
if (!formik.values) {
return false
}
return validationSchema.isValidSync(formik.values)
}, [formik.values])

const submitForm = useCallback(
(userMessage: string) => {
const trimmedValues = trimObjectStrings(
Expand Down Expand Up @@ -405,11 +428,16 @@ const JansLockConfiguration: React.FC<JansLockConfigurationProps> = ({ lockConfi
<Row>
<Col>
<GluuCommitFooter
saveHandler={toggle}
hideButtons={{ save: true, back: true }}
extraLabel="Cancel"
extraOnClick={handleCancel}
type="submit"
showBack={true}
showCancel={true}
showApply={true}
onApply={toggle}
onCancel={handleCancel}
disableBack={false}
disableCancel={!isFormDirty}
disableApply={!isFormValid || !isFormDirty}
applyButtonType="submit"
isLoading={!!isSubmitting}
/>
</Col>
</Row>
Expand Down
20 changes: 15 additions & 5 deletions admin-ui/plugins/jans-lock/helper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,21 @@ export const createPatchOperations = (
}
}
} else if (normalizedValues[key] !== undefined && normalizedValues[key] !== '') {
differences.push({
op: 'add',
path: `/${key}`,
value: normalizedValues[key],
})
const value = normalizedValues[key]
const isEmptyArray = Array.isArray(value) && value.length === 0
const isEmptyObject =
typeof value === 'object' &&
value !== null &&
!Array.isArray(value) &&
Object.keys(value as Record<string, unknown>).length === 0

if (!isEmptyArray && !isEmptyObject) {
differences.push({
op: 'add',
path: `/${key}`,
value: normalizedValues[key],
})
}
}
}

Expand Down