Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. export const validate = ({name})=> {
  2. let errors = {}
  3.  
  4. if (!name)
  5. errors.name = <FormattedMessage id={'common.error.empty'} />
  6.  
  7. return errors
  8. }
  9.  
  10. const returnToList = ({history, filter}) => history.push({pathname: '/setup/rooms', state: filter})
  11.  
  12. export class AddOrEditRoom extends Component {
  13. render = () => {
  14. let {intl, submit, onSubmit, handleSubmit, filter, currentRoomValues} = this.props
  15. debugger
  16.  
  17. const actions =
  18. <Fragment>
  19. <Button
  20. variant='contained'
  21. name='cancel'
  22. onClick={() => returnToList(this.props)}
  23. >
  24. <FormattedMessage id='common.cancel' />
  25. </Button>
  26. <Button
  27. name='save'
  28. onClick={() => onSubmit({currentRoomValues, filter})}
  29. /*onClick={submit} - previous implementation, but I need pass props `filter` to onSumbit*/
  30. >
  31. <FormattedMessage id='common.save' />
  32. </Button>
  33. </Fragment>
  34.  
  35. return (
  36. <Page title={<FormattedMessage id='rooms.roomInfo' />} actions={actions} footer={actions}>
  37. {isLoadingInProgress && <LinearProgress variant='indeterminate' />}
  38. <form onSubmit={handleSubmit}>
  39. <Field
  40. required
  41. name='name'
  42. label={<FormattedMessage id='name' />}
  43. component={renderTextField}
  44. />
  45. </form>
  46. </Page>
  47. )
  48. }
  49. }
  50.  
  51. export const mapStateToProps = (state, {match: {params: {roomId}}}) => {
  52. let initialValues = roomId && state.entities.rooms[roomId]
  53. const form = `room-${roomId}`
  54. const selector = formValueSelector(form)
  55.  
  56. return {
  57. roomId,
  58. initialValues,
  59. form,
  60. filter: filterSelector(state.rooms),
  61. currentRoomValues: {...initialValues, name: selector(state, 'name')},
  62. }}
  63.  
  64. export const mapDispatchToProps = (dispatch, {match: {params: {roomId}}, history}) => ({
  65. init: () => dispatch(RoomActions.get(roomId)),
  66. onSubmit: async ({currentRoomValues, filter}) => {
  67. const {success, response} = await dispatch(RoomActions.createOrUpdate({...currentRoomValues, ...getTrimmedStringFields(currentRoomValues)}))
  68.  
  69. if (!success) {
  70. const statusCode = response?.result?.statusCode
  71. const error = {}
  72.  
  73. if (statusCode === ValidationStatus.DuplicateName)
  74. error.name = <FormattedMessage id='rooms.duplicateName' />
  75. else
  76. error._error = <FormattedMessage id='rooms.failedMessageWithStatusCode' values={{statusCode}} />
  77.  
  78. throw new SubmissionError(error)
  79. }
  80.  
  81. returnToList({history, filter})
  82. },
  83. })
  84.  
  85. export default compose(
  86. connect(mapStateToProps, mapDispatchToProps),
  87. reduxForm({validate, enableReinitialize: true, keepDirtyOnReinitialize: true}),
  88. initializable,
  89. )(AddOrEditRoom)
  90.  
  91. export const renderTextField = ({input, label, meta: {touched, error}, ...other}) =>
  92. <TextField
  93. label={label}
  94. error={!!(touched && error)}
  95. helperText={!!(touched && error) && error}
  96. {...input}
  97. {...other}
  98. />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement