1- "use client"
2-
31import { createPasswordItem } from "@/app/actions" ;
42import { Button } from "@/components/ui/button" ;
53import { Dialog , DialogContent , DialogFooter , DialogHeader , DialogTitle } from "@/components/ui/dialog" ;
64import { Input } from "@/components/ui/input" ;
75import { encrypt } from "@/utils/encryption" ;
86import { useUser } from "@clerk/nextjs" ;
97import { Loader2 } from "lucide-react" ;
10- import { useState } from "react" ;
8+ import { ChangeEvent , useState } from "react" ;
119import toast from "react-hot-toast" ;
1210import { z } from "zod" ;
1311
12+ const initialPasswordItemState = {
13+ name : "" ,
14+ username : "" ,
15+ website : "" ,
16+ password : "" ,
17+ }
18+
1419export const CreatePasswordDialog = ( {
1520 open,
16- onClose,
21+ onClose,
1722} : {
1823 open : boolean ;
19- onClose : ( ) => void ;
24+ onClose : ( ) => void
2025} ) => {
21- const [ name , setName ] = useState ( "" ) ;
22- const [ username , setUsername ] = useState ( "" ) ;
23- const [ website , setWebsite ] = useState ( "" ) ;
24- const [ password , setPassword ] = useState ( "" ) ;
26+
27+ const [ passwordItem , setPasswordItem ] = useState ( initialPasswordItemState )
28+
2529 const [ loading , setLoading ] = useState ( false ) ;
2630
2731 const { user : clerkuser } = useUser ( ) ;
@@ -47,12 +51,7 @@ export const CreatePasswordDialog = ({
4751
4852 const handleSave = async ( ) => {
4953 setLoading ( true ) ;
50- const validationResult = passwordSchema . safeParse ( {
51- name,
52- username,
53- website,
54- password,
55- } ) ;
54+ const validationResult = passwordSchema . safeParse ( passwordItem ) ;
5655
5756 if ( ! validationResult . success ) {
5857 const errorMessage =
@@ -64,11 +63,12 @@ export const CreatePasswordDialog = ({
6463
6564 try {
6665 await createPasswordItem (
67- encrypt ( username , clerkuser ) ,
68- encrypt ( website , clerkuser ) ,
69- encrypt ( password , clerkuser )
66+ encrypt ( passwordItem . username , clerkuser ) ,
67+ encrypt ( passwordItem . website , clerkuser ) ,
68+ encrypt ( passwordItem . password , clerkuser )
7069 ) ;
7170 toast . success ( "Password created" ) ;
71+ setPasswordItem ( initialPasswordItemState )
7272 onClose ( ) ;
7373 } catch ( error ) {
7474 toast . error ( "Failed to create password" ) ;
@@ -77,6 +77,10 @@ export const CreatePasswordDialog = ({
7777 }
7878 } ;
7979
80+ const handleChange = ( e : ChangeEvent < HTMLInputElement > ) => {
81+ setPasswordItem ( prevState => ( { ...prevState , [ e . target . name ] : e . target . value } ) )
82+ }
83+
8084 return (
8185 < Dialog open = { open } onOpenChange = { onClose } >
8286 < DialogContent >
@@ -87,50 +91,54 @@ export const CreatePasswordDialog = ({
8791 < div className = "relative" >
8892 < Input
8993 placeholder = "Name"
90- value = { name }
91- onChange = { ( e ) => setName ( e . target . value ) }
94+ value = { passwordItem . name }
95+ onChange = { handleChange }
9296 maxLength = { 50 }
97+ name = "name"
9398 />
94- < div className = "mt-1 text-sm text-gray-500" > { name . length } / 50</ div >
99+ < div className = "mt-1 text-sm text-gray-500" > { passwordItem . name . length } / 50</ div >
95100 </ div >
96101 < div className = "relative" >
97102 < Input
98103 placeholder = "Username"
99- value = { username }
100- onChange = { ( e ) => setUsername ( e . target . value ) }
104+ value = { passwordItem . username }
105+ onChange = { handleChange }
101106 maxLength = { 30 }
107+ name = "username"
102108 />
103109 < div className = "mt-1 text-sm text-gray-500" >
104- { username . length } / 30
110+ { passwordItem . username . length } / 30
105111 </ div >
106112 </ div >
107113 < div className = "relative" >
108114 < Input
109115 placeholder = "Website"
110- value = { website }
111- onChange = { ( e ) => setWebsite ( e . target . value ) }
116+ value = { passwordItem . website }
117+ onChange = { handleChange }
112118 maxLength = { 1024 }
119+ name = "website"
113120 />
114121 < div className = "mt-1 text-sm text-gray-500" >
115- { website . length } / 1024
122+ { passwordItem . website . length } / 1024
116123 </ div >
117124 </ div >
118125 < div className = "relative" >
119126 < Input
120127 placeholder = "Password"
121- value = { password }
122- onChange = { ( e ) => setPassword ( e . target . value ) }
128+ value = { passwordItem . password }
129+ onChange = { handleChange }
123130 type = "password"
131+ name = "password"
124132 maxLength = { 128 }
125133 />
126134 < div className = " mt-1 text-sm text-gray-500" >
127- { password . length } / 128
135+ { passwordItem . password . length } / 128
128136 </ div >
129137 </ div >
130138 </ div >
131139 < DialogFooter >
132140 < div className = "mt-4 flex justify-end gap-2" >
133- < Button variant = "outline" onClick = { onClose } >
141+ < Button variant = "outline" onClick = { ( ) => onClose ( ) } >
134142 Cancel
135143 </ Button >
136144 < Button onClick = { handleSave } >
0 commit comments