Sfoglia il codice sorgente

split comment classes into a separate file/module

Lucas Stadler 11 anni fa
parent
commit
72c5a34565
2 ha cambiato i file con 103 aggiunte e 101 eliminazioni
  1. 102 0
      js/react/comments.js
  2. 1 101
      js/react/index.js

+ 102 - 0
js/react/comments.js

@ -0,0 +1,102 @@
1
import React from "react";
2
import marked from "marked";
3
4
export class CommentForm extends React.Component {
5
	constructor() {
6
		this.handleSubmit = this.handleSubmit.bind(this);
7
	}
8
9
	handleSubmit(ev) {
10
		ev.preventDefault();
11
		let author = this.refs.author.getDOMNode().value.trim();
12
		let text = this.refs.text.getDOMNode().value.trim();
13
		if (!text || !author) {
14
			return;
15
		}
16
17
		this.props.onCommentSubmit({author, text});
18
		this.refs.author.getDOMNode().value = '';
19
		this.refs.text.getDOMNode().value = '';
20
	}
21
22
	render() {
23
		return (
24
			<form className="comment-form" onSubmit={this.handleSubmit}>
25
				<input type="text" placeholder="Your name" ref="author" />
26
				<input type="text" placeholder="Say something..." ref="text" />
27
				<input type="submit" value="Say it!" />
28
			</form>
29
		);
30
	}
31
}
32
33
export class CommentList extends React.Component {
34
	render() {
35
		let commentNodes = this.props.data.map((comment) => {
36
			return (
37
				<Comment author={comment.author}>
38
					{comment.text}
39
				</Comment>
40
			);
41
		});
42
43
		return (
44
			<div className="comment-list">
45
				{commentNodes}
46
			</div>
47
		);
48
	}
49
}
50
51
export class CommentBox extends React.Component {
52
	constructor(props) {
53
		super(props);
54
		this.state = {data: []}
55
56
		this.handleCommentSubmit = this.handleCommentSubmit.bind(this);
57
	}
58
59
	handleCommentSubmit(comment) {
60
		let comments = this.state.data;
61
		let newComments = comments.concat([comment]);
62
		this.setState({data: newComments});
63
64
		// TODO: Send to server
65
	}
66
67
	componentDidMount() {
68
		let xhr = new XMLHttpRequest();
69
		xhr.open('GET', this.props.url);
70
		xhr.onreadystatechange = function() {
71
			if (xhr.readyState == XMLHttpRequest.DONE) {
72
				this.setState({data: JSON.parse(xhr.responseText)});
73
			}
74
		}.bind(this);
75
		xhr.onerror = function(err) {
76
			console.error(this.props.url, xhr.status, err);
77
		}.bind(this);
78
		xhr.send();
79
	}
80
81
	render() {
82
		return (
83
			<div className="comment-box">
84
				<h1>Comments</h1>
85
				<CommentList data={this.state.data} />
86
				<CommentForm onCommentSubmit={this.handleCommentSubmit} />
87
			</div>
88
		);
89
	}
90
}
91
92
export class Comment extends React.Component {
93
	render() {
94
		let rawMarkup = marked(this.props.children.toString());
95
		return (
96
			<div className="comment">
97
				<h2 className="comment-author">{this.props.author}</h2>
98
				<span dangerouslySetInnerHTML={{__html: rawMarkup}} />
99
			</div>
100
		);
101
	}
102
}

+ 1 - 101
js/react/index.js

@ -1,108 +1,8 @@
1 1
// following along http://facebook.github.io/react/docs/tutorial.html
2 2
3 3
import React from "react";
4
import marked from "marked";
4
import {CommentBox} from "./comments.js";
5 5
6 6
require('./style.css');
7 7
8
class CommentForm extends React.Component {
9
	constructor() {
10
		this.handleSubmit = this.handleSubmit.bind(this);
11
	}
12
13
	handleSubmit(ev) {
14
		ev.preventDefault();
15
		let author = this.refs.author.getDOMNode().value.trim();
16
		let text = this.refs.text.getDOMNode().value.trim();
17
		if (!text || !author) {
18
			return;
19
		}
20
21
		this.props.onCommentSubmit({author, text});
22
		this.refs.author.getDOMNode().value = '';
23
		this.refs.text.getDOMNode().value = '';
24
	}
25
26
	render() {
27
		return (
28
			<form className="comment-form" onSubmit={this.handleSubmit}>
29
				<input type="text" placeholder="Your name" ref="author" />
30
				<input type="text" placeholder="Say something..." ref="text" />
31
				<input type="submit" value="Say it!" />
32
			</form>
33
		);
34
	}
35
}
36
37
class CommentList extends React.Component {
38
	render() {
39
		let commentNodes = this.props.data.map((comment) => {
40
			return (
41
				<Comment author={comment.author}>
42
					{comment.text}
43
				</Comment>
44
			);
45
		});
46
47
		return (
48
			<div className="comment-list">
49
				{commentNodes}
50
			</div>
51
		);
52
	}
53
}
54
55
class CommentBox extends React.Component {
56
	constructor(props) {
57
		super(props);
58
		this.state = {data: []}
59
60
		this.handleCommentSubmit = this.handleCommentSubmit.bind(this);
61
	}
62
63
	handleCommentSubmit(comment) {
64
		let comments = this.state.data;
65
		let newComments = comments.concat([comment]);
66
		this.setState({data: newComments});
67
68
		// TODO: Send to server
69
	}
70
71
	componentDidMount() {
72
		let xhr = new XMLHttpRequest();
73
		xhr.open('GET', this.props.url);
74
		xhr.onreadystatechange = function() {
75
			if (xhr.readyState == XMLHttpRequest.DONE) {
76
				this.setState({data: JSON.parse(xhr.responseText)});
77
			}
78
		}.bind(this);
79
		xhr.onerror = function(err) {
80
			console.error(this.props.url, xhr.status, err);
81
		}.bind(this);
82
		xhr.send();
83
	}
84
85
	render() {
86
		return (
87
			<div className="comment-box">
88
				<h1>Comments</h1>
89
				<CommentList data={this.state.data} />
90
				<CommentForm onCommentSubmit={this.handleCommentSubmit} />
91
			</div>
92
		);
93
	}
94
}
95
96
class Comment extends React.Component {
97
	render() {
98
		let rawMarkup = marked(this.props.children.toString());
99
		return (
100
			<div className="comment">
101
				<h2 className="comment-author">{this.props.author}</h2>
102
				<span dangerouslySetInnerHTML={{__html: rawMarkup}} />
103
			</div>
104
		);
105
	}
106
}
107
108 8
React.render(<CommentBox url="comments.json" />, document.getElementById("content"));