@@ -12,8 +12,13 @@ import { Input } from '@/components/ui/input';
1212import { Button } from '@/components/ui/button' ;
1313import { Card } from '@/components/ui/card' ;
1414import { Label } from '@/components/ui/label' ;
15- import { Plus } from 'lucide-react' ;
15+ import { PhoneCall , Plus } from 'lucide-react' ;
1616import DefaultHeader from '@/components/Header/DefaultHeader' ;
17+ import { MdEmail } from 'react-icons/md' ;
18+ import { useSelector } from 'react-redux' ;
19+ import { addContactInfo , selectContacts } from '@/store/contacts' ;
20+ import { useDispatch } from '@/store/hooks' ;
21+ import { useStoreNewContactMutation } from '@/store/api/get-verified' ;
1722
1823const isValidEmail = ( email : string ) =>
1924 / ^ [ ^ \s @ ] + @ [ ^ \s @ ] + \. [ ^ \s @ ] + $ / . test ( email ) ;
@@ -26,12 +31,16 @@ type Contact = {
2631} ;
2732
2833export default function ContactPage ( ) {
29- const [ contacts , setContacts ] = useState < Contact [ ] > ( [ ] ) ;
3034 const [ type , setType ] = useState < 'email' | 'phone' > ( 'email' ) ;
3135 const [ value , setValue ] = useState ( '' ) ;
3236 const [ error , setError ] = useState ( '' ) ;
3337
34- const handleAdd = ( ) => {
38+ const contacts = useSelector ( selectContacts ) ;
39+ const dispatch = useDispatch ( ) ;
40+
41+ const [ mutate , { isLoading } ] = useStoreNewContactMutation ( ) ;
42+
43+ const handleAdd = async ( ) => {
3544 if ( type === 'email' && ! isValidEmail ( value ) ) {
3645 setError ( 'Invalid email address' ) ;
3746 return ;
@@ -41,9 +50,18 @@ export default function ContactPage() {
4150 return ;
4251 }
4352
44- setContacts ( [ ...contacts , { type, value } ] ) ;
45- setValue ( '' ) ;
46- setError ( '' ) ;
53+ mutate ( value ) . then ( ( res ) => {
54+ if ( res . error ) {
55+ setError ( ( res . error as { message : string } ) ?. message ?. toString ( ) ) ;
56+
57+ return ;
58+ }
59+
60+ dispatch ( addContactInfo ( { type, value } ) ) ;
61+
62+ setValue ( '' ) ;
63+ setError ( '' ) ;
64+ } ) ;
4765 } ;
4866
4967 return (
@@ -63,19 +81,15 @@ export default function ContactPage() {
6381 className = "flex items-center justify-between p-4 text-sm"
6482 >
6583 < span className = "capitalize" > { c . type } :</ span >
66- < span > { c . value } </ span >
84+ < span > ****************** </ span >
6785 </ Card >
6886 ) ) }
6987 </ div >
7088 </ div >
7189
72- { /* Floating Action Button and Dialog */ }
7390 < Dialog >
7491 < DialogTrigger asChild >
75- < Button
76- className = "h-12 bg-primary/60 p-0 text-black hover:bg-primary/90"
77- variant = "default"
78- >
92+ < Button className = "h-12 p-0 text-black" variant = "default" >
7993 < Plus size = { 24 } />
8094 </ Button >
8195 </ DialogTrigger >
@@ -93,6 +107,7 @@ export default function ContactPage() {
93107 setError ( '' ) ;
94108 } }
95109 >
110+ < MdEmail />
96111 Email
97112 </ Button >
98113 < Button
@@ -102,6 +117,7 @@ export default function ContactPage() {
102117 setError ( '' ) ;
103118 } }
104119 >
120+ < PhoneCall />
105121 Phone
106122 </ Button >
107123 </ div >
@@ -124,7 +140,7 @@ export default function ContactPage() {
124140 { error && < p className = "mt-1 text-xs text-red-500" > { error } </ p > }
125141 </ div >
126142
127- < Button onClick = { handleAdd } className = "w-full" >
143+ < Button disabled = { isLoading } onClick = { handleAdd } className = "w-full" >
128144 Save Contact
129145 </ Button >
130146 </ div >
0 commit comments