update transaction detail

This commit is contained in:
wayanrivan
2025-04-28 15:51:05 +07:00
parent 99687023a3
commit 1f1679c185
4 changed files with 166 additions and 162 deletions

View File

@ -18,9 +18,15 @@ interface Profile {
username: string;
}
interface PinCode {
newpincode: string;
}
interface ContextProps {
password: Password | null;
profile: Profile | null;
pincode: PinCode | null;
setPincode: (pincode: PinCode) => Promise<void>;
setPassword: (password: Password) => Promise<void>;
setProfile: (profile: Profile) => Promise<void>;
}
@ -28,8 +34,10 @@ interface ContextProps {
const initialProps: ContextProps = {
profile: null,
password: null,
pincode: null,
setPassword: async () => {},
setProfile: async () => {}
setProfile: async () => {},
setPincode: async () => {}
};
const AccountUserProfileContext = createContext<ContextProps>(initialProps);
@ -39,25 +47,29 @@ const API_URL = apiConfig.service_dashboard;
const AccountUserProfileContextProvider = ({ children }: { children: React.ReactNode }) => {
/* state */
const { login } = useAuthContext();
const [profile, setProfile] = useState<Profile | null>(null);
const [password, setPassword] = useState<Password | null>(null);
const [profile, setProfileState] = useState<Profile | null>(null);
const [password, setPasswordState] = useState<Password | null>(null);
const [pinCode, setPincodeState] = useState<PinCode | null>(null);
const [alert, setAlert] = useState({
show: false,
message: ''
});
const { PutData } = useCallApi();
const { PostData } = useCallApi();
const handleSetPassword = async (newPassword: Password) => {
setPassword(newPassword);
const { PutData, PostData } = useCallApi();
const handleError = (error: any, defaultMessage: string) => {
const errorMessage = error?.response?.data?.message || error?.message || defaultMessage;
setAlert({ show: true, message: errorMessage });
toast.error(errorMessage);
};
// Helper function to handle API errors
const handleError = (error: any, defaultMessage: string) => {
const errorMessage = error?.response?.data?.message || error?.message || defaultMessage;
setAlert({ show: true, message: errorMessage });
toast.error(errorMessage);
};
// Function to handle password update
const handleSetPassword = async (newPassword: Password) => {
setPasswordState(newPassword);
try {
// Validate the current password
const validate = await PostData(`${API_URL}/login`, {
password: newPassword.current_password,
username: newPassword.username
@ -68,6 +80,7 @@ const AccountUserProfileContextProvider = ({ children }: { children: React.React
return;
}
// Update the password
const response = await PutData(`${API_URL}/user/update_password`, {
password: newPassword.password,
retype_password: newPassword.retype_password
@ -83,9 +96,10 @@ const AccountUserProfileContextProvider = ({ children }: { children: React.React
}
};
// Function to handle profile update
const handleSetProfile = async (newProfile: Profile) => {
try {
setProfile(newProfile);
setProfileState(newProfile);
const response = await PutData(`${API_URL}/user/update_profile/`, {
name: newProfile.name,
@ -95,9 +109,19 @@ const AccountUserProfileContextProvider = ({ children }: { children: React.React
toast.success('Profile updated successfully.');
} catch (error: any) {
const errorMessage = error?.message || 'Failed to update Profile. Please try again.';
setAlert({ show: true, message: errorMessage });
toast.error(errorMessage);
handleError(error, 'Failed to update Profile. Please try again.');
}
};
// Function to handle pin code update
const handleSetPinCode = async (newPinCode: PinCode) => {
try {
setPincodeState(newPinCode);
// You can make an API call here to update the pin code in the backend if needed
toast.success('Pin code updated successfully.');
} catch (error: any) {
handleError(error, 'Failed to update Pin Code. Please try again.');
}
};
@ -106,8 +130,10 @@ const AccountUserProfileContextProvider = ({ children }: { children: React.React
value={{
password,
profile,
pincode: pinCode,
setPassword: handleSetPassword,
setProfile: handleSetProfile
setProfile: handleSetProfile,
setPincode: handleSetPinCode
}}
>
{children}