From 1f1679c185f278c0da840f133f22931f284d0cc7 Mon Sep 17 00:00:00 2001 From: wayanrivan Date: Mon, 28 Apr 2025 15:51:05 +0700 Subject: [PATCH] update transaction detail --- .../home/user-profile/blocks/PinCode.tsx | 158 +++--------------- .../hooks/AccountUserProfileContext.tsx | 60 +++++-- .../blocks/DetailApprovalTransaction.tsx | 52 +++++- .../blocks/DetailTransaction.tsx | 58 ++++++- 4 files changed, 166 insertions(+), 162 deletions(-) diff --git a/src/pages/account/home/user-profile/blocks/PinCode.tsx b/src/pages/account/home/user-profile/blocks/PinCode.tsx index da647ca..ff0ba0d 100644 --- a/src/pages/account/home/user-profile/blocks/PinCode.tsx +++ b/src/pages/account/home/user-profile/blocks/PinCode.tsx @@ -1,95 +1,50 @@ -import { useState, useContext, useEffect, useCallback, MouseEvent } from 'react'; +import { useState, useContext, useCallback, MouseEvent } from 'react'; import { AccountUserProfileContext } from '../hooks'; import { toast } from 'sonner'; import { useAuthContext } from '@/auth'; import { KeenIcon } from '@/components'; import clsx from 'clsx'; -type PasswordType = 'password' | 'retype_password' | 'current_password'; +type PasswordType = 'password'; + const PinCode = () => { - const { setPassword } = useContext(AccountUserProfileContext); + const { setPincode } = useContext(AccountUserProfileContext); const { getUser } = useAuthContext(); - const [newPassword, setNewPassword] = useState(''); - const [currentPassword, setCurrentPassword] = useState(''); - const [confirmPassword, setConfirmPassword] = useState(''); + const [newPincode, setNewPincode] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); - const [messagePassword, setMessagePassword] = useState(true); const [showPassword, setShowPassword] = useState({ - current_password: false, password: false, - retype_password: false }); - const [passwordErrors, setPasswordErrors] = useState([]); - - const validatePassword = (password: string) => { - const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password); - const hasCapitalLetter = /[A-Z]/.test(password); - const hasNumber = /[0-9]/.test(password); - return hasSpecialChar && hasCapitalLetter && hasNumber; - }; - - useEffect(() => { - const passwordsMatch = newPassword === confirmPassword; - const isPasswordValid = validatePassword(newPassword); - setMessagePassword(passwordsMatch && isPasswordValid); - - const errors: string[] = []; - if (newPassword) { - if (!/[A-Z]/.test(newPassword)) { - errors.push('Password must contain at least one capital letter'); - } - if (!/[0-9]/.test(newPassword)) { - errors.push('Password must contain at least one number'); - } - if (!/[!@#$%^&*(),.?":{}|<>]/.test(newPassword)) { - errors.push('Password must contain at least one special character'); - } - if (newPassword !== confirmPassword) { - errors.push('Passwords do not match'); - } - } - setPasswordErrors(errors); - }, [newPassword, confirmPassword]); - const handleResetPassword = useCallback(async () => { const user: any = await getUser(); - if (!messagePassword) { - toast.error('Passwords do not match!'); - return; - } - setIsSubmitting(true); try { - await setPassword({ - current_password: currentPassword, - password: newPassword, - retype_password: confirmPassword, - username: user.data.username ?? '' + await setPincode({ + newpincode: newPincode, }); - setNewPassword(''); - setConfirmPassword(''); - setCurrentPassword(''); + setNewPincode(''); + toast.success('Pin code updated successfully'); } catch (error: any) { const errorMessage = error?.response?.data?.message || error?.message || - 'An error occurred while resetting the password.'; + 'An error occurred while updating the pin code.'; toast.error(errorMessage); } finally { setIsSubmitting(false); } - }, [currentPassword, newPassword, newPassword, messagePassword, getUser]); + }, [newPincode, setPincode, getUser]); - const isButtonDisabled = isSubmitting || !newPassword || !confirmPassword || !messagePassword; + const isButtonDisabled = isSubmitting || !newPincode; - const togglePassword = useCallback((event: MouseEvent, key: string) => { + const togglePassword = useCallback((event: MouseEvent, key: PasswordType) => { event.preventDefault(); - setShowPassword((prev) => ({ ...prev, [key]: !prev[key as PasswordType] })); + setShowPassword((prev) => ({ ...prev, [key]: !prev[key] })); }, []); return ( @@ -99,89 +54,28 @@ const PinCode = () => {
- -
+ +
setCurrentPassword(e.target.value)} - disabled={isSubmitting} - /> - -
-
-
- -
- setNewPassword(e.target.value)} + value={newPincode} + onChange={(e) => setNewPincode(e.target.value)} // ✅ fix disabled={isSubmitting} type={showPassword.password ? 'text' : 'password'} /> -
-
-
- -
- setConfirmPassword(e.target.value)} - disabled={isSubmitting} - /> - -
-
-
- - {passwordErrors.length > 0 && ( -
- {passwordErrors.map((error, index) => ( -

{error}

- ))} -
- )} -
-