Skip to content

Instantly share code, notes, and snippets.

@JonJam
Last active October 27, 2017 06:56
Show Gist options
  • Save JonJam/914f2567675c00960dafdb5fc2ea2524 to your computer and use it in GitHub Desktop.
Save JonJam/914f2567675c00960dafdb5fc2ea2524 to your computer and use it in GitHub Desktop.
react-redux-ts: Actions
import { Dispatch } from "redux";
import {
signIn as signInToApi,
signOut as signOutFromApi
} from "../../api/authenticationApi";
import IStoreState from "../../store/IStoreState";
import keys from "../ActionTypeKeys";
import {
ISignInFailAction,
ISignInInProgressAction,
ISignInSuccessAction
} from "./signin";
import {
ISignOutFailAction,
ISignOutInProgressAction,
ISignOutSuccessAction
} from "./signout";
export function signIn(): (dispatch: Dispatch<IStoreState>) => Promise<void> {
return async (dispatch: Dispatch<IStoreState>) => {
// Signal work in progress.
dispatch(signInInProgress());
try {
await signInToApi();
dispatch(signInSuccess());
} catch (err) {
dispatch(signInFail(err));
}
};
}
export function signOut(): (dispatch: Dispatch<IStoreState>) => Promise<void> {
return async (dispatch: Dispatch<IStoreState>) => {
// Signal work in progress.
dispatch(signOutInProgress());
try {
await signOutFromApi();
dispatch(signOutSuccess());
} catch (err) {
dispatch(signOutFail(err));
}
};
}
function signInInProgress(): ISignInInProgressAction {
return {
type: keys.SIGNIN_INPROGRESS
};
}
function signInSuccess(): ISignInSuccessAction {
return {
type: keys.SIGNIN_SUCCESS
};
}
function signInFail(error: Error): ISignInFailAction {
return {
payload: {
error
},
type: keys.SIGNIN_FAIL
};
}
function signOutInProgress(): ISignOutInProgressAction {
return {
type: keys.SIGNOUT_INPROGRESS
};
}
function signOutSuccess(): ISignOutSuccessAction {
return {
type: keys.SIGNOUT_SUCCESS
};
}
function signOutFail(error: Error): ISignOutFailAction {
return {
payload: {
error
},
type: keys.SIGNOUT_FAIL
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment