@@ -6,18 +6,17 @@ import { connect } from 'react-redux';
6
6
import PropTypes from 'prop-types' ;
7
7
import Button from '@material-ui/core/Button' ;
8
8
import { withTranslation } from 'react-i18next' ;
9
- import TextField from '@material-ui/core/TextField' ;
10
9
import Dialog from '@material-ui/core/Dialog' ;
11
10
import DialogActions from '@material-ui/core/DialogActions' ;
12
11
import DialogContent from '@material-ui/core/DialogContent' ;
13
12
import DialogTitle from '@material-ui/core/DialogTitle' ;
14
13
import { addClassroom } from '../../actions' ;
15
14
import {
16
15
ADD_CLASSROOM_BUTTON_ID ,
17
- ADD_CLASSROOM_NAME_INPUT_ID ,
18
16
ADD_CLASSROOM_VALIDATE_BUTTON_ID ,
19
17
ADD_CLASSROOM_CANCEL_BUTTON_ID ,
20
18
} from '../../config/selectors' ;
19
+ import ClassroomNameTextField from './ClassroomNameTextField' ;
21
20
22
21
const styles = theme => ( {
23
22
fab : {
@@ -41,18 +40,25 @@ class AddClassroomButton extends Component {
41
40
42
41
state = ( ( ) => {
43
42
const { t } = this . props ;
43
+ const defaultName = t ( 'New Classroom' ) ;
44
44
return {
45
45
open : false ,
46
- name : t ( 'New Classroom' ) ,
46
+ name : defaultName ,
47
47
} ;
48
48
} ) ( ) ;
49
49
50
+ isClassroomNameValid = ( ) => {
51
+ const { name } = this . state ;
52
+ // todo: check for special characters
53
+ return name . trim ( ) . length ;
54
+ } ;
55
+
50
56
handleClickOpen = ( ) => {
51
57
this . setState ( { open : true } ) ;
52
58
} ;
53
59
54
60
close = ( ) => {
55
- this . setState ( { name : '' , open : false } ) ;
61
+ this . setState ( { open : false } ) ;
56
62
} ;
57
63
58
64
handleCancel = ( ) => {
@@ -62,11 +68,14 @@ class AddClassroomButton extends Component {
62
68
handleValidate = ( ) => {
63
69
const { name } = this . state ;
64
70
const { userId, dispatchAddClassroom } = this . props ;
65
- dispatchAddClassroom ( { name, userId } ) ;
66
- this . close ( ) ;
71
+ if ( this . isClassroomNameValid ( ) ) {
72
+ const trimmedName = name . trim ( ) ;
73
+ dispatchAddClassroom ( { name : trimmedName , userId } ) ;
74
+ this . close ( ) ;
75
+ }
67
76
} ;
68
77
69
- handleChange = event => {
78
+ handleNameChange = event => {
70
79
const { target } = event ;
71
80
this . setState ( { name : target . value } ) ;
72
81
} ;
@@ -96,15 +105,9 @@ class AddClassroomButton extends Component {
96
105
{ t ( 'Enter a name for your new classroom' ) }
97
106
</ DialogTitle >
98
107
< DialogContent >
99
- < TextField
100
- id = { ADD_CLASSROOM_NAME_INPUT_ID }
101
- autoFocus
102
- margin = "dense"
103
- label = { t ( "Classroom's Name" ) }
104
- type = "text"
105
- fullWidth
106
- value = { name }
107
- onChange = { this . handleChange }
108
+ < ClassroomNameTextField
109
+ name = { name }
110
+ handleChange = { this . handleNameChange }
108
111
/>
109
112
</ DialogContent >
110
113
< DialogActions >
@@ -119,6 +122,7 @@ class AddClassroomButton extends Component {
119
122
onClick = { this . handleValidate }
120
123
color = "primary"
121
124
id = { ADD_CLASSROOM_VALIDATE_BUTTON_ID }
125
+ disabled = { ! this . isClassroomNameValid ( ) }
122
126
>
123
127
{ t ( 'Validate' ) }
124
128
</ Button >
0 commit comments