@@ -2,6 +2,7 @@ import React, { useState, useEffect } from "react";
2
2
import Parse from "parse" ;
3
3
import Loader from "../primitives/Loader" ;
4
4
import { useTranslation } from "react-i18next" ;
5
+ import { emailRegex } from "../constant/const" ;
5
6
6
7
const AddSigner = ( props ) => {
7
8
const { t } = useTranslation ( ) ;
@@ -47,139 +48,143 @@ const AddSigner = (props) => {
47
48
const handleSubmit = async ( e ) => {
48
49
e . preventDefault ( ) ;
49
50
e . stopPropagation ( ) ;
50
- setIsLoader ( true ) ;
51
- if ( localStorage . getItem ( "TenantId" ) ) {
52
- try {
53
- const user = Parse . User . current ( ) ;
54
- const query = new Parse . Query ( "contracts_Contactbook" ) ;
55
- query . equalTo ( "CreatedBy" , user ) ;
56
- query . notEqualTo ( "IsDeleted" , true ) ;
57
- query . equalTo ( "Email" , email ) ;
58
- const res = await query . first ( ) ;
59
- // console.log(res);
60
- if ( ! res ) {
61
- const contactQuery = new Parse . Object ( "contracts_Contactbook" ) ;
62
- contactQuery . set ( "Name" , name ) ;
63
- if ( phone ) {
64
- contactQuery . set ( "Phone" , phone ) ;
65
- }
66
- contactQuery . set ( "Email" , email ) ;
67
- contactQuery . set ( "UserRole" , "contracts_Guest" ) ;
68
-
69
- contactQuery . set ( "TenantId" , {
70
- __type : "Pointer" ,
71
- className : "partners_Tenant" ,
72
- objectId : localStorage . getItem ( "TenantId" )
73
- } ) ;
74
-
75
- try {
76
- const _users = Parse . Object . extend ( "User" ) ;
77
- const _user = new _users ( ) ;
78
- _user . set ( "name" , name ) ;
79
- _user . set ( "username" , email ) ;
80
- _user . set ( "email" , email ) ;
81
- _user . set ( "password" , email ) ;
51
+ if ( ! emailRegex . test ( email ) ) {
52
+ alert ( "Please enter a valid email address." ) ;
53
+ } else {
54
+ setIsLoader ( true ) ;
55
+ if ( localStorage . getItem ( "TenantId" ) ) {
56
+ try {
57
+ const user = Parse . User . current ( ) ;
58
+ const query = new Parse . Query ( "contracts_Contactbook" ) ;
59
+ query . equalTo ( "CreatedBy" , user ) ;
60
+ query . notEqualTo ( "IsDeleted" , true ) ;
61
+ query . equalTo ( "Email" , email ) ;
62
+ const res = await query . first ( ) ;
63
+ // console.log(res);
64
+ if ( ! res ) {
65
+ const contactQuery = new Parse . Object ( "contracts_Contactbook" ) ;
66
+ contactQuery . set ( "Name" , name ) ;
82
67
if ( phone ) {
83
- _user . set ( "phone " , phone ) ;
68
+ contactQuery . set ( "Phone " , phone ) ;
84
69
}
85
- const user = await _user . save ( ) ;
86
- if ( user ) {
87
- const currentUser = Parse . User . current ( ) ;
88
- contactQuery . set (
89
- "CreatedBy" ,
90
- Parse . User . createWithoutData ( currentUser . id )
91
- ) ;
70
+ contactQuery . set ( "Email" , email ) ;
71
+ contactQuery . set ( "UserRole" , "contracts_Guest" ) ;
92
72
93
- contactQuery . set ( "UserId" , user ) ;
94
- const acl = new Parse . ACL ( ) ;
95
- acl . setPublicReadAccess ( true ) ;
96
- acl . setPublicWriteAccess ( true ) ;
97
- acl . setReadAccess ( currentUser . id , true ) ;
98
- acl . setWriteAccess ( currentUser . id , true ) ;
73
+ contactQuery . set ( "TenantId" , {
74
+ __type : "Pointer" ,
75
+ className : "partners_Tenant" ,
76
+ objectId : localStorage . getItem ( "TenantId" )
77
+ } ) ;
99
78
100
- contactQuery . setACL ( acl ) ;
79
+ try {
80
+ const _users = Parse . Object . extend ( "User" ) ;
81
+ const _user = new _users ( ) ;
82
+ _user . set ( "name" , name ) ;
83
+ _user . set ( "username" , email ) ;
84
+ _user . set ( "email" , email ) ;
85
+ _user . set ( "password" , email ) ;
86
+ if ( phone ) {
87
+ _user . set ( "phone" , phone ) ;
88
+ }
89
+ const user = await _user . save ( ) ;
90
+ if ( user ) {
91
+ const currentUser = Parse . User . current ( ) ;
92
+ contactQuery . set (
93
+ "CreatedBy" ,
94
+ Parse . User . createWithoutData ( currentUser . id )
95
+ ) ;
101
96
102
- const res = await contactQuery . save ( ) ;
97
+ contactQuery . set ( "UserId" , user ) ;
98
+ const acl = new Parse . ACL ( ) ;
99
+ acl . setPublicReadAccess ( true ) ;
100
+ acl . setPublicWriteAccess ( true ) ;
101
+ acl . setReadAccess ( currentUser . id , true ) ;
102
+ acl . setWriteAccess ( currentUser . id , true ) ;
103
103
104
- const parseData = JSON . parse ( JSON . stringify ( res ) ) ;
105
- if ( props . details ) {
106
- props . details ( {
107
- value : parseData [ props . valueKey ] ,
108
- label : parseData [ props . displayKey ]
109
- } ) ;
110
- }
111
- if ( props . closePopup ) {
112
- props . closePopup ( ) ;
113
- }
114
- if ( props . handleUserData ) {
115
- props . handleUserData ( parseData ) ;
116
- }
104
+ contactQuery . setACL ( acl ) ;
117
105
118
- setIsLoader ( false ) ;
119
- // Reset the form fields
120
- setAddYourself ( false ) ;
121
- setName ( "" ) ;
122
- setPhone ( "" ) ;
123
- setEmail ( "" ) ;
124
- }
125
- } catch ( err ) {
126
- console . log ( "err " , err ) ;
127
- if ( err . code === 202 ) {
128
- const params = { email : email } ;
129
- const userRes = await Parse . Cloud . run ( "getUserId" , params ) ;
130
- const currentUser = Parse . User . current ( ) ;
131
- contactQuery . set (
132
- "CreatedBy" ,
133
- Parse . User . createWithoutData ( currentUser . id )
134
- ) ;
106
+ const res = await contactQuery . save ( ) ;
135
107
136
- contactQuery . set ( "UserId" , {
137
- __type : "Pointer" ,
138
- className : "_User" ,
139
- objectId : userRes . id
140
- } ) ;
141
- const acl = new Parse . ACL ( ) ;
142
- acl . setPublicReadAccess ( true ) ;
143
- acl . setPublicWriteAccess ( true ) ;
144
- acl . setReadAccess ( currentUser . id , true ) ;
145
- acl . setWriteAccess ( currentUser . id , true ) ;
108
+ const parseData = JSON . parse ( JSON . stringify ( res ) ) ;
109
+ if ( props . details ) {
110
+ props . details ( {
111
+ value : parseData [ props . valueKey ] ,
112
+ label : parseData [ props . displayKey ]
113
+ } ) ;
114
+ }
115
+ if ( props . closePopup ) {
116
+ props . closePopup ( ) ;
117
+ }
118
+ if ( props . handleUserData ) {
119
+ props . handleUserData ( parseData ) ;
120
+ }
146
121
147
- contactQuery . setACL ( acl ) ;
148
- const res = await contactQuery . save ( ) ;
122
+ setIsLoader ( false ) ;
123
+ // Reset the form fields
124
+ setAddYourself ( false ) ;
125
+ setName ( "" ) ;
126
+ setPhone ( "" ) ;
127
+ setEmail ( "" ) ;
128
+ }
129
+ } catch ( err ) {
130
+ console . log ( "err " , err ) ;
131
+ if ( err . code === 202 ) {
132
+ const params = { email : email } ;
133
+ const userRes = await Parse . Cloud . run ( "getUserId" , params ) ;
134
+ const currentUser = Parse . User . current ( ) ;
135
+ contactQuery . set (
136
+ "CreatedBy" ,
137
+ Parse . User . createWithoutData ( currentUser . id )
138
+ ) ;
149
139
150
- const parseData = JSON . parse ( JSON . stringify ( res ) ) ;
151
- if ( props . details ) {
152
- props . details ( {
153
- value : parseData [ props . valueKey ] ,
154
- label : parseData [ props . displayKey ]
140
+ contactQuery . set ( "UserId" , {
141
+ __type : "Pointer" ,
142
+ className : "_User" ,
143
+ objectId : userRes . id
155
144
} ) ;
145
+ const acl = new Parse . ACL ( ) ;
146
+ acl . setPublicReadAccess ( true ) ;
147
+ acl . setPublicWriteAccess ( true ) ;
148
+ acl . setReadAccess ( currentUser . id , true ) ;
149
+ acl . setWriteAccess ( currentUser . id , true ) ;
150
+
151
+ contactQuery . setACL ( acl ) ;
152
+ const res = await contactQuery . save ( ) ;
153
+
154
+ const parseData = JSON . parse ( JSON . stringify ( res ) ) ;
155
+ if ( props . details ) {
156
+ props . details ( {
157
+ value : parseData [ props . valueKey ] ,
158
+ label : parseData [ props . displayKey ]
159
+ } ) ;
160
+ }
161
+ if ( props . closePopup ) {
162
+ props . closePopup ( ) ;
163
+ }
164
+ if ( props . handleUserData ) {
165
+ props . handleUserData ( parseData ) ;
166
+ }
167
+ setIsLoader ( false ) ;
168
+ // Reset the form fields
169
+ setAddYourself ( false ) ;
170
+ setName ( "" ) ;
171
+ setPhone ( "" ) ;
172
+ setEmail ( "" ) ;
156
173
}
157
- if ( props . closePopup ) {
158
- props . closePopup ( ) ;
159
- }
160
- if ( props . handleUserData ) {
161
- props . handleUserData ( parseData ) ;
162
- }
163
- setIsLoader ( false ) ;
164
- // Reset the form fields
165
- setAddYourself ( false ) ;
166
- setName ( "" ) ;
167
- setPhone ( "" ) ;
168
- setEmail ( "" ) ;
169
174
}
175
+ } else {
176
+ alert ( t ( "add-signer-alert" ) ) ;
177
+ setIsLoader ( false ) ;
170
178
}
171
- } else {
172
- alert ( t ( "add-signer-alert" ) ) ;
179
+ } catch ( err ) {
180
+ console . log ( "err in fetch contact" , err ) ;
173
181
setIsLoader ( false ) ;
182
+ alert ( t ( "something-went-wrong-mssg" ) ) ;
174
183
}
175
- } catch ( err ) {
176
- console . log ( "err in fetch contact" , err ) ;
184
+ } else {
177
185
setIsLoader ( false ) ;
178
186
alert ( t ( "something-went-wrong-mssg" ) ) ;
179
187
}
180
- } else {
181
- setIsLoader ( false ) ;
182
- alert ( t ( "something-went-wrong-mssg" ) ) ;
183
188
}
184
189
} ;
185
190
0 commit comments