-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: created CustomFields component, useVerifyPassword and useCusto…
…mFields
- Loading branch information
1 parent
48ba260
commit 05cc7c1
Showing
7 changed files
with
267 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React from 'react'; | ||
import RNPickerSelect from 'react-native-picker-select'; | ||
|
||
import { FormTextInput } from '../TextInput'; | ||
import useParsedCustomFields from '../../lib/hooks/useCustomFields'; | ||
|
||
interface ICustomFields { | ||
Accounts_CustomFields: string; | ||
customFields: any; | ||
onCustomFieldChange: (value: any) => void; | ||
} | ||
|
||
const CustomFields = ({ Accounts_CustomFields, customFields, onCustomFieldChange }: ICustomFields) => { | ||
if (!Accounts_CustomFields) { | ||
return null; | ||
} | ||
const { parsedCustomFields } = useParsedCustomFields(Accounts_CustomFields); | ||
try { | ||
return Object.keys(parsedCustomFields).map((key: string, index: number, array: any) => { | ||
if (parsedCustomFields[key].type === 'select') { | ||
const options = parsedCustomFields[key].options.map((option: string) => ({ label: option, value: option })); | ||
return ( | ||
<RNPickerSelect | ||
key={key} | ||
items={options} | ||
onValueChange={value => { | ||
const newValue: { [key: string]: string } = {}; | ||
newValue[key] = value; | ||
onCustomFieldChange({ ...customFields, ...newValue }); | ||
}} | ||
value={customFields[key]}> | ||
<FormTextInput | ||
inputRef={e => { | ||
// @ts-ignore | ||
this[key] = e; | ||
}} | ||
label={key} | ||
placeholder={key} | ||
value={customFields[key]} | ||
testID='settings-view-language' | ||
/> | ||
</RNPickerSelect> | ||
); | ||
} | ||
|
||
return ( | ||
<FormTextInput | ||
inputRef={e => { | ||
// @ts-ignore | ||
this[key] = e; | ||
}} | ||
key={key} | ||
label={key} | ||
placeholder={key} | ||
value={customFields[key]} | ||
onChangeText={value => { | ||
const newValue: { [key: string]: string } = {}; | ||
newValue[key] = value; | ||
onCustomFieldChange({ ...customFields, ...newValue }); | ||
}} | ||
onSubmitEditing={() => { | ||
if (array.length - 1 > index) { | ||
// @ts-ignore | ||
return this[array[index + 1]].focus(); | ||
} | ||
}} | ||
containerStyle={{ marginBottom: 0, marginTop: 0 }} | ||
/> | ||
); | ||
}); | ||
} catch (error) { | ||
return null; | ||
} | ||
}; | ||
|
||
export default CustomFields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useMemo } from 'react'; | ||
import log from '../../lib/methods/helpers/log'; | ||
|
||
const useParsedCustomFields = (Accounts_CustomFields: string) => { | ||
const parsedCustomFields = useMemo(() => { | ||
let parsed: any = {}; | ||
if (Accounts_CustomFields) { | ||
try { | ||
parsed = JSON.parse(Accounts_CustomFields); | ||
} catch (error) { | ||
log(error); | ||
} | ||
} | ||
return parsed; | ||
}, [Accounts_CustomFields]); | ||
|
||
return { parsedCustomFields }; | ||
}; | ||
|
||
export default useParsedCustomFields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import { useSetting } from './useSetting'; | ||
import i18n from '../../i18n'; | ||
|
||
export interface IPasswordPolicy { | ||
name: string; | ||
label: string; | ||
regex: RegExp; | ||
} | ||
|
||
const useVerifyPassword = (password: string, confirmPassword: string) => { | ||
const Accounts_Password_Policy_AtLeastOneLowercase = useSetting('Accounts_Password_Policy_AtLeastOneLowercase'); | ||
const Accounts_Password_Policy_Enabled = useSetting('Accounts_Password_Policy_Enabled'); | ||
const Accounts_Password_Policy_AtLeastOneNumber = useSetting('Accounts_Password_Policy_AtLeastOneNumber'); | ||
const Accounts_Password_Policy_AtLeastOneSpecialCharacter = useSetting('Accounts_Password_Policy_AtLeastOneSpecialCharacter'); | ||
const Accounts_Password_Policy_AtLeastOneUppercase = useSetting('Accounts_Password_Policy_AtLeastOneUppercase'); | ||
const Accounts_Password_Policy_ForbidRepeatingCharacters = useSetting('Accounts_Password_Policy_ForbidRepeatingCharacters'); | ||
const Accounts_Password_Policy_ForbidRepeatingCharactersCount = useSetting( | ||
'Accounts_Password_Policy_ForbidRepeatingCharactersCount' | ||
); | ||
const Accounts_Password_Policy_MaxLength = useSetting('Accounts_Password_Policy_MaxLength'); | ||
const Accounts_Password_Policy_MinLength = useSetting('Accounts_Password_Policy_MinLength'); | ||
|
||
const passwordPolicies: IPasswordPolicy[] | null = useMemo(() => { | ||
if (!Accounts_Password_Policy_Enabled) return null; | ||
|
||
const policies = []; | ||
|
||
if (Accounts_Password_Policy_AtLeastOneLowercase) { | ||
policies.push({ | ||
name: 'AtLeastOneLowercase', | ||
label: i18n.t('At_Least_1_Lowercase_Letter'), | ||
regex: new RegExp('[a-z]') | ||
}); | ||
} | ||
|
||
if (Accounts_Password_Policy_AtLeastOneUppercase) { | ||
policies.push({ | ||
name: 'AtLeastOneUppercase', | ||
label: i18n.t('At_Least_1_Uppercase_Letter'), | ||
regex: new RegExp('[A-Z]') | ||
}); | ||
} | ||
|
||
if (Accounts_Password_Policy_AtLeastOneNumber) { | ||
policies.push({ | ||
name: 'AtLeastOneNumber', | ||
label: i18n.t('At_Least_1_Number'), | ||
regex: new RegExp('[0-9]') | ||
}); | ||
} | ||
|
||
if (Accounts_Password_Policy_AtLeastOneSpecialCharacter) { | ||
policies.push({ | ||
name: 'AtLeastOneSpecialCharacter', | ||
label: i18n.t('At_Least_1_Symbol'), | ||
regex: new RegExp('[^A-Za-z0-9 ]') | ||
}); | ||
} | ||
|
||
if (Accounts_Password_Policy_ForbidRepeatingCharacters) { | ||
policies.push({ | ||
name: 'ForbidRepeatingCharacters', | ||
label: i18n.t('Max_Repeating_Characters', { quantity: Accounts_Password_Policy_ForbidRepeatingCharactersCount }), | ||
regex: new RegExp(`(.)\\1{${Accounts_Password_Policy_ForbidRepeatingCharactersCount},}`) | ||
}); | ||
} | ||
|
||
if (Accounts_Password_Policy_MaxLength !== -1) { | ||
policies.push({ | ||
name: 'MaxLength', | ||
label: i18n.t('At_Most_Characters', { quantity: Accounts_Password_Policy_MaxLength }), | ||
regex: new RegExp(`.{1,${Accounts_Password_Policy_MaxLength}}`) | ||
}); | ||
} | ||
|
||
if (Accounts_Password_Policy_MinLength !== -1) { | ||
policies.push({ | ||
name: 'MinLength', | ||
label: i18n.t('At_Least_Characters', { quantity: Accounts_Password_Policy_MinLength }), | ||
regex: new RegExp(`.{${Accounts_Password_Policy_MinLength},}`) | ||
}); | ||
} | ||
|
||
return policies; | ||
}, [ | ||
Accounts_Password_Policy_AtLeastOneLowercase, | ||
Accounts_Password_Policy_Enabled, | ||
Accounts_Password_Policy_AtLeastOneNumber, | ||
Accounts_Password_Policy_AtLeastOneSpecialCharacter, | ||
Accounts_Password_Policy_AtLeastOneUppercase, | ||
Accounts_Password_Policy_ForbidRepeatingCharacters, | ||
Accounts_Password_Policy_ForbidRepeatingCharactersCount, | ||
Accounts_Password_Policy_MaxLength, | ||
Accounts_Password_Policy_MinLength | ||
]); | ||
|
||
const isPasswordValid = () => { | ||
if (password !== confirmPassword) return false; | ||
|
||
if (!passwordPolicies) return true; | ||
return passwordPolicies.every(policy => policy.regex.test(password)); | ||
}; | ||
|
||
return { | ||
passwordPolicies, | ||
isPasswordValid | ||
}; | ||
}; | ||
|
||
export default useVerifyPassword; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.