Last active
October 27, 2017 06:56
-
-
Save JonJam/914f2567675c00960dafdb5fc2ea2524 to your computer and use it in GitHub Desktop.
react-redux-ts: Actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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