1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
+ import { connect } from 'react-redux' ;
4
+ import * as actions from '../../store/action_creators' ;
3
5
4
6
import { Card } from '../card' ;
5
7
import { TextInput , TextArea , SelectInput , CheckboxInput , DateInput } from '../form' ;
6
8
import { TASK_OPTIONS , FORM_ADD , TODO } from '../../lib/const' ;
7
9
8
- export default class MainForm extends React . Component {
10
+ class MainForm extends React . Component {
9
11
static propTypes = {
10
- formSate : PropTypes . string , // состояние формы (редактровать или добавить таску)
11
- taskForEdit : PropTypes . object , // номер таски, которую редактируют
12
- onSaveData : PropTypes . func
12
+ taskForEdit : PropTypes . string , // номер таски, которую редактируют
13
+ formState : PropTypes . string ,
14
+ taskList : PropTypes . array ,
15
+ taskAdd : PropTypes . func ,
16
+ setFormState : PropTypes . func ,
17
+ taskEdit : PropTypes . func
13
18
} ;
14
19
15
20
constructor ( props ) {
@@ -21,15 +26,24 @@ export default class MainForm extends React.Component {
21
26
}
22
27
}
23
28
24
- static getDerivedStateFromProps = ( nextProps , state ) => {
25
- if ( ! state . propsFlag && nextProps . taskForEdit ) {
26
- return {
27
- data : nextProps . taskForEdit ,
28
- propsFlag : true
29
- }
29
+ // static getDerivedStateFromProps = (nextProps, state) => {
30
+ // if (!state.propsFlag && nextProps.taskForEdit !== null && nextProps.taskList.length) {
31
+ // const data = { ...nextProps.taskList.find(item => item.id === nextProps.taskForEdit) };
32
+ // return {
33
+ // data,
34
+ // propsFlag: true
35
+ // }
36
+ // }
37
+ // return null;
38
+ // };
39
+
40
+ componentDidUpdate ( prevProps ) {
41
+ if ( this . props . taskForEdit !== prevProps . taskForEdit ) {
42
+ this . setState ( {
43
+ data : { ...this . props . taskList . find ( item => item . id === this . props . taskForEdit ) }
44
+ } )
30
45
}
31
- return null ;
32
- } ;
46
+ }
33
47
34
48
35
49
handleChange = ( e ) => {
@@ -43,32 +57,39 @@ export default class MainForm extends React.Component {
43
57
} ) ) ;
44
58
}
45
59
46
- handleClearForm = ( ) => {
47
- this . setState ( { data : { } } ) ;
48
- }
49
-
50
60
handleResetData = ( ) => {
51
- this . props . onResetData ( ) ;
52
- this . setState ( { data : { } } ) ;
61
+ if ( this . props . formState === FORM_ADD ) {
62
+ this . setState ( { data : { } } ) ;
63
+ } else {
64
+ this . props . setFormState ( { formState : FORM_ADD , taskId : null } ) ;
65
+ this . setState ( { data : { } } ) ;
66
+ }
53
67
}
54
68
55
69
handleSaveData = ( e ) => {
56
70
e . preventDefault ( ) ;
57
71
const { data } = this . state ;
58
72
const { taskStatus = TODO } = data ;
59
- if ( this . props . onSaveData ( { ...data , taskStatus, id : String ( Date . now ( ) ) } ) === true ) {
60
- this . setState ( { data : { } } ) ;
73
+ const { taskList, taskForEdit } = this . props ;
74
+ if ( this . props . formState === FORM_ADD ) {
75
+ const newItem = { ...data , taskStatus, id : String ( Date . now ( ) ) } ;
76
+ this . props . taskAdd ( { newItem, taskList } ) ;
77
+ } else {
78
+ const newItem = { ...data } ;
79
+ this . props . taskEdit ( { newItem, taskList, taskForEdit } ) ;
80
+ this . props . setFormState ( { formState : FORM_ADD , taskId : null } )
61
81
}
82
+ this . setState ( { data : { } } ) ;
62
83
}
63
84
64
85
render ( ) {
65
86
return (
66
87
< Card >
67
88
< h4 >
68
89
{
69
- this . props . formSate === FORM_ADD
90
+ this . props . formState === FORM_ADD
70
91
? "Add new task"
71
- : `Edit task ${ this . props . taskForEdit . taskName || "" } `
92
+ : `Edit task ${ this . state . data . taskName || "" } `
72
93
}
73
94
</ h4 >
74
95
@@ -119,27 +140,49 @@ export default class MainForm extends React.Component {
119
140
< div className = 'col-sm-6' >
120
141
< button
121
142
type = "submit"
122
- className = { `btn ${ this . props . formSate === FORM_ADD ? 'btn-primary' : 'btn-warning' } ` }
143
+ className = { `btn ${ this . props . formState === FORM_ADD ?
144
+ 'btn-primary' : 'btn-warning' } `}
123
145
onClick = { this . handleSaveData }
124
146
>
125
147
{
126
- this . props . formSate === FORM_ADD ? "Add new task" : "Save changes"
148
+ this . props . formState === FORM_ADD ?
149
+ "Add new task" : "Save changes"
127
150
}
128
151
</ button >
129
152
</ div >
130
153
< div className = 'col-sm-6' >
131
154
< button
132
155
type = "submit"
133
- className = { `btn ${ this . props . formSate === FORM_ADD ? 'btn-secondary' : 'btn-danger' } ` }
156
+ className = { `btn ${ this . props . formState === FORM_ADD ?
157
+ 'btn-secondary' : 'btn-danger' } `}
134
158
onClick = { this . handleResetData }
135
159
>
136
160
{
137
- this . props . formSate === FORM_ADD ? "Clear form" : "Cancel"
161
+ this . props . formState === FORM_ADD ?
162
+ "Clear form" : "Cancel"
138
163
}
139
164
</ button >
140
165
</ div >
141
166
</ div >
142
167
</ Card >
143
168
)
144
169
}
145
- }
170
+ }
171
+
172
+ const mapStateToProps = store => {
173
+ return {
174
+ taskForEdit : store . app . taskId ,
175
+ formState : store . app . formState ,
176
+ taskList : [ ...store . app . taskList ]
177
+ }
178
+ }
179
+
180
+ const mapDispatchToProps = dispatch => {
181
+ return {
182
+ taskAdd : payload => dispatch ( actions . addTask ( payload ) ) ,
183
+ taskEdit : payload => dispatch ( actions . editTask ( payload ) ) ,
184
+ setFormState : payload => dispatch ( actions . setFormState ( payload ) )
185
+ }
186
+ }
187
+
188
+ export default connect ( mapStateToProps , mapDispatchToProps ) ( MainForm ) ;
0 commit comments