1- import React , { useState , useContext , useEffect } from 'react'
1+ import React , { useState , useContext , useEffect , useMemo , useCallback } from 'react'
22import { useTranslation } from 'react-i18next'
33import GluuLabel from 'Routes/Apps/Gluu/GluuLabel'
44import GluuToogleRow from 'Routes/Apps/Gluu/GluuToogleRow'
@@ -13,7 +13,6 @@ import {
1313 InputGroup ,
1414 CustomInput ,
1515 Form ,
16- Button ,
1716} from 'Components'
1817import SetTitle from 'Utils/SetTitle'
1918import applicationStyle from 'Routes/Apps/Gluu/styles/applicationstyle'
@@ -27,6 +26,7 @@ import { SIMPLE_PASSWORD_AUTH } from 'Plugins/auth-server/common/Constants'
2726import { getScripts } from 'Redux/features/initSlice'
2827import GluuLoader from 'Routes/Apps/Gluu/GluuLoader'
2928import GluuProperties from 'Routes/Apps/Gluu/GluuProperties'
29+ import GluuCommitFooter from 'Routes/Apps/Gluu/GluuCommitFooter'
3030import packageJson from '../../../../package.json'
3131import { CedarlingLogType } from '@/cedarling'
3232import { updateToast } from '@/redux/features/toastSlice'
@@ -40,13 +40,25 @@ function SettingsPage() {
4040 const loadingConfig = useSelector ( ( state ) => state . authReducer ?. loadingConfig )
4141 const theme = useContext ( ThemeContext )
4242 const selectedTheme = theme . state . theme
43- const [ paggingSize , setPaggingSize ] = useState ( localStorage . getItem ( 'paggingSize' ) || 10 )
43+ const initialPaggingSize = localStorage . getItem ( 'paggingSize' ) || 10
44+ const [ paggingSize , setPaggingSize ] = useState ( initialPaggingSize )
45+ const [ originalPaggingSize , setOriginalPaggingSize ] = useState ( initialPaggingSize )
4446 SetTitle ( t ( 'titles.application_settings' ) )
4547
4648 useEffect ( ( ) => {
4749 dispatch ( getScripts ( { action : { } } ) )
4850 } , [ ] )
4951
52+ const resetPaggingSize = ( ) => {
53+ setPaggingSize ( originalPaggingSize )
54+ }
55+
56+ const savePaggingSize = ( ) => {
57+ localStorage . setItem ( 'paggingSize' , paggingSize )
58+ // Update the original value to the newly saved value
59+ setOriginalPaggingSize ( paggingSize )
60+ }
61+
5062 return (
5163 < React . Fragment >
5264 < GluuLoader blocking = { loadingScripts || loadingConfig } >
@@ -77,17 +89,10 @@ function SettingsPage() {
7789 type = "select"
7890 id = "pagingSize"
7991 name = "pagingSize"
80- defaultValue = {
81- levels [
82- levels . findIndex ( ( element ) => {
83- return element == paggingSize
84- } )
85- ]
86- }
92+ value = { paggingSize }
8793 onChange = { ( value ) => {
8894 const size = levels [ value . target . options . selectedIndex ]
8995 setPaggingSize ( size )
90- localStorage . setItem ( 'paggingSize' , size )
9196 } }
9297 >
9398 { levels . map ( ( item , key ) => (
@@ -123,46 +128,60 @@ function SettingsPage() {
123128 </ Col >
124129 </ FormGroup >
125130
126- { ! loadingScripts && < SettingsForm /> }
131+ { ! loadingScripts && (
132+ < SettingsForm resetPaggingSize = { resetPaggingSize } savePaggingSize = { savePaggingSize } />
133+ ) }
127134 </ CardBody >
128135 </ Card >
129136 </ GluuLoader >
130137 </ React . Fragment >
131138 )
132139}
133140
134- function SettingsForm ( ) {
141+ function SettingsForm ( { resetPaggingSize , savePaggingSize } ) {
135142 const { t } = useTranslation ( )
136- const theme = useContext ( ThemeContext )
137- const selectedTheme = theme . state . theme
138- const loadingConfig = useSelector ( ( state ) => state . authReducer ?. loadingConfig )
139- const additionalParameters =
140- useSelector ( ( state ) => state . authReducer ?. config ?. additionalParameters ) || [ ]
141- const acrValues = useSelector ( ( state ) => state . authReducer ?. config ?. acrValues )
142- const sessionTimeout =
143- useSelector ( ( state ) => state . authReducer ?. config ?. sessionTimeoutInMins ) || 5
144- const cedarlingLogType =
145- useSelector ( ( state ) => state . authReducer ?. config ?. cedarlingLogType ) || CedarlingLogType . OFF
146- const scripts = useSelector ( ( state ) => state . initReducer . scripts )
147143 const dispatch = useDispatch ( )
148144
149- const authScripts = scripts
150- . filter ( ( item ) => item . scriptType == 'person_authentication' )
151- . filter ( ( item ) => item . enabled )
152- . map ( ( item ) => item . name )
145+ const config = useSelector ( ( state ) => state . authReducer ?. config ) || { }
146+ const scripts = useSelector ( ( state ) => state . initReducer . scripts )
147+
148+ // Transform config to form values (Fido pattern)
149+ const transformToFormValues = useCallback ( ( configData ) => {
150+ return {
151+ sessionTimeoutInMins : configData ?. sessionTimeoutInMins || 30 ,
152+ acrValues : configData ?. acrValues || '' ,
153+ cedarlingLogType : configData ?. cedarlingLogType || CedarlingLogType . OFF ,
154+ additionalParameters : ( configData ?. additionalParameters || [ ] ) . map ( ( param ) => ( { ...param } ) ) ,
155+ }
156+ } , [ ] )
157+
158+ const initialValues = useMemo (
159+ ( ) => transformToFormValues ( config ) ,
160+ [ config , transformToFormValues ] ,
161+ )
153162
154- authScripts . push ( SIMPLE_PASSWORD_AUTH )
163+ const authScripts = useMemo ( ( ) => {
164+ const filtered = scripts
165+ . filter ( ( item ) => item . scriptType === 'person_authentication' )
166+ . filter ( ( item ) => item . enabled )
167+ . map ( ( item ) => item . name )
168+
169+ return [ ...filtered , SIMPLE_PASSWORD_AUTH ]
170+ } , [ scripts ] )
155171
156172 const formik = useFormik ( {
157- initialValues : {
158- sessionTimeoutInMins : sessionTimeout ,
159- acrValues : acrValues || '' ,
160- cedarlingLogType,
161- } ,
173+ initialValues,
174+ enableReinitialize : true ,
162175 onSubmit : ( values ) => {
176+ // Save paging size to localStorage
177+ savePaggingSize ( )
178+
179+ // Dispatch the action to save
163180 dispatch ( putConfigWorker ( values ) )
164- ! ( values ?. cedarlingLogType === cedarlingLogType ) &&
181+
182+ if ( values ?. cedarlingLogType !== config ?. cedarlingLogType ) {
165183 dispatch ( updateToast ( true , 'success' , t ( 'fields.reloginToViewCedarlingChanges' ) ) )
184+ }
166185 } ,
167186 validationSchema : Yup . object ( ) . shape ( {
168187 sessionTimeoutInMins : Yup . number ( )
@@ -171,6 +190,20 @@ function SettingsForm() {
171190 } ) ,
172191 } )
173192
193+ const handleCancel = useCallback ( ( ) => {
194+ const resetValues = transformToFormValues ( config )
195+ formik . resetForm ( { values : resetValues } )
196+ resetPaggingSize ( )
197+ } , [ formik , config , transformToFormValues , resetPaggingSize ] )
198+
199+ // Transform additionalParameters for GluuProperties (Fido pattern)
200+ const additionalParametersOptions = useMemo ( ( ) => {
201+ return ( formik . values . additionalParameters || [ ] ) . map ( ( param ) => ( {
202+ key : param . key || '' ,
203+ value : param . value || '' ,
204+ } ) )
205+ } , [ formik . values . additionalParameters ] )
206+
174207 return (
175208 < Form onSubmit = { formik . handleSubmit } >
176209 < GluuInputRow
@@ -186,7 +219,6 @@ function SettingsForm() {
186219 errorMessage = { formik . errors . sessionTimeoutInMins }
187220 showError = { formik . errors . sessionTimeoutInMins && formik . touched . sessionTimeoutInMins }
188221 />
189-
190222 < FormGroup row >
191223 < GluuLabel
192224 size = { 4 }
@@ -212,7 +244,6 @@ function SettingsForm() {
212244 </ InputGroup >
213245 </ Col >
214246 </ FormGroup >
215-
216247 < FormGroup row >
217248 < GluuLabel
218249 size = { 4 }
@@ -240,26 +271,23 @@ function SettingsForm() {
240271 />
241272 </ Col >
242273 </ FormGroup >
243-
244274 < div className = "mb-3" >
245275 < GluuProperties
246276 compName = "additionalParameters"
247277 label = "fields.custom_params_auth"
248278 formik = { formik }
249279 keyPlaceholder = { t ( 'placeholders.enter_property_key' ) }
250280 valuePlaceholder = { t ( 'placeholders.enter_property_value' ) }
251- options = { additionalParameters }
281+ options = { additionalParametersOptions }
252282 tooltip = "documentation.settings.custom_params"
253283 />
254284 </ div >
255- < Button
285+ < GluuCommitFooter
286+ hideButtons = { { save : true } }
256287 type = "submit"
257- color = { `primary-${ selectedTheme } ` }
258- className = "UserActionSubmitButton"
259- disabled = { loadingConfig || ! formik . dirty }
260- >
261- { t ( 'actions.submit' ) }
262- </ Button >
288+ disableBackButton = { true }
289+ cancelHandler = { handleCancel }
290+ />
263291 </ Form >
264292 )
265293}
0 commit comments