@@ -23,7 +23,7 @@ const AddSigner = (props) => {
23
23
) ;
24
24
if ( savedUserDetails && addYourself ) {
25
25
setName ( savedUserDetails . name ) ;
26
- setPhone ( savedUserDetails . phone ) ;
26
+ setPhone ( savedUserDetails ? .phone || "" ) ;
27
27
setEmail ( savedUserDetails . email ) ;
28
28
}
29
29
} , [ addYourself ] ) ;
@@ -50,145 +50,161 @@ const AddSigner = (props) => {
50
50
e . stopPropagation ( ) ;
51
51
setIsLoader ( true ) ;
52
52
if ( localStorage . getItem ( "TenantId" ) ) {
53
- try {
54
- const contactQuery = new Parse . Object ( "contracts_Contactbook" ) ;
55
- contactQuery . set ( "Name" , name ) ;
56
- contactQuery . set ( "Phone" , phone ) ;
57
- contactQuery . set ( "Email" , email ) ;
58
- contactQuery . set ( "UserRole" , "contracts_Guest" ) ;
59
-
60
- contactQuery . set ( "TenantId" , {
61
- __type : "Pointer" ,
62
- className : "partners_Tenant" ,
63
- objectId : localStorage . getItem ( "TenantId" )
64
- } ) ;
65
-
66
53
try {
67
- const _users = Parse . Object . extend ( "User" ) ;
68
- const _user = new _users ( ) ;
69
- _user . set ( "name" , name ) ;
70
- _user . set ( "username" , email ) ;
71
- _user . set ( "email" , email ) ;
72
- _user . set ( "phone" , phone ) ;
73
- _user . set ( "password" , phone ) ;
54
+ const user = Parse . User . current ( ) ;
55
+ const query = new Parse . Query ( "contracts_Contactbook" ) ;
56
+ query . equalTo ( "CreatedBy" , user ) ;
57
+ query . notEqualTo ( "IsDeleted" , true ) ;
58
+ query . equalTo ( "Email" , email ) ;
59
+ const res = await query . first ( ) ;
60
+ // console.log(res);
61
+ if ( ! res ) {
62
+ const contactQuery = new Parse . Object ( "contracts_Contactbook" ) ;
63
+ contactQuery . set ( "Name" , name ) ;
64
+ if ( phone ) {
65
+ contactQuery . set ( "Phone" , phone ) ;
66
+ }
67
+ contactQuery . set ( "Email" , email ) ;
68
+ contactQuery . set ( "UserRole" , "contracts_Guest" ) ;
74
69
75
- const user = await _user . save ( ) ;
76
- if ( user ) {
77
- const roleurl = `${ parseBaseUrl } functions/AddUserToRole` ;
78
- const headers = {
79
- "Content-Type" : "application/json" ,
80
- "X-Parse-Application-Id" : parseAppId ,
81
- sessionToken : localStorage . getItem ( "accesstoken" )
82
- } ;
83
- const body = {
84
- appName : localStorage . getItem ( "_appName" ) ,
85
- roleName : "contracts_Guest" ,
86
- userId : user . id
87
- } ;
88
- await axios . post ( roleurl , body , { headers : headers } ) ;
89
- const currentUser = Parse . User . current ( ) ;
90
- contactQuery . set (
91
- "CreatedBy" ,
92
- Parse . User . createWithoutData ( currentUser . id )
93
- ) ;
70
+ contactQuery . set ( "TenantId" , {
71
+ __type : "Pointer" ,
72
+ className : "partners_Tenant" ,
73
+ objectId : localStorage . getItem ( "TenantId" )
74
+ } ) ;
94
75
95
- contactQuery . set ( "UserId" , user ) ;
96
- const acl = new Parse . ACL ( ) ;
97
- acl . setPublicReadAccess ( true ) ;
98
- acl . setPublicWriteAccess ( true ) ;
99
- acl . setReadAccess ( currentUser . id , true ) ;
100
- acl . setWriteAccess ( currentUser . id , true ) ;
76
+ try {
77
+ const _users = Parse . Object . extend ( "User" ) ;
78
+ const _user = new _users ( ) ;
79
+ _user . set ( "name" , name ) ;
80
+ _user . set ( "username" , email ) ;
81
+ _user . set ( "email" , email ) ;
82
+ _user . set ( "password" , email ) ;
83
+ if ( phone ) {
84
+ _user . set ( "phone" , phone ) ;
85
+ }
86
+ const user = await _user . save ( ) ;
87
+ if ( user ) {
88
+ const roleurl = `${ parseBaseUrl } functions/AddUserToRole` ;
89
+ const headers = {
90
+ "Content-Type" : "application/json" ,
91
+ "X-Parse-Application-Id" : parseAppId ,
92
+ sessionToken : localStorage . getItem ( "accesstoken" )
93
+ } ;
94
+ const body = {
95
+ appName : localStorage . getItem ( "_appName" ) ,
96
+ roleName : "contracts_Guest" ,
97
+ userId : user . id
98
+ } ;
99
+ await axios . post ( roleurl , body , { headers : headers } ) ;
100
+ const currentUser = Parse . User . current ( ) ;
101
+ contactQuery . set (
102
+ "CreatedBy" ,
103
+ Parse . User . createWithoutData ( currentUser . id )
104
+ ) ;
101
105
102
- contactQuery . setACL ( acl ) ;
106
+ contactQuery . set ( "UserId" , user ) ;
107
+ const acl = new Parse . ACL ( ) ;
108
+ acl . setPublicReadAccess ( true ) ;
109
+ acl . setPublicWriteAccess ( true ) ;
110
+ acl . setReadAccess ( currentUser . id , true ) ;
111
+ acl . setWriteAccess ( currentUser . id , true ) ;
103
112
104
- const res = await contactQuery . save ( ) ;
113
+ contactQuery . setACL ( acl ) ;
105
114
106
- const parseData = JSON . parse ( JSON . stringify ( res ) ) ;
107
- if ( props . details ) {
108
- props . details ( {
109
- value : parseData [ props . valueKey ] ,
110
- label : parseData [ props . displayKey ]
111
- } ) ;
112
- }
113
- if ( props . closePopup ) {
114
- props . closePopup ( ) ;
115
- }
116
- if ( props . handleUserData ) {
117
- props . handleUserData ( parseData ) ;
118
- }
115
+ const res = await contactQuery . save ( ) ;
119
116
120
- setIsLoader ( false ) ;
121
- // Reset the form fields
122
- setAddYourself ( false ) ;
123
- setName ( "" ) ;
124
- setPhone ( "" ) ;
125
- setEmail ( "" ) ;
126
- }
127
- } catch ( err ) {
128
- console . log ( "err " , err ) ;
129
- if ( err . code === 202 ) {
130
- const params = { email : email } ;
131
- const userRes = await Parse . Cloud . run ( "getUserId" , params ) ;
132
- const roleurl = `${ parseBaseUrl } functions/AddUserToRole` ;
133
- const headers = {
134
- "Content-Type" : "application/json" ,
135
- "X-Parse-Application-Id" : parseAppId ,
136
- sessionToken : localStorage . getItem ( "accesstoken" )
137
- } ;
138
- const body = {
139
- appName : localStorage . getItem ( "_appName" ) ,
140
- roleName : "contracts_Guest" ,
141
- userId : userRes . id
142
- } ;
143
- await axios . post ( roleurl , body , { headers : headers } ) ;
144
- const currentUser = Parse . User . current ( ) ;
145
- contactQuery . set (
146
- "CreatedBy" ,
147
- Parse . User . createWithoutData ( currentUser . id )
148
- ) ;
117
+ const parseData = JSON . parse ( JSON . stringify ( res ) ) ;
118
+ if ( props . details ) {
119
+ props . details ( {
120
+ value : parseData [ props . valueKey ] ,
121
+ label : parseData [ props . displayKey ]
122
+ } ) ;
123
+ }
124
+ if ( props . closePopup ) {
125
+ props . closePopup ( ) ;
126
+ }
127
+ if ( props . handleUserData ) {
128
+ props . handleUserData ( parseData ) ;
129
+ }
149
130
150
- contactQuery . set ( "UserId" , {
151
- __type : "Pointer" ,
152
- className : "_User" ,
153
- objectId : userRes . id
154
- } ) ;
155
- const acl = new Parse . ACL ( ) ;
156
- acl . setPublicReadAccess ( true ) ;
157
- acl . setPublicWriteAccess ( true ) ;
158
- acl . setReadAccess ( currentUser . id , true ) ;
159
- acl . setWriteAccess ( currentUser . id , true ) ;
131
+ setIsLoader ( false ) ;
132
+ // Reset the form fields
133
+ setAddYourself ( false ) ;
134
+ setName ( "" ) ;
135
+ setPhone ( "" ) ;
136
+ setEmail ( "" ) ;
137
+ }
138
+ } catch ( err ) {
139
+ console . log ( "err " , err ) ;
140
+ if ( err . code === 202 ) {
141
+ const params = { email : email } ;
142
+ const userRes = await Parse . Cloud . run ( "getUserId" , params ) ;
143
+ const roleurl = `${ parseBaseUrl } functions/AddUserToRole` ;
144
+ const headers = {
145
+ "Content-Type" : "application/json" ,
146
+ "X-Parse-Application-Id" : parseAppId ,
147
+ sessionToken : localStorage . getItem ( "accesstoken" )
148
+ } ;
149
+ const body = {
150
+ appName : localStorage . getItem ( "_appName" ) ,
151
+ roleName : "contracts_Guest" ,
152
+ userId : userRes . id
153
+ } ;
154
+ await axios . post ( roleurl , body , { headers : headers } ) ;
155
+ const currentUser = Parse . User . current ( ) ;
156
+ contactQuery . set (
157
+ "CreatedBy" ,
158
+ Parse . User . createWithoutData ( currentUser . id )
159
+ ) ;
160
160
161
- contactQuery . setACL ( acl ) ;
162
- const res = await contactQuery . save ( ) ;
161
+ contactQuery . set ( "UserId" , {
162
+ __type : "Pointer" ,
163
+ className : "_User" ,
164
+ objectId : userRes . id
165
+ } ) ;
166
+ const acl = new Parse . ACL ( ) ;
167
+ acl . setPublicReadAccess ( true ) ;
168
+ acl . setPublicWriteAccess ( true ) ;
169
+ acl . setReadAccess ( currentUser . id , true ) ;
170
+ acl . setWriteAccess ( currentUser . id , true ) ;
163
171
164
- const parseData = JSON . parse ( JSON . stringify ( res ) ) ;
165
- if ( props . details ) {
166
- props . details ( {
167
- value : parseData [ props . valueKey ] ,
168
- label : parseData [ props . displayKey ]
169
- } ) ;
170
- }
171
- if ( props . closePopup ) {
172
- props . closePopup ( ) ;
173
- }
174
- if ( props . handleUserData ) {
175
- props . handleUserData ( parseData ) ;
172
+ contactQuery . setACL ( acl ) ;
173
+ const res = await contactQuery . save ( ) ;
174
+
175
+ const parseData = JSON . parse ( JSON . stringify ( res ) ) ;
176
+ if ( props . details ) {
177
+ props . details ( {
178
+ value : parseData [ props . valueKey ] ,
179
+ label : parseData [ props . displayKey ]
180
+ } ) ;
181
+ }
182
+ if ( props . closePopup ) {
183
+ props . closePopup ( ) ;
184
+ }
185
+ if ( props . handleUserData ) {
186
+ props . handleUserData ( parseData ) ;
187
+ }
188
+ setIsLoader ( false ) ;
189
+ // Reset the form fields
190
+ setAddYourself ( false ) ;
191
+ setName ( "" ) ;
192
+ setPhone ( "" ) ;
193
+ setEmail ( "" ) ;
194
+ }
176
195
}
196
+ } else {
197
+ alert ( "Contact already exist!" ) ;
177
198
setIsLoader ( false ) ;
178
- // Reset the form fields
179
- setAddYourself ( false ) ;
180
- setName ( "" ) ;
181
- setPhone ( "" ) ;
182
- setEmail ( "" ) ;
183
199
}
200
+ } catch ( err ) {
201
+ console . log ( "err in fetch contact" , err ) ;
202
+ setIsLoader ( false ) ;
203
+ alert ( "something went wrong, please try again later" ) ;
184
204
}
185
- } catch ( err ) {
186
- // console.log("err", err);
205
+ } else {
187
206
setIsLoader ( false ) ;
188
- alert ( "something went wrong!" ) ;
189
- }
190
- } else {
191
- alert ( 'something went wrong, please try again later' )
207
+ alert ( "something went wrong, please try again later" ) ;
192
208
}
193
209
} ;
194
210
@@ -266,10 +282,10 @@ const AddSigner = (props) => {
266
282
type = "email"
267
283
id = "email"
268
284
value = { email }
269
- onChange = { ( e ) => setEmail ( e . target . value ) }
285
+ onChange = { ( e ) => setEmail ( e . target . value ?. toLowerCase ( ) ) }
270
286
required
271
287
disabled = { addYourself }
272
- className = "px-3 py-2 w-full border-[1px] border-gray-300 rounded focus:outline-none text-xs"
288
+ className = "px-3 py-2 w-full border-[1px] border-gray-300 rounded focus:outline-none text-xs lowercase "
273
289
/>
274
290
</ div >
275
291
< div className = "mb-3" >
@@ -278,14 +294,12 @@ const AddSigner = (props) => {
278
294
className = "block text-xs text-gray-700 font-semibold"
279
295
>
280
296
Phone
281
- < span style = { { color : "red" , fontSize : 13 } } > *</ span >
282
297
</ label >
283
298
< input
284
299
type = "text"
285
300
id = "phone"
286
301
value = { phone }
287
302
onChange = { ( e ) => setPhone ( e . target . value ) }
288
- required
289
303
disabled = { addYourself }
290
304
className = "px-3 py-2 w-full border-[1px] border-gray-300 rounded focus:outline-none text-xs"
291
305
/>
0 commit comments