|
| 1 | +import React, { useEffect, useState } from 'react' |
| 2 | +import { useSelector, useDispatch } from 'react-redux' |
| 3 | +import { CardBody, Card } from 'Components' |
| 4 | +import ScopeForm from './ScopeForm' |
| 5 | +import GluuLoader from 'Routes/Apps/Gluu/GluuLoader' |
| 6 | +import { getAttributes, getScripts } from 'Redux/features/initSlice' |
| 7 | +import { buildPayload } from 'Utils/PermChecker' |
| 8 | +import GluuAlert from 'Routes/Apps/Gluu/GluuAlert' |
| 9 | +import { useTranslation } from 'react-i18next' |
| 10 | +import applicationStyle from 'Routes/Apps/Gluu/styles/applicationstyle' |
| 11 | +import { usePostOauthScopes } from 'JansConfigApi' |
| 12 | +import type { Scope } from 'JansConfigApi' |
| 13 | +import type { ExtendedScope, ScopeScript, ScopeClaim, ModifiedFields } from './types' |
| 14 | +import { EMPTY_SCOPE } from './types' |
| 15 | +import { useScopeActions } from './hooks' |
| 16 | + |
| 17 | +interface InitState { |
| 18 | + scripts: ScopeScript[] |
| 19 | + attributes: ScopeClaim[] |
| 20 | +} |
| 21 | + |
| 22 | +interface RootState { |
| 23 | + initReducer: InitState |
| 24 | +} |
| 25 | + |
| 26 | +const ScopeAddPage: React.FC = () => { |
| 27 | + const { t } = useTranslation() |
| 28 | + const dispatch = useDispatch() |
| 29 | + const { logScopeCreation, navigateToScopeList } = useScopeActions() |
| 30 | + |
| 31 | + const scripts = useSelector((state: RootState) => state.initReducer.scripts) |
| 32 | + const attributes = useSelector((state: RootState) => state.initReducer.attributes) |
| 33 | + |
| 34 | + const [modifiedFields, setModifiedFields] = useState<ModifiedFields>({}) |
| 35 | + const [errorMessage, setErrorMessage] = useState<string | null>(null) |
| 36 | + |
| 37 | + const createScope = usePostOauthScopes() |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + if (attributes.length === 0) { |
| 41 | + const attributeOptions: Record<string, unknown> = {} |
| 42 | + buildPayload(attributeOptions, 'Fetch attributes', { limit: 100 }) |
| 43 | + dispatch(getAttributes({ options: attributeOptions })) |
| 44 | + } |
| 45 | + if (scripts.length === 0) { |
| 46 | + const scriptAction: Record<string, unknown> = {} |
| 47 | + buildPayload(scriptAction, 'Fetch custom scripts', {}) |
| 48 | + dispatch(getScripts({ action: scriptAction })) |
| 49 | + } |
| 50 | + }, [dispatch, attributes.length, scripts.length]) |
| 51 | + |
| 52 | + async function handleSubmit(data: string) { |
| 53 | + if (!data) return |
| 54 | + |
| 55 | + setErrorMessage(null) |
| 56 | + |
| 57 | + let parsedData: Scope |
| 58 | + try { |
| 59 | + parsedData = JSON.parse(data) as Scope |
| 60 | + } catch (error) { |
| 61 | + console.error('Error parsing scope data:', error) |
| 62 | + setErrorMessage(t('messages.error_in_parsing_data')) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + const message = (parsedData as Record<string, unknown>).action_message as string |
| 68 | + delete (parsedData as Record<string, unknown>).action_message |
| 69 | + |
| 70 | + await createScope.mutateAsync({ data: parsedData }) |
| 71 | + |
| 72 | + try { |
| 73 | + await logScopeCreation(parsedData, message, modifiedFields) |
| 74 | + } catch (auditError) { |
| 75 | + console.error('Error logging audit:', auditError) |
| 76 | + } |
| 77 | + |
| 78 | + navigateToScopeList() |
| 79 | + } catch (error) { |
| 80 | + console.error('Error creating scope:', error) |
| 81 | + setErrorMessage(error instanceof Error ? error.message : t('messages.error_in_saving')) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const handleSearch = (value: string) => { |
| 86 | + const option = { |
| 87 | + pattern: value, |
| 88 | + } |
| 89 | + dispatch(getAttributes({ options: option })) |
| 90 | + } |
| 91 | + |
| 92 | + const scope: ExtendedScope = { |
| 93 | + ...EMPTY_SCOPE, |
| 94 | + claims: [], |
| 95 | + dynamicScopeScripts: [], |
| 96 | + defaultScope: false, |
| 97 | + attributes: { |
| 98 | + spontaneousClientId: undefined, |
| 99 | + spontaneousClientScopes: [], |
| 100 | + showInConfigurationEndpoint: false, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + return ( |
| 105 | + <GluuLoader blocking={createScope.isPending}> |
| 106 | + <GluuAlert |
| 107 | + severity={t('titles.error')} |
| 108 | + message={errorMessage || t('messages.error_in_saving')} |
| 109 | + show={!!errorMessage} |
| 110 | + /> |
| 111 | + <Card className="mb-3" style={applicationStyle.mainCard}> |
| 112 | + <CardBody> |
| 113 | + <ScopeForm |
| 114 | + scope={scope} |
| 115 | + scripts={scripts} |
| 116 | + attributes={attributes} |
| 117 | + handleSubmit={handleSubmit} |
| 118 | + onSearch={handleSearch} |
| 119 | + modifiedFields={modifiedFields} |
| 120 | + setModifiedFields={setModifiedFields} |
| 121 | + /> |
| 122 | + </CardBody> |
| 123 | + </Card> |
| 124 | + </GluuLoader> |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +export default ScopeAddPage |
0 commit comments