Guest User

Untitled

a guest
Dec 1st, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. import { Action, Reducer } from 'redux'
  2. import fire from '../services/firebasecred'
  3.  
  4. import { AppThunkAction } from '../store'
  5.  
  6. export interface LoginState {
  7. isLoggedIn: boolean;
  8. processingLogin: boolean;
  9. }
  10.  
  11. export const initialState: LoginState = {
  12. isLoggedIn: false,
  13. processingLogin: false
  14. }
  15.  
  16. export enum LoginActionTypes {
  17. Login = 'LOGIN',
  18. LoggedIn = 'LOGGED_IN',
  19. }
  20.  
  21. export interface LoginAction extends Action { payload: {}; type: LoginActionTypes.Login; }
  22. export interface LoggedinAction extends Action { payload: { loginname: String }; type: LoginActionTypes.LoggedIn; }
  23.  
  24. export type KnownAction = LoginAction | LoggedinAction;
  25.  
  26.  
  27. export interface LoginReducer<T> extends Reducer<T> {
  28. (State: LoginState, Action: LoginAction): LoginState
  29. }
  30.  
  31. export const actionCreators = {
  32. login: (username: string, password: string): AppThunkAction<KnownAction> => (dispatch, getState) => {
  33. // login code but not executed for some reason
  34. }
  35. }
  36.  
  37. const loginReducer: LoginReducer<LoginState> = (state: LoginState = initialState, action: KnownAction):
  38. LoginState => {
  39. switch (action.type) {
  40. case LoginActionTypes.Login:
  41. {
  42. var x = 0;
  43. }
  44. break;
  45. case LoginActionTypes.LoggedIn:
  46. {
  47. var x = 0;
  48. }
  49. break;
  50. }
  51.  
  52. return state;
  53. }
  54.  
  55. export default loginReducer
  56.  
  57. import { connect, Dispatch } from 'react-redux'
  58. import { RootState } from '../store'
  59. import * as Login from '../reducers/login'
  60. import NavMenu, { OwnProps } from '../components/NavMenu'
  61.  
  62. interface StateProps {
  63. loggedIn: boolean
  64. // isLoggingIn: boolean
  65. }
  66.  
  67. interface AntiStateProps {
  68.  
  69. }
  70.  
  71. interface DispatchProps {
  72. login: (username: string, password: string) => void
  73. }
  74.  
  75. const mapStateToProps = (state: RootState): StateProps => {
  76. return ({ loggedIn: state.login.isLoggedIn });
  77. }
  78.  
  79. const mapDispatchToProps = (dispatch: Dispatch<DispatchProps>): DispatchProps => {
  80. return ({
  81. login: Login.actionCreators.login
  82. })
  83. }
  84.  
  85. export default connect<StateProps, DispatchProps, OwnProps | AntiStateProps>(mapStateToProps, mapDispatchToProps)(NavMenu)
  86.  
  87. import * as React from 'react';
  88. import AppBar from 'material-ui/AppBar'
  89. import FlatButton from 'material-ui/FlatButton';
  90.  
  91. import rootStore from '../store';
  92. import { connect } from 'react-redux';
  93. import { RootState } from '../store';
  94. import * as LoginStore from '../reducers/login';
  95.  
  96. export interface OwnProps {
  97. loggedIn: boolean,
  98. login: (username: string, password: string) => void;
  99. }
  100.  
  101. interface OwnState {
  102. // isLoggedIn: boolean
  103. }
  104.  
  105. export default class NavMenu extends React.Component<OwnProps, OwnState>
  106. {
  107. // private onLogin: (username: string, password: string) => void
  108.  
  109. public constructor(props: OwnProps) {
  110. super(props);
  111.  
  112. // this.onLogin = (username: string, password: string) => props.login(username, password);
  113. }
  114. public render() {
  115. return <div>
  116. <AppBar title="LOL" zDepth={0}>
  117. </AppBar>
  118. <FlatButton label="LOGIN" onClick={this.login.bind(this)} />
  119. </div>;
  120. }
  121.  
  122. login(){
  123. this.props.login("username", "password");
  124. }
  125. }
  126.  
  127. import { combineReducers, createStore, applyMiddleware, Reducer } from 'redux'
  128. import calenderReducer, { CalenderState } from './reducers/calender'
  129. import countReducer, { CountState } from './reducers/count'
  130. import createBrowserHistory from 'history/createBrowserHistory'
  131. import loginReducer, { LoginState } from './reducers/login'
  132. import { routerReducer, RouterState, routerMiddleware } from 'react-router-redux'
  133. import thunk from 'redux-thunk';
  134.  
  135. export const history = createBrowserHistory()
  136. const middleware = routerMiddleware(history)
  137.  
  138. export interface RootState {
  139. calender : CalenderState,
  140. count : CountState,
  141. routing : RouterState,
  142. login : LoginState,
  143. }
  144.  
  145. const rootReducer = combineReducers({
  146. calender : calenderReducer,
  147. count : countReducer,
  148. routing : routerReducer as Reducer<RouterState>,
  149. login : loginReducer,
  150. })
  151.  
  152. const rootStore = createStore(
  153. rootReducer,
  154. applyMiddleware(thunk, middleware),
  155. )
  156.  
  157. // This type can be used as a hint on action creators so that its 'dispatch' and 'getState' params are
  158. // correctly typed to match your store.
  159. export interface AppThunkAction<TAction> {
  160. (dispatch: (action: TAction) => void, getState: () => RootState): void;
  161. }
  162.  
  163.  
  164. export default rootStore
Add Comment
Please, Sign In to add comment