Skip to content

Commit

Permalink
Update userActions.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 12, 2024
1 parent 5301e50 commit da7cc93
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion blockchain_integration/pi_network/PiRide/actions/userActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,45 @@ import { Web3Provider } from '../providers/Web3Provider';
import { NotificationContext } from '../contexts/NotificationContext';

export const UPDATE_USER_PROFILE = 'UPDATE_USER_PROFILE';
export const UPDATE_USER_PROFILE_SUCCESS = 'UPDATE
export const UPDATE_USER_PROFILE_SUCCESS = 'UPDATE_USER_PROFILE_SUCCESS';
export const UPDATE_USER_PROFILE_FAILURE = 'UPDATE_USER_PROFILE_FAILURE';

export const GET_USER_PROFILE = 'GET_USER_PROFILE';
export const GET_USER_PROFILE_SUCCESS = 'GET_USER_PROFILE_SUCCESS';
export const GET_USER_PROFILE_FAILURE = 'GET_USER_PROFILE_FAILURE';

const updateUserProfile = createAction(UPDATE_USER_PROFILE, (name, email, phone, address) => ({
name,
email,
phone,
address,
}));

const getUserProfile = createAction(GET_USER_PROFILE);

export const updateUserProfileAsync = (name, email, phone, address) => async (dispatch) => {
dispatch(updateUserProfile({ name, email, phone, address }));
try {
const web3Provider = new Web3Provider();
const userContract = new UserContract(web3Provider);
const txHash = await userContract.updateUserProfile(name, email, phone, address);
dispatch({ type: UPDATE_USER_PROFILE_SUCCESS, txHash });
NotificationContext.notify(`User profile updated successfully! Tx Hash: ${txHash}`);
} catch (error) {
dispatch({ type: UPDATE_USER_PROFILE_FAILURE, error });
NotificationContext.notify(`Error updating user profile: ${error.message}`);
}
};

export const getUserProfileAsync = () => async (dispatch) => {
dispatch(getUserProfile());
try {
const web3Provider = new Web3Provider();
const userContract = new UserContract(web3Provider);
const userProfile = await userContract.getUserProfile();
dispatch({ type: GET_USER_PROFILE_SUCCESS, userProfile });
} catch (error) {
dispatch({ type: GET_USER_PROFILE_FAILURE, error });
NotificationContext.notify(`Error getting user profile: ${error.message}`);
}
};

0 comments on commit da7cc93

Please sign in to comment.