Guest User

Untitled

a guest
Dec 12th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. import * as React from 'react';
  2. import { Input } from 'src/FormUtilities/Input';
  3. import { IUserProps } from '../userEntitities';
  4.  
  5. interface IProps{
  6. user:IUserProps
  7. onChange:(name:string, value:string)=>void
  8. }
  9.  
  10. export const RegistrationForm =(props:IProps) =>{
  11.  
  12. return(
  13. <div>
  14. <Input name={props.user.FirstName.Name}
  15. label= {props.user.FirstName.Name}
  16. placeholder= ''
  17. value=''
  18. onChange={props.onChange}
  19. error= {props.user.FirstName.ErrorMessage} />
  20.  
  21. <Input name={props.user.Email.Name}
  22. label= {props.user.Email.Name}
  23. placeholder= ''
  24. value=''
  25. onChange={props.onChange}
  26. error= {props.user.Email.ErrorMessage} />
  27. </div>
  28. )
  29.  
  30. }
  31.  
  32. import * as actionsEnums from './actionEnums';
  33.  
  34. interface IUpdateField{
  35. type: actionsEnums.FIELD_CHANGED,
  36. value: string,
  37. name:string
  38. }
  39. export const updateField = (name:string, value:string):IUpdateField => ({
  40. 'name' : name,
  41. type: actionsEnums.FIELD_CHANGED,
  42. 'value': value
  43. });
  44.  
  45. export type UserAction = IUpdateField
  46.  
  47. import * as constants from './actionEnums';
  48. import {UserAction} from './actions'
  49. import { IField } from './userEntitities';
  50.  
  51. export interface IUserState {
  52. user:{
  53. Email:IField,
  54. FirstName: IField
  55. }
  56. }
  57. export const setDefaultUserState:() => IUserState = () => ({
  58. user: {
  59. Email: {
  60. ErrorMessage: '',
  61. Label:'Email',
  62. Name:'Email',
  63. Text: ''
  64. },
  65. FirstName: {
  66. ErrorMessage: '',
  67. Label:'Name',
  68. Name:'Name',
  69. Text: ''
  70. },
  71. }}
  72. )
  73.  
  74. export const userRegistrationReducer = (state= setDefaultUserState(), action:UserAction):IUserState =>{
  75. switch (action.type)
  76. {
  77. case constants.FIELD_CHANGED:
  78. return validateField(state, action.name, action.value)
  79.  
  80. default: return state
  81. }
  82. }
  83.  
  84. const validateField = (state: IUserState, name:string, value:string) => {
  85.  
  86. if(name==='Name')
  87. {
  88. if(value===''){
  89. return {
  90.  
  91. ...state, FirstName: { ...state.user.FirstName, ErrorMessage: "Invalid name" } }
  92.  
  93. }
  94. }
  95. if(name==='Email'){
  96. if (!value.includes("@"))
  97. {
  98. return{
  99. ...state,ErrorMessage:'Invalid Email', Label:'Email *'
  100. }
  101.  
  102. }
  103. }
  104. return {
  105. ...state
  106. }
  107.  
  108. };
  109.  
  110. import { connect } from 'react-redux';
  111. import { Dispatch } from 'redux';
  112. import { IState } from 'src/Redux lessons';
  113. import {updateField, UserAction} from './actions'
  114. import {RegistrationForm} from './Components/RegistrationForm'
  115.  
  116.  
  117. const mapStateToProps = (state:IState)=>
  118. ({
  119. user: state.userRegistrationReducer.user
  120. })
  121.  
  122. const mapDispatchToProps = (dispatch: Dispatch<UserAction>)=>{
  123. return {
  124. onChange:(name:string, value:string)=>dispatch(updateField(name, value))
  125. }
  126. }
  127.  
  128. export const RegistrationFormContainer = connect(mapStateToProps, mapDispatchToProps)(RegistrationForm)
  129.  
  130. import { combineReducers} from 'redux';
  131. import {IUserState, userRegistrationReducer} from '../Registration/reducers'
  132. import { IUserColorState, userReducer} from './Lesson4/redux/userColorReducer'
  133.  
  134. export interface IState{
  135. userRegistrationReducer : IUserState,
  136. userReducer:IUserColorState
  137. }
  138.  
  139. export const reducers = combineReducers<IState>({
  140. userReducer,
  141. userRegistrationReducer,
  142. })
Add Comment
Please, Sign In to add comment