update change pincode
This commit is contained in:
@ -2,6 +2,7 @@ interface apiConfigProps {
|
|||||||
service_dashboard: string;
|
service_dashboard: string;
|
||||||
service_customer: string;
|
service_customer: string;
|
||||||
service_master_data: string;
|
service_master_data: string;
|
||||||
|
service_master_data2: string;
|
||||||
service_transaction: string;
|
service_transaction: string;
|
||||||
service_wallet: string;
|
service_wallet: string;
|
||||||
transaction: string;
|
transaction: string;
|
||||||
@ -18,7 +19,7 @@ const apiConfig: apiConfigProps = {
|
|||||||
// service_dashboard: `${API_URL}${import.meta.env.VITE_ENV != 'development' ? '/d' : ''}`,
|
// service_dashboard: `${API_URL}${import.meta.env.VITE_ENV != 'development' ? '/d' : ''}`,
|
||||||
service_dashboard: `${API_URL}/d`,
|
service_dashboard: `${API_URL}/d`,
|
||||||
service_customer: `${API_URL}/c`,
|
service_customer: `${API_URL}/c`,
|
||||||
// service_master_data: `${API_URL}/m`
|
service_master_data2: `${API_URL}/m`,
|
||||||
service_master_data: `${API_URL}/t`,
|
service_master_data: `${API_URL}/t`,
|
||||||
service_transaction: `${API_URL}/tt`,
|
service_transaction: `${API_URL}/tt`,
|
||||||
service_wallet: `${API_URL}/w`,
|
service_wallet: `${API_URL}/w`,
|
||||||
|
|||||||
@ -1,34 +1,45 @@
|
|||||||
import { useState, useContext, useCallback, MouseEvent } from 'react';
|
import { useState, useContext, useCallback, MouseEvent } from 'react';
|
||||||
import { AccountUserProfileContext } from '../hooks';
|
import { AccountUserProfileContext } from '../hooks';
|
||||||
import { toast } from 'sonner';
|
import { toast } from 'sonner';
|
||||||
import { useAuthContext } from '@/auth';
|
import { getAuth,useAuthContext } from '@/auth';
|
||||||
import { KeenIcon } from '@/components';
|
import { KeenIcon } from '@/components';
|
||||||
import clsx from 'clsx';
|
|
||||||
|
|
||||||
type PasswordType = 'password';
|
type PasswordType = 'current' | 'new' | 'retype';
|
||||||
|
|
||||||
const PinCode = () => {
|
const PinCode = () => {
|
||||||
const { setPincode } = useContext(AccountUserProfileContext);
|
const { setPincode } = useContext(AccountUserProfileContext);
|
||||||
const { getUser } = useAuthContext();
|
|
||||||
|
|
||||||
|
const [currentPincode, setCurrentPincode] = useState('');
|
||||||
const [newPincode, setNewPincode] = useState('');
|
const [newPincode, setNewPincode] = useState('');
|
||||||
|
const [retypePincode, setRetypePincode] = useState('');
|
||||||
|
const [errorRetype, setErrorRetype] = useState('');
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||||
const [showPassword, setShowPassword] = useState({
|
const [showPassword, setShowPassword] = useState({
|
||||||
password: false,
|
current: false,
|
||||||
|
new: false,
|
||||||
|
retype: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleResetPassword = useCallback(async () => {
|
const handleResetPassword = useCallback(async () => {
|
||||||
const user: any = await getUser();
|
if (newPincode !== retypePincode) {
|
||||||
|
toast.error('New Pin Code and Retype Pin Code do not match.');
|
||||||
|
setErrorRetype('New Pin Code and Retype Pin Code do not match.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
setIsSubmitting(true);
|
setIsSubmitting(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await setPincode({
|
await setPincode({
|
||||||
|
currentpincode: currentPincode,
|
||||||
newpincode: newPincode,
|
newpincode: newPincode,
|
||||||
|
retypepincode: retypePincode,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setCurrentPincode('');
|
||||||
setNewPincode('');
|
setNewPincode('');
|
||||||
toast.success('Pin code updated successfully');
|
setRetypePincode('');
|
||||||
|
setErrorRetype(''); // Clear error after success
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
const errorMessage =
|
const errorMessage =
|
||||||
error?.response?.data?.message ||
|
error?.response?.data?.message ||
|
||||||
@ -38,14 +49,26 @@ const PinCode = () => {
|
|||||||
} finally {
|
} finally {
|
||||||
setIsSubmitting(false);
|
setIsSubmitting(false);
|
||||||
}
|
}
|
||||||
}, [newPincode, setPincode, getUser]);
|
}, [currentPincode, newPincode, retypePincode, setPincode]);
|
||||||
|
|
||||||
const isButtonDisabled = isSubmitting || !newPincode;
|
const isButtonDisabled = isSubmitting || !currentPincode || !newPincode || !retypePincode;
|
||||||
|
|
||||||
const togglePassword = useCallback((event: MouseEvent<HTMLButtonElement>, key: PasswordType) => {
|
const togglePassword = useCallback(
|
||||||
|
(event: MouseEvent<HTMLButtonElement>, key: PasswordType) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
setShowPassword((prev) => ({ ...prev, [key]: !prev[key] }));
|
setShowPassword((prev) => ({ ...prev, [key]: !prev[key] }));
|
||||||
}, []);
|
},
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleRetypeChange = (value: string) => {
|
||||||
|
setRetypePincode(value);
|
||||||
|
if (newPincode && value && newPincode !== value) {
|
||||||
|
setErrorRetype('New Pin Code and Retype Pin Code do not match.');
|
||||||
|
} else {
|
||||||
|
setErrorRetype('');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="card pb-2.5">
|
<div className="card pb-2.5">
|
||||||
@ -53,30 +76,97 @@ const PinCode = () => {
|
|||||||
<h3 className="card-title">Pin Code</h3>
|
<h3 className="card-title">Pin Code</h3>
|
||||||
</div>
|
</div>
|
||||||
<div className="card-body grid gap-5">
|
<div className="card-body grid gap-5">
|
||||||
|
{/* Current Pin Code */}
|
||||||
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||||
<label className="form-label max-w-56">New Pin Code</label>
|
<label className="form-label max-w-56">Current Pin Code</label>
|
||||||
<div className="input relative">
|
<div className="input relative">
|
||||||
<input
|
<input
|
||||||
className="form-control pr-10" // untuk kasih jarak buat tombol mata
|
className="form-control pr-10"
|
||||||
placeholder="New Pin Code"
|
placeholder="Current Pin Code"
|
||||||
value={newPincode}
|
value={currentPincode}
|
||||||
onChange={(e) => setNewPincode(e.target.value)} // ✅ fix
|
onChange={(e) => setCurrentPincode(e.target.value)}
|
||||||
disabled={isSubmitting}
|
disabled={isSubmitting}
|
||||||
type={showPassword.password ? 'text' : 'password'}
|
type={showPassword.current ? 'text' : 'password'}
|
||||||
/>
|
/>
|
||||||
<button
|
<button
|
||||||
className="btn btn-icon absolute right-2 top-1/2 transform -translate-y-1/2"
|
className="btn btn-icon absolute right-2 top-1/2 transform -translate-y-1/2"
|
||||||
onClick={(e) => togglePassword(e, 'password')}
|
onClick={(e) => togglePassword(e, 'current')}
|
||||||
type="button"
|
type="button"
|
||||||
>
|
>
|
||||||
<KeenIcon
|
<KeenIcon
|
||||||
icon={showPassword.password ? 'eye-slash' : 'eye'}
|
icon={showPassword.current ? 'eye-slash' : 'eye'}
|
||||||
className="text-gray-500"
|
className="text-gray-500"
|
||||||
/>
|
/>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* New Pin Code */}
|
||||||
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||||
|
<label className="form-label max-w-56">New Pin Code</label>
|
||||||
|
<div className="input relative">
|
||||||
|
<input
|
||||||
|
className="form-control pr-10"
|
||||||
|
placeholder="New Pin Code"
|
||||||
|
value={newPincode}
|
||||||
|
onChange={(e) => {
|
||||||
|
setNewPincode(e.target.value);
|
||||||
|
// Kalau retype sudah diisi, cek lagi error
|
||||||
|
if (retypePincode) {
|
||||||
|
if (e.target.value !== retypePincode) {
|
||||||
|
setErrorRetype('New Pin Code and Retype Pin Code do not match.');
|
||||||
|
} else {
|
||||||
|
setErrorRetype('');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
disabled={isSubmitting}
|
||||||
|
type={showPassword.new ? 'text' : 'password'}
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
className="btn btn-icon absolute right-2 top-1/2 transform -translate-y-1/2"
|
||||||
|
onClick={(e) => togglePassword(e, 'new')}
|
||||||
|
type="button"
|
||||||
|
>
|
||||||
|
<KeenIcon
|
||||||
|
icon={showPassword.new ? 'eye-slash' : 'eye'}
|
||||||
|
className="text-gray-500"
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Retype Pin Code */}
|
||||||
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||||
|
<label className="form-label max-w-56">Retype Pin Code</label>
|
||||||
|
<div className="input relative w-full">
|
||||||
|
<input
|
||||||
|
className={`form-control pr-10 ${errorRetype ? 'border-red-500' : ''}`}
|
||||||
|
placeholder="Retype Pin Code"
|
||||||
|
value={retypePincode}
|
||||||
|
onChange={(e) => handleRetypeChange(e.target.value)}
|
||||||
|
disabled={isSubmitting}
|
||||||
|
type={showPassword.retype ? 'text' : 'password'}
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
className="btn btn-icon absolute right-2 top-1/2 transform -translate-y-1/2"
|
||||||
|
onClick={(e) => togglePassword(e, 'retype')}
|
||||||
|
type="button"
|
||||||
|
>
|
||||||
|
<KeenIcon
|
||||||
|
icon={showPassword.retype ? 'eye-slash' : 'eye'}
|
||||||
|
className="text-gray-500"
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Error message under Retype */}
|
||||||
|
{errorRetype && (
|
||||||
|
<div className="text-red-500 text-sm -mt-4 ml-[calc(14rem+10px)]">{errorRetype}</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Submit button */}
|
||||||
<div className="flex justify-end">
|
<div className="flex justify-end">
|
||||||
<button
|
<button
|
||||||
className="btn btn-primary"
|
className="btn btn-primary"
|
||||||
|
|||||||
@ -2,8 +2,7 @@ import React, { createContext, useState } from 'react';
|
|||||||
import { apiConfig } from '@/config/api.config';
|
import { apiConfig } from '@/config/api.config';
|
||||||
import { useCallApi } from '@/hooks';
|
import { useCallApi } from '@/hooks';
|
||||||
import { toast } from 'sonner';
|
import { toast } from 'sonner';
|
||||||
import axios from 'axios';
|
import { getAuth,useAuthContext } from '@/auth';
|
||||||
import { useAuthContext } from '@/auth';
|
|
||||||
|
|
||||||
interface Password {
|
interface Password {
|
||||||
password: string;
|
password: string;
|
||||||
@ -18,15 +17,17 @@ interface Profile {
|
|||||||
username: string;
|
username: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PinCode {
|
interface PinCodePayload {
|
||||||
|
currentpincode: string;
|
||||||
newpincode: string;
|
newpincode: string;
|
||||||
|
retypepincode: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ContextProps {
|
interface ContextProps {
|
||||||
password: Password | null;
|
password: Password | null;
|
||||||
profile: Profile | null;
|
profile: Profile | null;
|
||||||
pincode: PinCode | null;
|
pincode: PinCodePayload | null;
|
||||||
setPincode: (pincode: PinCode) => Promise<void>;
|
setPincode: (pincode: PinCodePayload) => Promise<void>;
|
||||||
setPassword: (password: Password) => Promise<void>;
|
setPassword: (password: Password) => Promise<void>;
|
||||||
setProfile: (profile: Profile) => Promise<void>;
|
setProfile: (profile: Profile) => Promise<void>;
|
||||||
}
|
}
|
||||||
@ -37,42 +38,36 @@ const initialProps: ContextProps = {
|
|||||||
pincode: null,
|
pincode: null,
|
||||||
setPassword: async () => {},
|
setPassword: async () => {},
|
||||||
setProfile: async () => {},
|
setProfile: async () => {},
|
||||||
setPincode: async () => {}
|
setPincode: async () => {},
|
||||||
};
|
};
|
||||||
|
|
||||||
const AccountUserProfileContext = createContext<ContextProps>(initialProps);
|
const AccountUserProfileContext = createContext<ContextProps>(initialProps);
|
||||||
|
|
||||||
const API_URL = apiConfig.service_dashboard;
|
const API_URL = apiConfig.service_dashboard;
|
||||||
|
const API_URL2 = apiConfig.service_master_data2;
|
||||||
|
|
||||||
const AccountUserProfileContextProvider = ({ children }: { children: React.ReactNode }) => {
|
const AccountUserProfileContextProvider = ({ children }: { children: React.ReactNode }) => {
|
||||||
/* state */
|
|
||||||
const { login } = useAuthContext();
|
const { login } = useAuthContext();
|
||||||
const [profile, setProfileState] = useState<Profile | null>(null);
|
const [profile, setProfileState] = useState<Profile | null>(null);
|
||||||
const [password, setPasswordState] = useState<Password | null>(null);
|
const [password, setPasswordState] = useState<Password | null>(null);
|
||||||
const [pinCode, setPincodeState] = useState<PinCode | null>(null);
|
const [pinCode, setPincodeState] = useState<PinCodePayload | null>(null);
|
||||||
const [alert, setAlert] = useState({
|
const [alert, setAlert] = useState({ show: false, message: '' });
|
||||||
show: false,
|
|
||||||
message: ''
|
|
||||||
});
|
|
||||||
|
|
||||||
const { PutData, PostData } = useCallApi();
|
const { PutData, PostData } = useCallApi();
|
||||||
|
|
||||||
// Helper function to handle API errors
|
|
||||||
const handleError = (error: any, defaultMessage: string) => {
|
const handleError = (error: any, defaultMessage: string) => {
|
||||||
const errorMessage = error?.response?.data?.message || error?.message || defaultMessage;
|
const errorMessage = error?.response?.data?.message || error?.message || defaultMessage;
|
||||||
setAlert({ show: true, message: errorMessage });
|
setAlert({ show: true, message: errorMessage });
|
||||||
toast.error(errorMessage);
|
toast.error(errorMessage);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function to handle password update
|
|
||||||
const handleSetPassword = async (newPassword: Password) => {
|
const handleSetPassword = async (newPassword: Password) => {
|
||||||
setPasswordState(newPassword);
|
setPasswordState(newPassword);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Validate the current password
|
|
||||||
const validate = await PostData(`${API_URL}/login`, {
|
const validate = await PostData(`${API_URL}/login`, {
|
||||||
password: newPassword.current_password,
|
password: newPassword.current_password,
|
||||||
username: newPassword.username
|
username: newPassword.username,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!validate || !validate.status) {
|
if (!validate || !validate.status) {
|
||||||
@ -80,10 +75,9 @@ const AccountUserProfileContextProvider = ({ children }: { children: React.React
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the password
|
|
||||||
const response = await PutData(`${API_URL}/user/update_password`, {
|
const response = await PutData(`${API_URL}/user/update_password`, {
|
||||||
password: newPassword.password,
|
password: newPassword.password,
|
||||||
retype_password: newPassword.retype_password
|
retype_password: newPassword.retype_password,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response && response.status) {
|
if (response && response.status) {
|
||||||
@ -96,30 +90,37 @@ const AccountUserProfileContextProvider = ({ children }: { children: React.React
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function to handle profile update
|
|
||||||
const handleSetProfile = async (newProfile: Profile) => {
|
const handleSetProfile = async (newProfile: Profile) => {
|
||||||
try {
|
try {
|
||||||
setProfileState(newProfile);
|
setProfileState(newProfile);
|
||||||
|
|
||||||
const response = await PutData(`${API_URL}/user/update_profile/`, {
|
await PutData(`${API_URL}/user/update_profile/`, {
|
||||||
name: newProfile.name,
|
name: newProfile.name,
|
||||||
email: newProfile.email,
|
email: newProfile.email,
|
||||||
username: newProfile.username
|
username: newProfile.username,
|
||||||
});
|
});
|
||||||
|
|
||||||
toast.success('Profile updated successfully.');
|
toast.success('Profile updated successfully.');
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
handleError(error, 'Failed to update Profile. Please try again.');
|
handleError(error, 'Failed to update profile. Please try again.');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function to handle pin code update
|
const handleSetPinCode = async (newPinCode: PinCodePayload) => {
|
||||||
const handleSetPinCode = async (newPinCode: PinCode) => {
|
|
||||||
try {
|
try {
|
||||||
setPincodeState(newPinCode);
|
setPincodeState(newPinCode);
|
||||||
|
const customerid = getAuth()?.user?.customer?.id;
|
||||||
|
const response = await PutData(`${API_URL2}/customer/pin/${customerid}`, {
|
||||||
|
old_pin: newPinCode.currentpincode,
|
||||||
|
new_pin: newPinCode.newpincode,
|
||||||
|
retype_new_pin: newPinCode.retypepincode,
|
||||||
|
});
|
||||||
|
|
||||||
// You can make an API call here to update the pin code in the backend if needed
|
if (response && response.status) {
|
||||||
toast.success('Pin code updated successfully.');
|
toast.success('Pin code updated successfully.');
|
||||||
|
} else {
|
||||||
|
toast.error(response?.message || 'Failed to update pin code.');
|
||||||
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
handleError(error, 'Failed to update Pin Code. Please try again.');
|
handleError(error, 'Failed to update Pin Code. Please try again.');
|
||||||
}
|
}
|
||||||
@ -133,7 +134,7 @@ const AccountUserProfileContextProvider = ({ children }: { children: React.React
|
|||||||
pincode: pinCode,
|
pincode: pinCode,
|
||||||
setPassword: handleSetPassword,
|
setPassword: handleSetPassword,
|
||||||
setProfile: handleSetProfile,
|
setProfile: handleSetProfile,
|
||||||
setPincode: handleSetPinCode
|
setPincode: handleSetPinCode,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
Reference in New Issue
Block a user