@@ -2,127 +2,130 @@ import React, { useState, useEffect } from "react";
2
2
import "./creation.scss" ;
3
3
import Spinner from "../spinner/spinner" ;
4
4
import {
5
- createABHAAddress ,
6
- getAbhaAddressSuggestions ,
5
+ createABHAAddress ,
6
+ getAbhaAddressSuggestions ,
7
7
} from "../../api/hipServiceApi" ;
8
8
import Footer from "./Footer" ;
9
9
import { cmSuffixProperty } from "../../api/constants" ;
10
10
import { Autocomplete , TextField , InputAdornment } from "@mui/material" ;
11
11
12
12
const CreateABHAAddress = ( props ) => {
13
- const [ loader , setLoader ] = useState ( false ) ;
14
- const [ error , setError ] = useState ( "" ) ;
15
- const [ newAbhaAddress , setNewAbhaAddress ] = [
16
- props . newAbhaAddress ,
17
- props . setNewAbhaAddress ,
18
- ] ;
19
- const cmSuffix = localStorage . getItem ( cmSuffixProperty ) ;
13
+ const [ loader , setLoader ] = useState ( false ) ;
14
+ const [ error , setError ] = useState ( "" ) ;
15
+ const [ newAbhaAddress , setNewAbhaAddress ] = [
16
+ props . newAbhaAddress ,
17
+ props . setNewAbhaAddress ,
18
+ ] ;
19
+ const cmSuffix = localStorage . getItem ( cmSuffixProperty ) ;
20
20
const [ loadingAbhaAddressSuggestions , setLoadingAbhaAddressSuggestions ] = useState ( false ) ;
21
- const [ abhaAddressSuggestions , setAbhaAddressSuggestions ] = useState ( [ ] ) ;
21
+ const [ abhaAddressSuggestions , setAbhaAddressSuggestions ] = useState ( [ ] ) ;
22
22
23
- useEffect ( async ( ) => {
23
+ useEffect ( async ( ) => {
24
24
setLoadingAbhaAddressSuggestions ( true ) ;
25
- let response = await getAbhaAddressSuggestions ( ) ;
26
- if ( response . data !== undefined ) {
25
+ let response = await getAbhaAddressSuggestions ( ) ;
26
+ if ( response . data !== undefined ) {
27
27
setLoadingAbhaAddressSuggestions ( false ) ;
28
- const fetchedOptions = response . data . abhaAddressList . map ( ( item ) => ( {
29
- label : item ,
30
- value : item ,
31
- } ) ) ;
32
- setAbhaAddressSuggestions ( fetchedOptions ) ;
33
- } else {
28
+ const fetchedOptions = response . data . abhaAddressList . map ( ( item ) => ( {
29
+ label : item ,
30
+ value : item ,
31
+ } ) ) ;
32
+ setAbhaAddressSuggestions ( fetchedOptions ) ;
33
+ } else {
34
34
setLoadingAbhaAddressSuggestions ( false ) ;
35
- console . error ( "An error occurred while getting suggestions" ) ;
35
+ console . error ( "An error occurred while getting suggestions" ) ;
36
+ }
37
+ } , [ ] ) ;
38
+
39
+ const validateAbhaAddress = ( newAbhaAddress ) => {
40
+ const MIN_LENGTH = 8 ;
41
+ const MAX_LENGTH = 18 ;
42
+
43
+ if ( newAbhaAddress . length < MIN_LENGTH || newAbhaAddress . length > MAX_LENGTH ) {
44
+ return `ABHA Address must be between ${ MIN_LENGTH } and ${ MAX_LENGTH } characters.` ;
36
45
}
37
- } , [ ] ) ;
38
46
39
- const validateAbhaAddress = ( newAbhaAddress ) => {
40
- if ( newAbhaAddress . length < 8 || newAbhaAddress . length > 18 ) {
41
- return "ABHA Address must be at least 8 characters long and must not exceed 18 characters ." ;
42
- }
43
-
44
- const regex = / ^ (? ! [ . _ ] ) (? ! .* \. .* \. .* ) (? ! .* _ .* _ .* ) [ a - z A - Z 0 - 9 . _ ] * (?< ! [ . _ ] ) $ / ;
45
-
46
- if ( ! regex . test ( newAbhaAddress ) ) {
47
- return (
48
- < div >
49
- ABHA Address can contain letters, numbers, one dot (.) or one
50
- underscore (_).
51
- < br />
52
- Dot(.) or underscore(_) should be in the middle.
53
- </ div >
54
- ) ;
55
- }
56
- return null ;
57
- } ;
58
- async function onCreate ( ) {
59
- setError ( "" ) ;
47
+ const abhaRegex = / ^ (? ! [ . _ ] ) (? ! .* \. .* \. .* ) (? ! .* _ .* _ .* ) [ a - z A - Z 0 - 9 . _ ] * (?< ! [ . _ ] ) $ / ;
48
+
49
+ if ( ! abhaRegex . test ( newAbhaAddress ) ) {
50
+ return (
51
+ < div >
52
+ ABHA Address can only contain letters, numbers, a dot (.) or an underscore (_).
53
+ < br />
54
+ The dot (.) or underscore (_) must not be at the beginning or end and should not appear consecutively.
55
+ </ div >
56
+ ) ;
57
+ }
58
+ return null ;
59
+ } ;
60
+
61
+ async function onCreate ( ) {
62
+ setError ( "" ) ;
60
63
const validationError = validateAbhaAddress ( newAbhaAddress ) ;
61
64
if ( validationError ) {
62
65
setError ( validationError ) ;
63
66
} else {
64
- setLoader ( true ) ;
65
- var response = await createABHAAddress ( newAbhaAddress ) ;
66
- setLoader ( false ) ;
67
- if ( response . error ) {
68
- setError ( response . error . message ) ;
69
- } else {
70
- setNewAbhaAddress ( newAbhaAddress . concat ( cmSuffix ) ) ;
71
- props . setABHAAddressCreated ( true ) ;
72
- }
73
- }
74
- }
67
+ setLoader ( true ) ;
68
+ var response = await createABHAAddress ( newAbhaAddress ) ;
69
+ setLoader ( false ) ;
70
+ if ( response . error ) {
71
+ setError ( response . error . message ) ;
72
+ } else {
73
+ setNewAbhaAddress ( newAbhaAddress . concat ( cmSuffix ) ) ;
74
+ props . setABHAAddressCreated ( true ) ;
75
+ }
76
+ }
77
+ }
75
78
76
- return (
77
- < div >
78
- < div className = "abha-address" >
79
- < label htmlFor = "abhaAdddress" > Enter custom ABHA Address or Select from suggestions </ label >
80
- < div className = "abha-adddress-input" >
81
- < div >
82
- < Autocomplete
83
- id = "free-solo-demo"
84
- freeSolo
85
- options = { abhaAddressSuggestions . map ( ( option ) => option . label ) }
79
+ return (
80
+ < div >
81
+ < div className = "abha-address" >
82
+ < label htmlFor = "abhaAdddress" > Enter custom ABHA Address or Select from suggestions </ label >
83
+ < div className = "abha-adddress-input" >
84
+ < div >
85
+ < Autocomplete
86
+ id = "free-solo-demo"
87
+ freeSolo
88
+ options = { abhaAddressSuggestions . map ( ( option ) => option . label ) }
86
89
loading = { loadingAbhaAddressSuggestions }
87
- inputValue = { newAbhaAddress }
88
- onInputChange = { ( event , value ) => setNewAbhaAddress ( value ) }
89
- renderInput = { ( params ) => (
90
- < TextField
91
- { ...params }
92
- id = "abha-address-input"
93
- label = "ABHA Address"
94
- InputProps = { {
95
- ...params . InputProps ,
96
- endAdornment : (
97
- < InputAdornment position = "end" > { cmSuffix } </ InputAdornment >
98
- ) ,
99
- } }
100
- noOptionsText = {
90
+ inputValue = { newAbhaAddress }
91
+ onInputChange = { ( event , value ) => setNewAbhaAddress ( value ) }
92
+ renderInput = { ( params ) => (
93
+ < TextField
94
+ { ...params }
95
+ id = "abha-address-input"
96
+ label = "ABHA Address"
97
+ InputProps = { {
98
+ ...params . InputProps ,
99
+ endAdornment : (
100
+ < InputAdornment position = "end" > { cmSuffix } </ InputAdornment >
101
+ ) ,
102
+ } }
103
+ noOptionsText = {
101
104
loadingAbhaAddressSuggestions
102
- ? "Getting suggestions..."
103
- : "No suggestions"
104
- }
105
- />
106
- ) }
107
- />
108
- </ div >
109
- </ div >
110
- </ div >
111
- < div className = "center" style = { { paddingTop : "20px" } } >
112
- < button
113
- type = "button"
114
- className = "proceed"
115
- disabled = { newAbhaAddress === "" }
116
- onClick = { onCreate }
117
- >
118
- Create
119
- </ button >
120
- </ div >
121
- { loader && < Spinner /> }
122
- { error !== "" && < h6 className = "error" > { error } </ h6 > }
123
- < Footer setBack = { props . setBack } />
124
- </ div >
125
- ) ;
105
+ ? "Getting suggestions..."
106
+ : "No suggestions"
107
+ }
108
+ />
109
+ ) }
110
+ />
111
+ </ div >
112
+ </ div >
113
+ </ div >
114
+ < div className = "center" style = { { paddingTop : "20px" } } >
115
+ < button
116
+ type = "button"
117
+ className = "proceed"
118
+ disabled = { newAbhaAddress === "" }
119
+ onClick = { onCreate }
120
+ >
121
+ Create
122
+ </ button >
123
+ </ div >
124
+ { loader && < Spinner /> }
125
+ { error !== "" && < h6 className = "error" > { error } </ h6 > }
126
+ < Footer setBack = { props . setBack } />
127
+ </ div >
128
+ ) ;
126
129
} ;
127
130
128
131
export default CreateABHAAddress ;
0 commit comments