Skip to content

Instantly share code, notes, and snippets.

@kuzvac
Created April 3, 2018 13:24
Show Gist options
  • Save kuzvac/2955ddeb9228d9c1e539c1464e4c452d to your computer and use it in GitHub Desktop.
Save kuzvac/2955ddeb9228d9c1e539c1464e4c452d to your computer and use it in GitHub Desktop.
import { buildAction } from 'typesafe-actions';
export const authActions = {
getLoggedUser: buildAction('GET_LOGGED_USER').empty(),
fetchAuth: buildAction('AUTH').async<
{
pending: boolean
email: string,
password: string
},
{
pending: boolean,
logged: boolean,
token: string,
refreshToken: string,
email: string,
username: string,
id: string,
},
{
show: boolean,
type: string,
message: string
}
>()
};
import { getType } from 'typesafe-actions';
import { RootAction } from './root-action';
import { authActions } from './actions';
export type AuthState = Readonly<{
pending: boolean,
logged: boolean,
token: string,
refreshToken: string,
email: string,
username: string,
id: string,
alert: {
show: boolean,
type: string,
message: string
}
}>
const initialState: AuthState = {
pending: true,
logged: false,
token: '',
refreshToken: '',
email: '',
username: '',
id: '',
alert: {
show: false,
type: '',
message: ''
}
};
export const authReducer = (state: AuthState = initialState, action: RootAction) => {
switch (action.type) {
case getType(authActions.getLoggedUser):
return Object.assign({}, state, {
pending: false
});
case getType(authActions.fetchAuth.request):
return Object.assign({}, state, {
pending: action.payload.pending
});
default:
return state
}
};
import { ActionsUnion } from 'typesafe-actions';
import { authActions } from './actions'
const actions = {
...authActions
};
export type RootAction = ActionsUnion<typeof actions>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment