Front End Setup
1. Set up user action & action type to solidify the sequence of actions
export const REGISTER = 'API:REGISTER';
// async function that forwards the registration parameters to the
// apiService to complete user registration, waits for the registration
// to complete, then dispatches the new state to Redux store
async function registerWithDispatchAsync(
dispatchType: any,
dispatch: Dispatch<Action>,
firstName: string,
lastName: string,
username: string,
email: string,
password: string
): Promise<void> {
var user = await apiService.register(
firstName,
lastName,
username,
email,
password
);
localStorage.setItem('user', JSON.stringify(user));
dispatch({ type: dispatchType, data: user });
}
// we will need to reference the function type in
// numerous react component Prop interfaces.
export type register_fntype = (
firstName: string,
lastName: string,
username: string,
email: string,
password: string
) => Promise<void>;
// returns a dispatch function that returns a promise that
// represents the asynchronous completion of the dispatch work
export function register(
firstName: string,
lastName: string,
username: string,
email: string,
password: string
): (dispatch: Dispatch<UserAction>) => Promise<void> {
return dispatch => {
return registerWithDispatchAsync(
REGISTER,
dispatch,
firstName,
lastName,
username,
email,
password
);
};
}2. Set up User and UserAction types file
3. Set up apiService function to handle request / response to server
4. Set up Redux reducer to receive the returned response and save to Redux store
Last updated