@@ -62,8 +62,10 @@ function ContactPopup({
6262 } ;
6363
6464 useEffect ( ( ) => {
65- setFilteredData ( contacts ) ;
66- checkAllContacts ( contacts ) ;
65+ if ( contacts && ! isLoading ) {
66+ setFilteredData ( contacts ) ;
67+ checkAllContacts ( contacts ) ;
68+ }
6769 } , [ contacts , isLoading ] ) ;
6870
6971 // get contacts
@@ -112,7 +114,7 @@ function ContactPopup({
112114 . finally ( ( ) => {
113115 setIsloading ( false ) ;
114116 } ) ;
115- } , 1500 )
117+ } , 0 ) ;
116118 } , [ show ] ) ;
117119
118120 const getPrimaryEmail = ( contact ) => {
@@ -154,9 +156,52 @@ function ContactPopup({
154156
155157 const importContact = ( data ) => {
156158 if ( data ) {
157- checkAllContacts ( data ) ;
158- setimportContactDialog ( false ) ;
159- setFilteredData ( data ) ;
159+ axios
160+ . get ( `${ API_BASE_URL } /contacts/list/${ user ?. user_id } ` )
161+ . then ( ( response ) => {
162+ // save user details
163+ // before saving contacts to the reducer make all the emails unique
164+ const {
165+ data : { data : contacts = [ ] }
166+ } = response ;
167+ const uniqueEmails = [ ] ;
168+ // console.log(`Got ${contacts.length} contacts from server`);
169+ const uniqueContacts = contacts . filter ( ( contactObj ) => {
170+ if ( contactObj . email && contactObj . email . length ) {
171+ let emailExists = false ;
172+ for ( let i = 0 ; i < contactObj . email . length ; i ++ ) {
173+ const emailObj = { ...contactObj . email [ i ] } ;
174+ if (
175+ emailObj && emailObj . address && uniqueEmails . indexOf ( emailObj . address ) !== - 1
176+ ) {
177+ emailExists = true ;
178+ break ;
179+ } else {
180+ uniqueEmails . push ( emailObj . address ) ;
181+ }
182+ }
183+ if ( emailExists ) {
184+ return false ;
185+ }
186+ return true ;
187+ }
188+ return true ;
189+ } ) ;
190+ checkAllContacts ( uniqueContacts ) ;
191+ setimportContactDialog ( false ) ;
192+ setFilteredData ( uniqueContacts ) ;
193+ dispatch ( { type : "update_contacts" , payload : uniqueContacts } ) ;
194+ } )
195+ . catch ( ( error ) => {
196+ if ( error ?. response ?. data ) {
197+ toast . error ( error . response . data . message ) ;
198+ } else {
199+ toast . error ( 'Failed to Load Contacts' ) ;
200+ }
201+ } )
202+ . finally ( ( ) => {
203+ setIsloading ( false ) ;
204+ } ) ;
160205 }
161206 } ;
162207
@@ -299,23 +344,36 @@ function ContactPopup({
299344 Import
300345 </ button >
301346 </ div >
302- < div className = { styles . search__wrapper } >
303- < button
304- className = { styles . import__button }
305- onClick = { ( ) => {
306- if ( selectedContacts . length === contacts . length ) {
307- setSelectedContacts ( [ ] )
308- } else {
309- setSelectedContacts ( contacts )
310- }
311- } }
312- >
313- { selectedContacts . length === contacts . length ? 'Unselect All' : 'Select All' }
314- </ button >
315- </ div >
347+
316348 < div className = { styles . data__wrapper } >
317349 < div > { isLoading && < LoaderIconBlue /> } </ div >
318350
351+ { filteredData . length > 0 && (
352+ < div className = { styles . data_row_container } >
353+ { /* TEXT */ }
354+ < div className = { styles . selectAllTextContainer } >
355+ < h6 > { selectedContacts . length === contacts . length ? 'Unselect All' : 'Select All' } </ h6 >
356+ </ div >
357+ { /* ICONS */ }
358+ < div
359+ className = { styles . icon }
360+ onClick = { ( ) => {
361+ if ( selectedContacts . length === contacts . length ) {
362+ setSelectedContacts ( [ ] )
363+ } else {
364+ setSelectedContacts ( contacts )
365+ }
366+ } }
367+ >
368+ { selectedContacts . length === contacts . length ? (
369+ < BsCheckCircleFill className = { styles . checked } />
370+ ) : (
371+ < GoPrimitiveDot className = { styles . unchecked } />
372+ ) }
373+ </ div >
374+ </ div >
375+ ) }
376+
319377 { filteredData . map ( ( contact ) => (
320378 < div className = { styles . data_row_container } key = { nanoid ( ) } >
321379 { /* TEXT */ }
0 commit comments