Created
April 3, 2018 13:24
-
-
Save kuzvac/2955ddeb9228d9c1e539c1464e4c452d to your computer and use it in GitHub Desktop.
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 { 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 | |
} | |
>() | |
}; |
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 { 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 | |
} | |
}; |
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 { 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