@@ -3,6 +3,7 @@ import Header from './components/Header';
33import Navigation from './components/Navigation' ;
44import ReadArticle from './components/ReadArticle' ;
55import CreateArticle from './components/CreateArticle' ;
6+ import UpdateArticle from './components/UpdateArticle' ;
67import Control from './components/Control' ;
78import './App.css' ;
89
@@ -21,7 +22,7 @@ class App extends React.Component {
2122 description : "Welcome to Web"
2223 } ,
2324 selected_contents : {
24- id :0
25+ id : 0
2526 } ,
2627 contents : [
2728 {
@@ -43,20 +44,27 @@ class App extends React.Component {
4344 } ;
4445 }
4546
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+
4658 render ( ) {
4759 let _title , _desc , _article ;
4860 if ( this . state . mode === 'Welcome' ) {
4961 _title = this . state . Welcome . title ;
5062 _desc = this . state . Welcome . description ;
5163 _article = < ReadArticle title = { _title } description = { _desc } > </ ReadArticle > ;
5264 } 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 ;
6068 _article = < ReadArticle title = { _title } description = { _desc } > </ ReadArticle > ;
6169 } else if ( this . state . mode === 'Create' ) {
6270 _article = < CreateArticle onCreate = { function ( _title , _desc ) {
@@ -69,10 +77,47 @@ class App extends React.Component {
6977 const grpArticle = this . state . contents . concat ( newArticle ) ;
7078 this . setState (
7179 {
80+ mode : 'Read' ,
81+ selected_contents : newArticle . id ,
7282 contents : grpArticle
7383 }
7484 ) ;
7585 } . 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+ ) ;
76121 }
77122 return (
78123 < div className = "App" >
@@ -101,6 +146,14 @@ class App extends React.Component {
101146 < Control
102147 mode = { this . state . mode }
103148 onChangePage = { function ( _mode ) {
149+ if ( _mode === 'Delete' ) {
150+ if ( window . confirm ( 'Delete?' ) ) {
151+
152+ } else {
153+ return ;
154+ }
155+ }
156+
104157 this . setState (
105158 {
106159 mode : _mode
0 commit comments