1
- import React from "react" ;
1
+ import React , { Component } from "react" ;
2
2
import { Card , CardImg , CardImgOverlay , CardTitle , Breadcrumb , BreadcrumbItem , CardBody , CardText } from "reactstrap" ;
3
3
import { Link } from 'react-router-dom' ;
4
+ import {
5
+ Button , Modal , ModalBody , ModalHeader , Label , Row , Col
6
+ } from "reactstrap" ;
7
+
8
+ import { Control , LocalForm , Errors } from 'react-redux-form' ;
9
+
10
+
11
+
12
+
13
+ /**........................ comment component ends ................................................. */
14
+ //// validators
15
+ const required = ( val ) => val && val . length ; //value > 0
16
+ const maxLength = ( len ) => ( val ) => ! ( val ) || ( val . length <= len ) ;
17
+ const minLength = ( len ) => ( val ) => ( val ) && ( val . length >= len ) ;
18
+
19
+ class CommentForm extends Component {
20
+
21
+ constructor ( props ) {
22
+ super ( props ) ;
23
+
24
+
25
+ this . state = {
26
+ isCommentFormModalOpen : false
27
+ } ;
28
+
29
+ this . toggleCommentFormModal = this . toggleCommentFormModal . bind ( this ) ;
30
+ this . handleCommentFormSubmit = this . handleCommentFormSubmit . bind ( this ) ;
31
+
32
+ }
33
+
34
+ handleCommentFormSubmit ( values ) {
35
+ console . log ( "Current State is: " + JSON . stringify ( values ) ) ;
36
+ alert ( "Current State is: " + JSON . stringify ( values ) ) ;
37
+
38
+
39
+ }
40
+
41
+ toggleCommentFormModal ( ) {
42
+ this . setState ( {
43
+ isCommentFormModalOpen : ! this . state . isCommentFormModalOpen
44
+ } ) ;
45
+ }
46
+
47
+
48
+ render ( ) {
49
+ return (
50
+ < React . Fragment >
51
+ < Button outline onClick = { this . toggleCommentFormModal } >
52
+ < span className = "fa fa-comments fa-lg" > </ span > Submit Comment
53
+ </ Button >
54
+
55
+
56
+ { /* commentform Modal */ }
57
+ < Modal isOpen = { this . state . isCommentFormModalOpen } toggle = { this . toggleCommentFormModal } >
58
+ < ModalHeader toggle = { this . toggleCommentFormModal } > Submit Comment </ ModalHeader >
59
+ < ModalBody >
60
+
61
+ < LocalForm onSubmit = { ( values ) => this . handleCommentFormSubmit ( values ) } >
62
+
63
+ { /* rating */ }
64
+ < Row className = "form-group" >
65
+ < Label htmlFor = "rating" md = { 12 } > Rating</ Label >
66
+ < Col md = { 12 } >
67
+ < Control . select model = ".rating"
68
+ className = "form-control"
69
+ name = "rating"
70
+ id = "rating"
71
+ validators = { {
72
+ required
73
+ } }
74
+ >
75
+ < option > Please Select</ option >
76
+ < option > 1</ option >
77
+ < option > 2</ option >
78
+ < option > 3</ option >
79
+ < option > 4</ option >
80
+ < option > 5</ option >
81
+ </ Control . select >
82
+ < Errors
83
+ className = "text-danger"
84
+ model = ".author"
85
+ show = "touched"
86
+ messages = { {
87
+ required : 'Required' ,
88
+ } }
89
+ />
90
+ </ Col >
91
+ </ Row >
92
+
93
+
94
+ { /* author */ }
95
+ < Row className = "form-group" >
96
+ < Label htmlFor = "author" md = { 12 } > Your Name </ Label >
97
+ < Col md = { 12 } >
98
+ < Control . text model = ".author" id = "author" name = "author"
99
+ placeholder = "First Name"
100
+ className = "form-control"
101
+ validators = { {
102
+ required, minLength : minLength ( 3 ) , maxLength : maxLength ( 15 )
103
+ } }
104
+ />
105
+ < Errors
106
+ className = "text-danger"
107
+ model = ".author"
108
+ show = "touched"
109
+ messages = { {
110
+ required : 'Required' ,
111
+ minLength : 'Must be greater than 2 characters' ,
112
+ maxLength : 'Must be 15 characters or less'
113
+ } }
114
+ />
115
+ </ Col >
116
+ </ Row >
117
+
118
+
119
+
120
+
121
+ { /* comment */ }
122
+ < Row className = "form-group" >
123
+ < Label htmlFor = "comment" md = { 12 } > Comment</ Label >
124
+ < Col md = { 12 } >
125
+ < Control . textarea model = ".comment" id = "comment" name = "comment"
126
+ rows = "6"
127
+ className = "form-control"
128
+ validators = { {
129
+ required
130
+ } }
131
+ />
132
+ < Errors
133
+ className = "text-danger"
134
+ model = ".author"
135
+ show = "touched"
136
+ messages = { {
137
+ required : 'Required' ,
138
+ } }
139
+ />
140
+ </ Col >
141
+
142
+ </ Row >
143
+
144
+ { /* submit button */ }
145
+ < Row className = "form-group" >
146
+ < Col >
147
+ < Button type = "submit" color = "primary" >
148
+ Submit
149
+ </ Button >
150
+ </ Col >
151
+ </ Row >
152
+
153
+ </ LocalForm >
154
+
155
+ </ ModalBody >
156
+ </ Modal >
157
+
158
+
159
+ </ React . Fragment >
160
+ ) ;
161
+ }
162
+ }
163
+
164
+ /**........................ comment component ends ................................................. */
165
+
166
+
167
+
168
+
169
+
170
+
171
+
4
172
5
173
function RenderDish ( { dish} ) {
6
174
if ( dish != null ) {
@@ -23,12 +191,13 @@ import { Link } from 'react-router-dom';
23
191
}
24
192
}
25
193
26
- function RenderComments ( { comments} ) {
194
+ function RenderComments ( { dish , comments} ) {
27
195
if ( comments == null ) {
28
196
return ( < div > </ div > )
29
197
}
30
198
const cmnts = comments . map ( comment => {
31
199
return (
200
+
32
201
< li key = { comment . id } >
33
202
< p > { comment . comment } </ p >
34
203
< p > -- { comment . author } ,
@@ -40,6 +209,7 @@ import { Link } from 'react-router-dom';
40
209
} ) . format ( new Date ( comment . date ) ) }
41
210
</ p >
42
211
</ li >
212
+
43
213
)
44
214
} )
45
215
return (
@@ -48,7 +218,7 @@ import { Link } from 'react-router-dom';
48
218
< ul className = 'list-unstyled' >
49
219
{ cmnts }
50
220
</ ul >
51
-
221
+ < CommentForm dish = { dish } comments = { comments } />
52
222
</ div >
53
223
)
54
224
}
@@ -83,8 +253,10 @@ import { Link } from 'react-router-dom';
83
253
84
254
< div className = 'row' >
85
255
< RenderDish dish = { props . dish } />
86
- < RenderComments comments = { props . comments } />
256
+ < RenderComments dish = { props . dish } comments = { props . comments } />
87
257
</ div >
258
+
259
+
88
260
</ div >
89
261
)
90
262
}
0 commit comments