11import { useState } from "react" ;
2+ import { useRouter } from "next/router" ;
23import { changePassword } from "@/api/changepassword" ;
3- import MypageModal from "../modal/MypageModal" ;
4- import Input from "../input/Input" ;
5- import Image from "next/image" ;
4+ import Input from "@/components/input/Input" ;
5+ import { toast } from "react-toastify" ;
66
77export default function ChangePassword ( ) {
8+ const router = useRouter ( ) ;
89 const [ password , setPassword ] = useState ( "" ) ;
910 const [ newPassword , setNewPassword ] = useState ( "" ) ;
1011 const [ checkNewpassword , setCheckNewPassword ] = useState ( "" ) ;
1112 const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
12- const [ errorMessage , setErrorMessage ] = useState ( "" ) ;
13- const [ successMessage , setSuccessMessage ] = useState ( "" ) ;
14- const [ showCheckNewPassword , setShowCheckNewPassword ] = useState ( false ) ;
15- const [ showSuccessModal , setShowSuccessModal ] = useState ( false ) ;
16- const [ showErrorModal , setShowErrorModal ] = useState ( false ) ;
1713
18- const toggleCheckNewPasswordVisibility = ( ) =>
19- setShowCheckNewPassword ( ! showCheckNewPassword ) ;
2014 const isPasswordMismatch =
2115 newPassword && checkNewpassword && newPassword !== checkNewpassword ;
2216 const isDisabled =
@@ -30,8 +24,6 @@ export default function ChangePassword() {
3024 if ( isDisabled ) return ;
3125
3226 setIsSubmitting ( true ) ;
33- setErrorMessage ( "" ) ;
34- setSuccessMessage ( "" ) ;
3527
3628 const result = await changePassword ( { password, newPassword } ) ;
3729
@@ -40,124 +32,87 @@ export default function ChangePassword() {
4032 result . status === 400
4133 ? result . message || "현재 비밀번호가 올바르지 않습니다."
4234 : "비밀번호 변경 중 오류가 발생했습니다." ;
43-
44- setErrorMessage ( msg ) ;
45- setShowErrorModal ( true ) ;
35+ toast . error ( msg ) ;
4636 setIsSubmitting ( false ) ;
37+
4738 return ;
4839 }
49-
50- setSuccessMessage ( "비밀번호가 성공적으로 변경되었습니다." ) ;
51- setShowSuccessModal ( true ) ;
40+ toast . success ( "비밀번호가 변경되었습니다." ) ;
5241 setPassword ( "" ) ;
5342 setNewPassword ( "" ) ;
5443 setCheckNewPassword ( "" ) ;
5544 setIsSubmitting ( false ) ;
45+ setTimeout ( ( ) => {
46+ router . reload ( ) ;
47+ } , 1700 ) ;
5648 } ;
5749
5850 return (
59- < div className = "sm:w-[672px] sm:h-[466px] w-[284px] h-[454px] bg-white rounded-[16px] shadow-md p-[24px] flex flex-col" >
60- < h2 className = "text-[18px] sm:text-[24px] font-bold mb-4" >
51+ < div
52+ className = "flex flex-col w-[284px] sm:w-[548px] md:w-[672px] min:h-[454px] sm:min-h-[466px]
53+ bg-white rounded-[16px] p-[24px]"
54+ >
55+ < h2 className = "text-black3 text-[18px] sm:text-[24px] font-bold mb-4" >
6156 비밀번호 변경
6257 </ h2 >
6358
64- < div >
65- < div className = "-mt-2" >
66- < Input
67- type = "password"
68- name = "password"
69- label = "현재 비밀번호"
70- labelClassName = "font-16r"
71- placeholder = "현재 비밀번호 입력"
72- value = { password }
73- onChange = { setPassword }
74- pattern = ".{8,}"
75- className = "max-w-[620px]"
76- />
77- < Input
78- type = "password"
79- name = "password"
80- label = "새 비밀번호"
81- labelClassName = "font-16r"
82- placeholder = "새 비밀번호 입력"
83- value = { newPassword }
84- onChange = { setNewPassword }
85- pattern = ".{8,}"
86- invalidMessage = "8자 이상 입력해주세요."
87- className = "max-w-[620px]"
88- />
59+ < div className = "flex flex-col text-black3 my-3 gap-4" >
60+ < Input
61+ type = "password"
62+ name = "password"
63+ label = "현재 비밀번호"
64+ labelClassName = "text-[14px] sm:text-base"
65+ placeholder = "현재 비밀번호 입력"
66+ value = { password }
67+ onChange = { setPassword }
68+ pattern = ".{8,}"
69+ className = "max-w-[624px] "
70+ />
71+ < Input
72+ type = "password"
73+ name = "password"
74+ label = "새 비밀번호"
75+ labelClassName = "text-[14px] sm:text-base"
76+ placeholder = "새 비밀번호 입력"
77+ value = { newPassword }
78+ onChange = { setNewPassword }
79+ pattern = ".{8,}"
80+ invalidMessage = "8자 이상 입력해주세요."
81+ className = "max-w-[624px]"
82+ />
83+ < Input
84+ type = "password"
85+ name = "passwordCheck"
86+ label = "새 비밀번호 확인"
87+ labelClassName = "text-[14px] sm:text-base"
88+ placeholder = "새 비밀번호 입력"
89+ value = { checkNewpassword }
90+ onChange = { ( value ) => setCheckNewPassword ( value ) }
91+ forceInvalid = { ! ! checkNewpassword && checkNewpassword !== newPassword }
92+ invalidMessage = "비밀번호가 일치하지 않습니다."
93+ className = "max-w-[624px]"
94+ />
8995
90- < label className = "mb-2 text-sm sm:text-base text-black mt-3" >
91- 새 비밀번호 확인
92- </ label >
93- < div className = "relative w-full" >
94- < input
95- type = { showCheckNewPassword ? "text" : "password" }
96- value = { checkNewpassword }
97- placeholder = "새 비밀번호 입력"
98- onChange = { ( e ) => setCheckNewPassword ( e . target . value ) }
99- className = { `mt-3 sm:w-[624px] sm:h-[50px] w-[236px] h-[50px] px-[16px] pr-12 rounded-[8px] transition-colors focus:outline-none
100- ${
101- checkNewpassword
102- ? checkNewpassword === newPassword
103- ? "border border-gray-300"
104- : "border border-[var(--color-red)]"
105- : "border border-gray-300 focus:border-purple-500"
106- } `}
107- />
108- < button
109- type = "button"
110- onClick = { toggleCheckNewPasswordVisibility }
111- className = "absolute right-4 top-6 flex size-6 items-center justify-center"
112- >
113- < Image
114- src = {
115- showCheckNewPassword
116- ? "/svgs/eye-on.svg"
117- : "/svgs/eye-off.svg"
118- }
119- alt = "비밀번호 표시 토글"
120- width = { 20 }
121- height = { 20 }
122- className = " w-5 h-5"
123- />
124- </ button >
96+ < button
97+ className = { `w-full h-[54px]
98+ rounded-[8px] text-lg font-medium text-white
99+ ${
100+ isDisabled
101+ ? "bg-gray-300 cursor-not-allowed"
102+ : "bg-[var(--primary)] text-white cursor-pointer"
103+ } `}
104+ onClick = { ( ) => handleChangePassword ( ) }
105+ disabled = { isDisabled || isSubmitting }
106+ >
107+ 변경
108+ </ button >
125109
126- { checkNewpassword && checkNewpassword !== newPassword && (
127- < p className = "mt-2 font-16m block text-[var(--color-red)]" >
128- 비밀번호가 일치하지 않습니다.
129- </ p >
130- ) }
131- </ div >
132- </ div >
110+ { checkNewpassword && checkNewpassword !== newPassword && (
111+ < p className = "font-14r block text-[var(--color-red)]" >
112+ 비밀번호가 일치하지 않습니다.
113+ </ p >
114+ ) }
133115 </ div >
134-
135- { errorMessage && (
136- < p className = "text-red-500 text-sm mt-4" > { errorMessage } </ p >
137- ) }
138- { successMessage && (
139- < p className = "text-green-600 text-sm mt-4" > { successMessage } </ p >
140- ) }
141- < MypageModal
142- isOpen = { showSuccessModal }
143- onClose = { ( ) => setShowSuccessModal ( false ) }
144- message = "비밀번호 변경에 성공하였습니다."
145- />
146-
147- < MypageModal
148- isOpen = { showErrorModal }
149- onClose = { ( ) => setShowErrorModal ( false ) }
150- message = "비밀번호 변경에 실패하였습니다."
151- />
152- < button
153- className = { `mt-2.5 cursor-pointer w-full sm:w-[624px] h-[54px]
154- rounded-[8px] text-lg font-medium
155- ${ isDisabled ? "bg-gray-300 cursor-not-allowed" : "bg-[#5A3FFF] text-white" } ` }
156- onClick = { ( ) => handleChangePassword ( ) }
157- disabled = { isDisabled || isSubmitting }
158- >
159- 변경
160- </ button >
161116 </ div >
162117 ) ;
163118}
0 commit comments