File tree Expand file tree Collapse file tree 4 files changed +48
-13
lines changed Expand file tree Collapse file tree 4 files changed +48
-13
lines changed Original file line number Diff line number Diff line change 8
8
"http-proxy-middleware" : " ^0.19.1" ,
9
9
"react" : " ^16.8.5" ,
10
10
"react-dom" : " ^16.8.5" ,
11
- "react-scripts" : " 2.1.8"
11
+ "react-scripts" : " 2.1.8" ,
12
+ "prop-types" : " ^15.5.7"
12
13
},
13
14
"scripts" : {
14
15
"start" : " react-scripts start" ,
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
4
- const List = ( { list } ) => (
4
+ const List = ( { list, onRemoveList } ) => (
5
5
< div >
6
6
< div className = "single-listItem" key = { list . id } >
7
7
< h4 > { list . title } </ h4 >
8
8
< p > { list . excerpt } </ p >
9
+ < button
10
+ type = "button"
11
+ onClick = { ( ) => {
12
+ onRemoveList ( list . id ) ;
13
+ } }
14
+ >
15
+ Remove
16
+ </ button >
9
17
</ div >
10
18
</ div >
11
19
) ;
12
20
13
- List . propTypes = { list : PropTypes . object } ;
14
- List . defaultProps = { list : { } } ;
21
+ List . propTypes = { list : PropTypes . object , onRemoveList : PropTypes . func } ;
22
+ List . defaultProps = { list : { } , onRemoveList : ( ) => { } } ;
15
23
16
24
export default List ;
Original file line number Diff line number Diff line change
1
+ /* eslint-disable no-unused-expressions */
1
2
import React , { Component } from 'react' ;
2
3
import axios from 'axios' ;
3
4
import List from './List' ;
4
5
import NewListForm from './NewListForm' ;
5
6
6
7
class ListsContainer extends Component {
7
- state = {
8
- lists : [ ] ,
9
- } ;
8
+ constructor ( props ) {
9
+ super ( props ) ;
10
+ this . state = {
11
+ lists : [ ] ,
12
+ } ;
13
+ }
10
14
11
15
componentDidMount = ( ) => {
12
- this . loadLists ( ) ;
16
+ this . loadLists ;
13
17
} ;
14
18
15
- loadLists = ( ) =>
16
- axios
19
+ get loadLists ( ) {
20
+ return axios
17
21
. get ( 'api/v1/lists.json' )
18
22
. then ( response => {
19
23
console . log ( response ) ;
@@ -22,14 +26,36 @@ class ListsContainer extends Component {
22
26
} ) ;
23
27
} )
24
28
. catch ( error => console . log ( error ) ) ;
29
+ }
30
+
31
+ addNewList = ( title , excerpt ) => {
32
+ axios
33
+ . post ( '/api/v1/lists' , { list : { title, excerpt } } )
34
+ . then ( response => {
35
+ console . log ( response ) ;
36
+ const lists = [ ...this . state . lists , response . data ] ;
37
+ this . setState ( { lists } ) ;
38
+ } )
39
+ . catch ( error => {
40
+ console . log ( error ) ;
41
+ } ) ;
42
+ } ;
43
+
44
+ removeList = id => {
45
+ axios . delete ( `/api/v1/lists/${ id } ` ) . then ( ( ) => {
46
+ const { lists } = this . state ;
47
+ const newlist = lists . filter ( list => list . id !== id ) ;
48
+ this . setState ( { lists : newlist } ) ;
49
+ } ) ;
50
+ } ;
25
51
26
52
render ( ) {
27
53
const { lists } = this . state ;
28
54
return (
29
55
< React . Fragment >
30
56
< div className = "lists-container" >
31
57
{ lists . map ( list => (
32
- < List list = { list } key = { list . id } />
58
+ < List list = { list } key = { list . id } onRemoveList = { this . removeList } />
33
59
) ) }
34
60
</ div >
35
61
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
4
- const NewListForm = ( { onNewList = f => f } ) => {
4
+ const NewListForm = ( { onNewList } ) => {
5
5
let title ;
6
6
let excerpt ;
7
7
const submit = e => {
8
8
e . preventDefault ( ) ;
9
- onNewList ( _title . value , _excerpt . value ) ;
9
+ onNewList ( title . value , excerpt . value ) ;
10
10
title . value = '' ;
11
11
excerpt . value = '' ;
12
12
title . focus ( ) ;
You can’t perform that action at this time.
0 commit comments