@@ -3,6 +3,7 @@ import Header from './components/Header';
3
3
import Navigation from './components/Navigation' ;
4
4
import ReadArticle from './components/ReadArticle' ;
5
5
import CreateArticle from './components/CreateArticle' ;
6
+ import UpdateArticle from './components/UpdateArticle' ;
6
7
import Control from './components/Control' ;
7
8
import './App.css' ;
8
9
@@ -21,7 +22,7 @@ class App extends React.Component {
21
22
description : "Welcome to Web"
22
23
} ,
23
24
selected_contents : {
24
- id :0
25
+ id : 0
25
26
} ,
26
27
contents : [
27
28
{
@@ -43,20 +44,27 @@ class App extends React.Component {
43
44
} ;
44
45
}
45
46
47
+ getContent ( ) {
48
+ let content ;
49
+ for ( let i = 0 ; i < this . state . contents . length ; i ++ ) {
50
+ if ( this . state . contents [ i ] . id === this . state . selected_contents ) {
51
+ content = this . state . contents [ i ] ;
52
+ break ;
53
+ }
54
+ }
55
+ return content ;
56
+ }
57
+
46
58
render ( ) {
47
59
let _title , _desc , _article ;
48
60
if ( this . state . mode === 'Welcome' ) {
49
61
_title = this . state . Welcome . title ;
50
62
_desc = this . state . Welcome . description ;
51
63
_article = < ReadArticle title = { _title } description = { _desc } > </ ReadArticle > ;
52
64
} else if ( this . state . mode === 'Read' ) {
53
- for ( let i = 0 ; i < this . state . contents . length ; i ++ ) {
54
- if ( this . state . contents [ i ] . id === this . state . selected_contents ) {
55
- _title = this . state . contents [ i ] . title ;
56
- _desc = this . state . contents [ i ] . description ;
57
- break ;
58
- }
59
- }
65
+ const content = this . getContent ( ) ;
66
+ _title = content . title ;
67
+ _desc = content . description ;
60
68
_article = < ReadArticle title = { _title } description = { _desc } > </ ReadArticle > ;
61
69
} else if ( this . state . mode === 'Create' ) {
62
70
_article = < CreateArticle onCreate = { function ( _title , _desc ) {
@@ -69,10 +77,47 @@ class App extends React.Component {
69
77
const grpArticle = this . state . contents . concat ( newArticle ) ;
70
78
this . setState (
71
79
{
80
+ mode : 'Read' ,
81
+ selected_contents : newArticle . id ,
72
82
contents : grpArticle
73
83
}
74
84
) ;
75
85
} . bind ( this ) } > </ CreateArticle >
86
+ } else if ( this . state . mode === 'Update' ) {
87
+ const content = this . getContent ( ) ;
88
+ _article = < UpdateArticle
89
+ content = { content }
90
+ onUpdate = { function ( _id , _title , _desc ) {
91
+ let _contents = Array . from ( this . state . contents ) ;
92
+ for ( let i = 0 ; i < _contents . length ; i ++ ) {
93
+ if ( _contents [ i ] . id === _id ) {
94
+ _contents [ i ] . title = _title ;
95
+ _contents [ i ] . description = _desc ;
96
+ }
97
+ }
98
+ this . setState (
99
+ {
100
+ mode : 'Read' ,
101
+ contents : _contents
102
+ }
103
+ ) ;
104
+ } . bind ( this ) }
105
+ > </ UpdateArticle >
106
+ }
107
+ else if ( this . state . mode === 'Delete' ) {
108
+ let _contents = Array . from ( this . state . contents ) ;
109
+ for ( let i = 0 ; i < _contents . length ; i ++ ) {
110
+ if ( _contents [ i ] . id === this . state . selected_contents ) {
111
+ _contents . splice ( i , 1 ) ;
112
+ break ;
113
+ }
114
+ }
115
+ this . setState (
116
+ {
117
+ mode : 'Welcome' ,
118
+ contents : _contents
119
+ }
120
+ ) ;
76
121
}
77
122
return (
78
123
< div className = "App" >
@@ -101,6 +146,14 @@ class App extends React.Component {
101
146
< Control
102
147
mode = { this . state . mode }
103
148
onChangePage = { function ( _mode ) {
149
+ if ( _mode === 'Delete' ) {
150
+ if ( window . confirm ( 'Delete?' ) ) {
151
+
152
+ } else {
153
+ return ;
154
+ }
155
+ }
156
+
104
157
this . setState (
105
158
{
106
159
mode : _mode
0 commit comments